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.cs
More file actions
178 lines (151 loc) · 6.67 KB
/
FollowerBot.cs
File metadata and controls
178 lines (151 loc) · 6.67 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
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace FlickrFollowerBot
{
public partial class FollowerBot : IDisposable
{
private const string DetectContactsFollowBackStr = "DETECTCONTACTSFOLLOWBACK";
private const string DetectContactsUnfollowBackStr = "DETECTCONTACTSUNFOLLOWBACK";
private const string DetectExploredContactsOnlyStr = "DETECTEXPLORED_CONTACTSONLY";
private const string DetectExploredPhotosOnlyStr = "DETECTEXPLORED_PHOTOSSONLY";
private const string DetectExploredStr = "DETECTEXPLORED";
private const string DetectRecentContactPhotosStr = "DETECTRECENTCONTACTPHOTOS";
private const string DoContactsFavStr = "DOCONTACTSFAV";
private const string DoContactsFollowStr = "DOCONTACTSFOLLOW";
private const string DoContactsUnfollowStr = "DOCONTACTSUNFOLLOW";
private const string DoPhotosFavStr = "DOPHOTOSFAV";
private const string PauseStr = "PAUSE";
private const string SaveStr = "SAVE";
private const string SearchKeywordsContactsOnlyStr = "SEARCHKEYWORDS_CONTACTSONLY";
private const string SearchKeywordsPhotosOnlyStr = "SEARCHKEYWORDS_PHOTOSSONLY";
private const string SearchKeywordsStr = "SEARCHKEYWORDS";
private const string WaitStr = "WAIT";
private static readonly string ExecPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
private readonly ILogger Log;
public FollowerBot(string[] configArgs, ILogger logger)
{
Log = logger;
Log.LogInformation("## LOADING...");
LoadConfig(configArgs);
LoadData();
string w = PseudoRand.Next(Config.SeleniumWindowMinW, Config.SeleniumWindowMaxW).ToString(CultureInfo.InvariantCulture);
string h = PseudoRand.Next(Config.SeleniumWindowMinH, Config.SeleniumWindowMaxH).ToString(CultureInfo.InvariantCulture);
if (string.IsNullOrWhiteSpace(Config.SeleniumRemoteServer))
{
Log.LogDebug("NewChromeSeleniumWrapper({0}, {1}, {2})", ExecPath, w, h);
Selenium = SeleniumWrapper.NewChromeSeleniumWrapper(ExecPath, w, h, Config.SeleniumBrowserArguments, Config.BotSeleniumTimeoutSec);
}
else
{
if (Config.SeleniumRemoteServerWarmUpWaitMs > 0)
{
Task.Delay(Config.SeleniumRemoteServerWarmUpWaitMs)
.Wait();
}
Log.LogDebug("NewRemoteSeleniumWrapper({0}, {1}, {2})", Config.SeleniumRemoteServer, w, h);
Selenium = SeleniumWrapper.NewRemoteSeleniumWrapper(Config.SeleniumRemoteServer, w, h, Config.SeleniumBrowserArguments, Config.BotSeleniumTimeoutSec);
}
}
public void Run()
{
Log.LogInformation("## LOGGING...");
if (Data.UserContactUrl == null
|| !TryAuthCookies())
{
AuthLogin();
}
Log.LogInformation("Logged user : {0}", Data.UserContactUrl);
PostAuthInit();
SaveData(); // save cookies at last
Log.LogInformation("## RUNNING...");
foreach (string curTask in GetTasks(Config.BotTasks, Config.BotSaveAfterEachAction, Config.BotSaveOnEnd, Config.BotSaveOnLoop, Config.BotLoopTaskLimit))
{
Log.LogInformation("# {0}...", curTask);
switch (curTask)
{
case DetectContactsFollowBackStr:
DetectContactsFollowBack();
break;
case DetectExploredStr:
DetectExplored();
break;
case DetectExploredContactsOnlyStr:
DetectExplored(true, false);
break;
case DetectExploredPhotosOnlyStr:
DetectExplored(false, true);
break;
case SearchKeywordsStr:
SearchKeywords();
break;
case SearchKeywordsContactsOnlyStr:
SearchKeywords(true, false);
break;
case SearchKeywordsPhotosOnlyStr:
SearchKeywords(false, true);
break;
case DoContactsFollowStr:
DoContactsFollow();
break;
case DoContactsFavStr:
DoContactsFav();
break;
case DoPhotosFavStr:
DoPhotosFav();
break;
case DetectContactsUnfollowBackStr:
DetectContactsUnfollowBack();
break;
case DoContactsUnfollowStr:
DoContactsUnfollow();
break;
case DetectRecentContactPhotosStr:
DetectRecentContactPhotos();
break;
case SaveStr:
SaveData();
break;
case PauseStr:
case WaitStr:
Task.Delay(PseudoRand.Next(Config.BotWaitTaskMinWaitMs, Config.BotWaitTaskMaxWaitMs))
.Wait();
break;
default:
Log.LogError("Unknown BotTask : {0}", curTask);
break;
}
}
Log.LogInformation("## ENDED OK");
}
internal void DebugDump()
{
StringBuilder dump = new StringBuilder();
try
{
dump.AppendFormat("# Try Dump last page : {0} @ {1}\r\n", Selenium.Title, Selenium.Url);
dump.Append(Selenium.CurrentPageSource); // this one may crash more probably
}
catch
{
// Not usefull because already in exception
}
finally
{
try
{
Log.LogDebug("# Try to dumping last page : {0} @ {1}\r\n", Selenium.Title, Selenium.Url);
Selenium.DumpCurrentPage(Config.BotUserSaveFolder ?? ExecPath, Config.BotUserEmail);
}
catch
{
// Not usefull because already in exception context
}
}
}
}
}