This repository was 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
Expand file tree
/
Copy pathFollowerBot.Actions.cs
More file actions
545 lines (493 loc) · 22.7 KB
/
Copy pathFollowerBot.Actions.cs
File metadata and controls
545 lines (493 loc) · 22.7 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using Microsoft.Extensions.Logging;
namespace FlickrFollowerBot
{
public partial class FollowerBot
{
private IEnumerable<string> GetContactList(string subUrl)
{
MoveTo(Data.UserContactUrl + subUrl);
List<string> ret = Selenium.GetAttributes(Config.CssContactPhotos)
.ToList();
int page = 2;
while (Selenium.GetElements("a[href*=\"/?page=" + page.ToString(CultureInfo.InvariantCulture) + "\"]", true, true).Any())
{
MoveTo(Data.UserContactUrl + subUrl + "/?page=" + page.ToString(CultureInfo.InvariantCulture));
ret.AddRange(Selenium.GetAttributes(Config.CssContactPhotos));
page++;
}
return ret;
}
private void AddForced(string configName, string configValue, Queue<string> queue)
{
if (!string.IsNullOrWhiteSpace(configValue))
{
int added = 0;
foreach (string s in configValue.Split(',', StringSplitOptions.RemoveEmptyEntries))
{
if (s.StartsWith(Config.UrlRoot, StringComparison.OrdinalIgnoreCase))
{
if (!queue.Contains(s))
{
added++;
queue.Enqueue(s);
}
else
{
Log.LogDebug("{0} already in ${1}", s, configName);
}
}
else
{
Log.LogWarning("Check {0} Url format for {1}", configName, s);
}
}
Log.LogDebug("${0} +{1} AddForced", configName, added);
}
}
private void PostAuthInit()
{
ClickWaitIfPresent(Config.CssLoginWarning);
if (!Data.MyContactsUpdate.HasValue
|| Config.BotCacheTimeLimitHours <= 0
|| DateTime.UtcNow > Data.MyContactsUpdate.Value.AddHours(Config.BotCacheTimeLimitHours))
{
Data.MyContacts = GetContactList(Config.UrlContacts).ToHashSet();
Log.LogDebug("$MyContacts ={0}", Data.MyContacts.Count);
Data.MyContactsBanned = GetContactList(Config.UrlContactsBlocked).ToHashSet();
Log.LogDebug("$MyContactsBanned ={0}", Data.MyContactsBanned.Count);
Data.MyContactsUpdate = DateTime.UtcNow;
}
AddForced("AddContactsToFav", Config.AddContactsToFav, Data.ContactsToFav);
AddForced("AddContactsToFollow", Config.AddContactsToFollow, Data.ContactsToFollow);
AddForced("AddContactsToUnfollow", Config.AddContactsToUnfollow, Data.ContactsToUnfollow);
AddForced("AddPhotosToFav", Config.AddPhotosToFav, Data.PhotosToFav);
}
private void AuthLogin()
{
if (!MoveTo(Config.UrlLogin))
{
throw new NotSupportedException("FLICKR RETURN ERROR 500 ON " + Config.UrlLogin);
}
if (!string.IsNullOrWhiteSpace(Config.BotUserEmail))
{
Selenium.InputWrite(Config.CssLoginEmail, Config.BotUserEmail);
Selenium.EnterKey(Config.CssLoginEmail);
if (!string.IsNullOrWhiteSpace(Config.BotUserPassword))
{
Selenium.InputWrite(Config.CssLoginPassword, Config.BotUserPassword);
Selenium.EnterKey(Config.CssLoginPassword);
}
else
{
Log.LogWarning("Waiting user manual password validation...");
}
WaitUrlStartsWith(Config.UrlRoot); // loading may take some time
WaitHumanizer(); // after WaitUrlStartsWith because 1st loading may take extra time
// who am i ?
Data.UserContactUrl = Selenium.GetAttributes(Config.CssLoginMyself, "href", false)
.First(); // not single to be safe
if (Data.UserContactUrl.EndsWith('/')) // standardize
{
Data.UserContactUrl = Data.UserContactUrl.Remove(Data.UserContactUrl.Length - 1);
}
Data.CookiesInitDate = DateTime.UtcNow;
}
else
{
throw new FormatException("BotUserEmail required !");
}
}
private bool TryAuthCookies()
{
if (Data.Cookies != null && Data.Cookies.Any())
{
if (!MoveTo(Config.UrlRoot))
{
throw new NotSupportedException("FLICKR RETURN ERROR 500 ON " + Config.UrlRoot);
}
Selenium.Cookies = Data.Cookies; // need to have loaded the page 1st
Selenium.SessionStorage = Data.SessionStorage; // need to have loaded the page 1st
Selenium.LocalStorage = Data.LocalStorage; // need to have loaded the page 1st
if (!MoveTo(Config.UrlRoot))
{
throw new NotSupportedException("FLICKR RETURN ERROR 500 ON " + Config.UrlRoot);
}
//check cookie auth OK
// who am i ?
string curUserContactUrl = Selenium.GetAttributes(Config.CssLoginMyself, "href", false)
.FirstOrDefault(); // not single to be safe
if (curUserContactUrl != null && curUserContactUrl.EndsWith('/')) // standardize
{
curUserContactUrl = curUserContactUrl.Remove(curUserContactUrl.Length - 1);
}
if (Data.UserContactUrl.Equals(curUserContactUrl, StringComparison.OrdinalIgnoreCase))
{
return true;
}
else
{
Log.LogWarning("Couldn't log user from cookie. Try normal auth");
return false;
}
}
else
{
return false;
}
}
private void DetectExplored(bool doContact = true, bool doPost = true)
{
MoveTo(Config.UrlExplore); // 500 images so u can scrool some time...
SchroolDownLoop(Config.BotExploreScrools);
if (doContact)
{
string[] list = Selenium.GetAttributes(Config.CssExploreContact, "href", false)
.Distinct()
.Except(Data.MyContacts)
.Except(Data.MyContactsBanned)
.ToArray();// solve linq for multiple use
int c = Data.ContactsToFollow.Count;
foreach (string needToFollow in list.Except(Data.ContactsToFollow))
{
Data.ContactsToFollow.Enqueue(needToFollow);
}
Log.LogDebug("$ContactsToFollow +{0}", Data.ContactsToFollow.Count - c);
c = Data.ContactsToFav.Count;
foreach (string needToFollow in list.Except(Data.ContactsToFav))
{
Data.ContactsToFav.Enqueue(needToFollow);
}
Log.LogDebug("$ContactsToFav +{0}", Data.ContactsToFav.Count - c);
}
if (doPost)
{
string[] list = Selenium.GetAttributes(Config.CssPhotos, "href", false)
.Except(Data.PhotosToFav)
.ToArray();// solve linq for multiple use
int c = Data.PhotosToFav.Count;
foreach (string needToFav in list
.Except(Data.PhotosToFav))
{
Data.PhotosToFav.Enqueue(needToFav);
}
Log.LogDebug("$PhotosToFav +{0}", Data.PhotosToFav.Count - c);
}
}
private void SearchKeywords(bool doContact = true, bool doPhoto = true)
{
IEnumerable<string> keywords = Config.BotSearchKeywords?.Split(',', StringSplitOptions.RemoveEmptyEntries) ?? Enumerable.Empty<string>();
foreach (string keyword in keywords)
{
Log.LogDebug("Searching {0}", keyword);
MoveTo(Config.UrlSearch + HttpUtility.UrlEncode(keyword));
WaitHumanizer();
SchroolDownLoop(Config.BotSearchScrools);
if (doContact)
{
string[] list = Selenium.GetAttributes(Config.CssExploreContact, "href", false)
.Distinct()
.Except(Data.MyContacts)
.Except(Data.MyContactsBanned)
.ToArray();// solve for multiple use
int c = Data.ContactsToFollow.Count;
foreach (string needToFollow in list.Except(Data.ContactsToFollow))
{
Data.ContactsToFollow.Enqueue(needToFollow);
}
Log.LogDebug("$ContactsToFollow +{0}", Data.ContactsToFollow.Count - c);
c = Data.ContactsToFav.Count;
foreach (string needToFollow in list.Except(Data.ContactsToFav))
{
Data.ContactsToFav.Enqueue(needToFollow);
}
Log.LogDebug("$ContactsToFav +{0}", Data.ContactsToFav.Count - c);
}
if (doPhoto)
{
string[] list = Selenium.GetAttributes(Config.CssPhotos, "href", false)
.Except(Data.PhotosToFav)
.ToArray();// solve linq for multiple use
int c = Data.PhotosToFav.Count;
foreach (string needToFav in list
.Except(Data.PhotosToFav))
{
Data.PhotosToFav.Enqueue(needToFav);
}
Log.LogDebug("$PhotosToFav +{0}", Data.PhotosToFav.Count - c);
}
}
}
private void DetectContactsFollowBack()
{
IEnumerable<string> list = GetContactList(Config.UrlContactsOneWay)
.Except(Data.MyContactsBanned)
.ToList(); // Solve
int c = Data.ContactsToFollow.Count;
foreach (string needToFollow in list
.Except(Data.ContactsToFollow))
{
Data.ContactsToFollow.Enqueue(needToFollow);
}
Log.LogDebug("$ContactsToFollow +{0}", Data.ContactsToFollow.Count - c);
c = Data.ContactsToFav.Count;
foreach (string needToFollow in list
.Except(Data.ContactsToFav))
{
Data.ContactsToFav.Enqueue(needToFollow);
}
Log.LogDebug("$ContactsToFav +{0}", Data.ContactsToFav.Count - c);
}
private void DoContactsFollow()
{
int todo = PseudoRand.Next(Config.BotFollowTaskBatchMinLimit, Config.BotFollowTaskBatchMaxLimit);
int c = Data.ContactsToFollow.Count;
while (Data.ContactsToFollow.TryDequeue(out string uri) && todo > 0)
{
if (!MoveTo(uri))
{
Log.LogWarning("ACTION STOPED : FLICKR RETURN ERROR 500 ON ({0})", uri);
break; // no retry
}
try
{
MyContactsInTryout.Add(uri);
if (Selenium.GetElements(Config.CssContactFollow).Any()) // manage the already followed like this
{
Selenium.Click(Config.CssContactFollow);
Data.MyContacts.Add(uri);
WaitHumanizer();// the url relad may break a waiting ball
// issue detection : Manage limit to 20 follow on a new account : https://www.flickr.com/help/forum/en-us/72157651299881165/ Then there seem to be another limit
if (Selenium.GetElements(Config.CssContactFollow).Any()) // may be slow so will wait if required
{
WaitHumanizer();// give a last chance
if (Selenium.GetElements(Config.CssContactFollow, true, true).Any())
{
Log.LogWarning("ACTION STOPED : SEEMS USER CAN'T FOLLOW ({0}) ANYMORE", uri);
break; // no retry
}
}
// hang issue detection
if (Selenium.GetElements(Config.CssModalWaiterBalls, true, true).Any()) // will not wait
{
WaitHumanizer();// give a last chance...
if (Selenium.GetElements(Config.CssModalWaiterBalls, true, true).Any()) // will not wait
{
Log.LogWarning("ACTION STOPED : SEEMS FLICKR HANG ON THIS CONTACT ({0})", uri);
break; // no retry
}
}
todo--;
}
}
catch (Exception ex)
{
Log.LogWarning(default, ex, "ACTION STOPED : {0}", ex.GetBaseException().Message);
break; // stop this action
}
}
Log.LogDebug("$ContactsToFollow -{0}", c - Data.ContactsToFollow.Count);
}
private void PhotoFav(string url, out bool wasFaved, out bool inError)
{
inError = wasFaved = false;
if (!MoveTo(url))
{
WaitHumanizer(); // try again
if (!MoveTo(url))
{
Log.LogWarning("ACTION STOPED : FLICKR RETURN ERROR 500 ON ({0})", url);
inError = true;
}
}
//photo may have been deleted.
if (!inError
&& !Selenium.GetElements(Config.CssPhotosError404, true, true).Any() // doesn't count as an error : user may have deleted his pictures
&& !Selenium.GetElements(Config.CssPhotoFaved, true, true).Any()) // may have been already faved
{
try
{
Selenium.Click(Config.CssPhotoFave);
}
catch (OpenQA.Selenium.ElementClickInterceptedException ex) // check if we are in a Flickr Freeze (fluid-modal-overlay/balls over all)
{
Log.LogWarning(default, ex, "ACTION STOPED : FLICKER SEEMS TO NEED A RELOAD ({0})", ex.GetBaseException().Message);
inError = true;
}
WaitHumanizer();// the url relad may break a waiting ball
// fav issue detection
if (!inError && !Selenium.GetElements(Config.CssPhotoFaved).Any()) // will wait a little if required because it s an expected state
{
WaitHumanizer();// give a last chance...
if (!Selenium.GetElements(Config.CssPhotoFaved, true, true).Any()) // will wait a little if required because it s an expected state
{
Log.LogWarning("ACTION STOPED : SEEMS USER ({0}) CAN'T FAV ANYMORE", url);
inError = true;
}
}
// hang issue detection
if (!inError && Selenium.GetElements(Config.CssModalWaiterBalls, true, true).Any()) // will not wait
{
WaitHumanizer();// give a last chance...
if (Selenium.GetElements(Config.CssModalWaiterBalls, true, true).Any()) // will not wait
{
Log.LogWarning("ACTION STOPED : SEEMS FLICKR HANG ON THIS PICTURE ({0})", url);
inError = true;
}
}
wasFaved = true;
}
}
private void DoContactsFav()
{
if (Config.BotFavPictsPerContactMin > 0)
{
int contactsTodo = PseudoRand.Next(Config.BotContactsFavTaskBatchMinLimit, Config.BotContactsFavTaskBatchMaxLimit);
int c = Data.ContactsToFav.Count;
while (Data.ContactsToFav.TryDequeue(out string uri) && 0 < contactsTodo)
{
if (!MoveTo(uri))
{
Log.LogWarning("ACTION STOPED : FLICKR RETURN ERROR 500 ON ({0})", uri);
break; // no retry
}
try
{
Selenium.ScrollToBottom(); // else will only find 1st picts
int favsTodo = PseudoRand.Next(Config.BotFavPictsPerContactMin, Config.BotFavPictsPerContactMax);
favsTodo -= Selenium.GetElements(Config.CssPhotosFaved, false, true).Count(); // mainly no picture already faved :-) don t wait uselessl
if (0 < favsTodo)
{
string[] array = Selenium.GetAttributes(Config.CssPhotos, "href", false)
.ToArray(); // page change so, need to solve
int i = 0;
while (favsTodo > 0 && i < array.Length)
{
PhotoFav(array[i], out bool hasFaved, out bool inError);
if (inError)
{
break;
}
if (hasFaved)
{
favsTodo--;
}
i++;
}
contactsTodo--;
}
}
catch (Exception ex)
{
Log.LogWarning(default, ex, "ACTION STOPED : {0}", ex.GetBaseException().Message);
break; // stop this action
}
}
Log.LogDebug("$ContactsToFav -{0}", c - Data.ContactsToFav.Count);
}
}
private void DetectContactsUnfollowBack()
{
// contacts not family or friend
IEnumerable<string> listMutual = GetContactList(Config.UrlContactsMutual);
//mutual contacts
string[] listNotClose = GetContactList(Config.UrlContactsNotFriendAndFamily)
.ToArray(); // fix order by oldest 1st
if (Data.ContactsToUnfollow.Any()) // all data will be retried, so clear cache if required
{
Data.ContactsToUnfollow.Clear();
}
string[] result = listNotClose
.Except(listMutual)
.Except(MyContactsInTryout)
.ToArray(); // solve
int r = result.Length;
if (r > 0) // all data will be retried, so clear cache if required
{
if (Config.BotKeepSomeUnfollowerContacts > 0 && r > Config.BotKeepSomeUnfollowerContacts)
{
r -= Config.BotKeepSomeUnfollowerContacts;
}
foreach (string needToUnfollow in result
.Take(r))
{
Data.ContactsToUnfollow.Enqueue(needToUnfollow);
}
}
Log.LogDebug("$ContactsToUnfollow ={0}", Data.ContactsToUnfollow.Count);
}
private void DoContactsUnfollow()
{
int todo = PseudoRand.Next(Config.BotUnfollowTaskBatchMinLimit, Config.BotUnfollowTaskBatchMaxLimit);
int c = Data.ContactsToUnfollow.Count;
while (Data.ContactsToUnfollow.TryDequeue(out string uri) && todo > 0)
{
MoveTo(uri);
try
{
if (Selenium.GetElements(Config.CssContactFollowed).Any()) // manage the already unfollowed like this
{
Selenium.Click(Config.CssContactFollowed);
WaitHumanizer();// the url relad may break a waiting ball
Selenium.Click(Config.CssContactUnfollow);
WaitHumanizer();// the url relad may break a waiting ball
Data.MyContacts.Remove(uri);
MyContactsInTryout.Remove(uri);
Data.MyContactsBanned.Add(uri);
todo--;
}
}
catch (Exception ex)
{
Log.LogWarning(default, ex, "ACTION STOPED : {0}", ex.GetBaseException().Message);
break; // stop this action
}
}
Log.LogDebug("$ContactsToUnfollow -{0}", c - Data.ContactsToUnfollow.Count);
}
private void DetectRecentContactPhotos()
{
MoveTo(Config.UrlRecentContactPost, true); // if 1st action post login, the home page may be loaded for your account
SchroolDownLoop(Config.BotRecentContactPostScrools);
string[] list = Selenium.GetAttributes(Config.CssRecentContactPost, "href")
.ToArray();
int c = Data.PhotosToFav.Count;
foreach (string needToFav in list
.Except(Data.PhotosToFav))
{
Data.PhotosToFav.Enqueue(needToFav);
}
Log.LogDebug("$PhotosToFav +{0}", Data.PhotosToFav.Count - c);
}
private void DoPhotosFav()
{
int todo = PseudoRand.Next(Config.BotPhotoFavTaskBatchMinLimit, Config.BotPhotoFavTaskBatchMaxLimit);
int c = Data.PhotosToFav.Count;
while (Data.PhotosToFav.TryDequeue(out string uri) && todo > 0)
{
try
{
PhotoFav(uri, out bool wasFaved, out bool inError);
if (inError)
{
break;
}
else if (wasFaved)
{
todo--;
}
}
catch (Exception ex)
{
Log.LogWarning(default, ex, "ACTION STOPED : {0}", ex.GetBaseException().Message);
break; // stop this action
}
}
Log.LogDebug("$PhotosToFav -{0}", c - Data.PhotosToFav.Count);
}
}
}