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
/
FollowerBot.Data.cs
203 lines (186 loc) · 8.51 KB
/
FollowerBot.Data.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
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace FlickrFollowerBot
{
public partial class FollowerBot
{
private class PersistenceData
{
public string UserContactUrl = null;
/// <summary>
/// List of URL of the contact'photos
/// </summary>
public Queue<string> ContactsToFav = new Queue<string>();
/// <summary>
/// List of URL of the contact'photos
/// </summary>
public Queue<string> ContactsToFollow = new Queue<string>();
/// <summary>
/// List of URL of the contact'photos
/// </summary>
public Queue<string> PhotosToFav = new Queue<string>();
/// <summary>
/// List of URL of the contact'photos
/// </summary>
public Queue<string> ContactsToUnfollow = new Queue<string>();
/// <summary>
/// Logon Cookies date
/// </summary>
public Nullable<DateTime> CookiesInitDate = null;
public IEnumerable<object> Cookies = new List<object>();
public IDictionary<string, string> SessionStorage = new Dictionary<string, string>();
public IDictionary<string, string> LocalStorage = new Dictionary<string, string>();
/// <summary>
/// Last refresh date
/// </summary>
public Nullable<DateTime> MyContactsUpdate = null;
/// <summary>
/// List of URL of the contact'photos
/// </summary>
public HashSet<string> MyContacts = new HashSet<string>();
/// <summary>
/// List of URL of the contact'photos
/// </summary>
public HashSet<string> MyContactsBanned = new HashSet<string>();
}
/// <summary>
/// Not banned if don t follow back
/// </summary>
private readonly HashSet<string> MyContactsInTryout = new HashSet<string>();
private string JsonPath;
private PersistenceData Data;
private void LoadData()
{
Data = new PersistenceData();
if (Config.BotUsePersistence)
{
if (!string.IsNullOrWhiteSpace(Config.BotUserSaveFolder))
{
JsonPath = Config.BotUserSaveFolder;
if (!JsonPath.EndsWith(Path.DirectorySeparatorChar))
{
JsonPath += Path.DirectorySeparatorChar;
}
if (!Directory.Exists(Config.BotUserSaveFolder))
{
Log.LogDebug("Create session directory {0}", JsonPath);
try
{
Directory.CreateDirectory(JsonPath);
}
catch (Exception ex)
{
Log.LogError(default, ex, "Coundn't create {0} directory, using current.", JsonPath);
JsonPath = ExecPath + Path.DirectorySeparatorChar + "PersistenceData_";
}
}
}
else
{
JsonPath = ExecPath + Path.DirectorySeparatorChar + "PersistenceData_";
}
string fn = JsonPath + Config.BotUserEmail + ".json";
if (File.Exists(fn))
{
PersistenceData tmp = JsonConvert.DeserializeObject<PersistenceData>(File.ReadAllText(fn, Encoding.UTF8));
Data.CookiesInitDate = tmp.CookiesInitDate ?? tmp.MyContactsUpdate ?? DateTime.UtcNow; // manage cache from previous version
if (Config.BotUsePersistenceLimitHours > 0 && DateTime.UtcNow < Data.CookiesInitDate.Value.AddHours(Config.BotUsePersistenceLimitHours))
{
Log.LogDebug("LOADING USER JSON");
Data.UserContactUrl = tmp.UserContactUrl;
if (Config.BotCacheMyContacts)
{
Data.MyContactsUpdate = tmp.MyContactsUpdate;
if (tmp.MyContacts != null)
{
Data.MyContacts = tmp.MyContacts;
Log.LogDebug("$MyContacts #{0}", Data.MyContacts.Count);
}
if (tmp.MyContactsBanned != null)
{
Data.MyContactsBanned = tmp.MyContactsBanned;
Log.LogDebug("$MyContactsBanned #{0}", Data.MyContactsBanned.Count);
}
}
if (tmp.ContactsToFollow != null)
{
Data.ContactsToFollow = new Queue<string>(tmp.ContactsToFollow
.Except(Data.MyContacts).Except(Data.MyContactsBanned)); // some contacts may have been already added manualy
Log.LogDebug("$ContactsToFollow #{0}", Data.ContactsToFollow.Count);
}
if (tmp.ContactsToFav != null)
{
Data.ContactsToFav = new Queue<string>(tmp.ContactsToFav
.Except(Data.MyContacts).Except(Data.MyContactsBanned)); // some contacts may have been already added manualy
Log.LogDebug("$ContactsToFav #{0}", Data.ContactsToFav.Count);
}
if (tmp.ContactsToUnfollow != null)
{
Data.ContactsToUnfollow = tmp.ContactsToUnfollow;
Log.LogDebug("$ContactsToUnfollow #{0}", Data.ContactsToUnfollow.Count);
}
if (tmp.PhotosToFav != null)
{
Data.PhotosToFav = tmp.PhotosToFav;
Log.LogDebug("$PhotosToFav #{0}", Data.PhotosToFav.Count);
}
if (tmp.Cookies != null)
{
Data.Cookies = tmp.Cookies;
Log.LogDebug("Cookies : {0}", Data.Cookies.Count());
}
if (tmp.SessionStorage != null)
{
Data.SessionStorage = tmp.SessionStorage;
Log.LogDebug("SessionStorage : {0}", Data.SessionStorage.Count);
}
if (tmp.LocalStorage != null)
{
Data.LocalStorage = tmp.LocalStorage;
Log.LogDebug("LocalStorage : {0}", Data.LocalStorage.Count);
}
}
else
{
Log.LogWarning("Persistence limit reached, starting a new session");
}
}
else
{
Log.LogDebug("No existing session to load : {0}", fn);
}
}
}
private void SaveData()
{
if (Config.BotUsePersistence)
{
Log.LogDebug("SAVING USER JSON");
PersistenceData tmp = new PersistenceData()
{
UserContactUrl = Data.UserContactUrl,
ContactsToFollow = Data.ContactsToFollow,
ContactsToFav = Data.ContactsToFav,
ContactsToUnfollow = Data.ContactsToUnfollow,
PhotosToFav = Data.PhotosToFav,
CookiesInitDate = Data.CookiesInitDate,
Cookies = Selenium.Cookies,
SessionStorage = Selenium.SessionStorage,
LocalStorage = Selenium.LocalStorage
};
if (Config.BotCacheMyContacts)
{
tmp.MyContactsUpdate = Data.MyContactsUpdate;
tmp.MyContacts = Data.MyContacts;
tmp.MyContactsBanned = Data.MyContactsBanned;
}
File.WriteAllText(JsonPath + Config.BotUserEmail + ".json", JsonConvert.SerializeObject(tmp, Formatting.Indented), Encoding.UTF8);
}
}
}
}