This repository has been archived by the owner on May 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFollowerBot.Tools.cs
192 lines (172 loc) · 6.18 KB
/
FollowerBot.Tools.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace FlickrFollowerBot
{
public partial class FollowerBot
{
private static readonly Random PseudoRand = new Random(); // this pseudorandom number generator is safe here.
private void WaitingBalls()
{
do
{
Log.LogDebug("WaitingBalls...");
WaitMin();
}
while (Selenium.GetElements(Config.CssWaiterBalls, true, true).Any());
}
private void SchroolDownLoop(int loop)
{
for (int i = 0; i < loop; i++)
{
Selenium.ScrollToBottom();
WaitHumanizer();
WaitingBalls();
}
}
private void WaitMin()
{
Task.Delay(Config.BotStepMinWaitMs)
.Wait();
}
private void WaitHumanizer()
{
Task.Delay(PseudoRand.Next(Config.BotStepMinWaitMs, Config.BotStepMaxWaitMs))
.Wait();
}
private void WaitUrlStartsWith(string url)
{
while (!Selenium.Url.StartsWith(url, StringComparison.OrdinalIgnoreCase))
{
Log.LogDebug("WaitUrlStartsWith...");
WaitMin();
}
}
private bool MoveTo(string partialOrNotUrl, bool forceReload = false)
{
Log.LogDebug("GET {0}", partialOrNotUrl);
string target;
if (partialOrNotUrl.StartsWith(Config.UrlRoot, StringComparison.OrdinalIgnoreCase))
{
target = partialOrNotUrl;
}
else
{
target = Config.UrlRoot + partialOrNotUrl;
}
if (!target.Equals(Selenium.Url, StringComparison.OrdinalIgnoreCase) || forceReload)
{
Selenium.Url = target;
WaitHumanizer();
// try again once on error 500
if (!Selenium.GetElements(Config.CssError500, true, true).Any())
{
return true;
}
else
{
WaitMin();
Selenium.Url = target;
WaitMin();
WaitHumanizer();
return !Selenium.GetElements(Config.CssError500, true, true).Any();
}
}
else
{
return true; // no redirection si OK.
}
}
private void ClickWaitIfPresent(string cssSelector)
{
if (Selenium.ClickIfPresent(cssSelector))
{
WaitHumanizer();
}
}
private static IEnumerable<string> GetTasks(string runTasks, bool botSaveAfterEachAction, bool botSaveOnEnd, bool botSaveOnLoop, int botLoopTaskLimit)
{
StringBuilder tasks = new StringBuilder(runTasks.ToUpperInvariant());
if (botSaveAfterEachAction)
{
tasks = tasks
.Replace(",", ",SAVE,") // brut
.Replace("WAIT,SAVE", "WAIT") // useless save removed
.Replace("LOOP,SAVE", "LOOP") // useless save removed
.Replace("BEGINLOOP,SAVE", "BEGINLOOP"); // useless save removed
}
if (botSaveOnEnd || botSaveAfterEachAction) // botSaveAfterEachAction because last action doesn t have a , after and the replace didn t added it
{
tasks = tasks
.Append(",SAVE"); // last one
}
if (botSaveOnLoop && !botSaveAfterEachAction)
{
tasks = tasks
.Replace(",LOOP", ",SAVE,LOOP");
}
tasks = tasks
.Replace("SAVE,LOOP,SAVE", "SAVE,LOOP") // small optim if Save on loop and save at end and finish by a loop
.Replace("SAVE,SAVE", "SAVE"); // both config save management could have duplicated this task
string computedTasks = tasks.ToString();
// Loop Management
int iEnd = computedTasks
.IndexOf(",LOOP");
if (iEnd > 0)
{
int iStart = computedTasks.IndexOf("BEGINLOOP,");
tasks = new StringBuilder(computedTasks); // faster work on string
tasks = tasks
.Replace(",LOOP", "");
string loopedTasks;
if (iStart >= 0)
{
iEnd -= 10;
loopedTasks = computedTasks.Substring(iStart + 10, iEnd - iStart);
tasks = tasks.Remove(iStart, 10); // BEGINLOOP,
}
else
{
loopedTasks = computedTasks.Substring(0, iEnd);
}
if (botLoopTaskLimit > 0)
{
for (int i = 0; i < botLoopTaskLimit; i++)
{
tasks = tasks.Insert(iEnd, loopedTasks);
tasks = tasks.Insert(iEnd, ',');
}
}
else
{
throw new ArgumentOutOfRangeException(nameof(botLoopTaskLimit), "Config.BotLoopTaskLimit must be greater than 0 when LOOP task is used");
}
computedTasks = tasks.ToString(); // resolve
}
return computedTasks.Split(',', StringSplitOptions.RemoveEmptyEntries);
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
private readonly SeleniumWrapper Selenium;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
Selenium.Dispose();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion IDisposable Support
}
}