-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
333 lines (277 loc) · 12.3 KB
/
Form1.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
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
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
// private
private ChromeDriver ChDriver;
public Form1()
{
InitializeComponent();
}
public List<cNews> UnitePosts(string fileName, List<cNews> parcedPosts)
{
if (!File.Exists(fileName))
{
return parcedPosts;
}
List<cNews> writtenPosts = new List<cNews>();
string serializedWrittenPosts = File.ReadAllText(fileName);
writtenPosts = JsonConvert.DeserializeObject<List<cNews>>(serializedWrittenPosts);
return parcedPosts.Union(writtenPosts, new CNewsEqualityComparer()).ToList();
}
public List<IWebElement> FindIWIWebElementByClassName(IWebElement element, string className)
{
return (from item in element.FindElements(By.ClassName(className)) where item.Displayed && item.Enabled select item).ToList();
}
private void WriteToJson(String fileName, byte mod, List<cNews> news, ref FileOperation fileOperation)
{
bool lockTaken = false;
try
{
Monitor.TryEnter(fileOperation, -1, ref lockTaken);
if (lockTaken)
{
while (fileOperation.IsOpen())
{
Thread.Sleep(2000);
}
// fileIsOpen = true;
fileOperation.CreateLockMarker();
List<cNews> toBeWritten = UnitePosts(fileName, news);
Action breakThisShittyCode = () =>
{
FileStream forBreakingGoals = File.Open(fileName, FileMode.Append);
Thread.Sleep(3000);
forBreakingGoals.Close();
};
Action action;
switch (mod)
{
case 1:
//var aCText = new { id = "" , text = ""};
//var aCTextList = (new[] { aCText }).ToList();
//foreach (cNews item in news)
//{
// aCTextList.Add(new { id = item.id, text = item.text });
//}
//aCTextList.Remove(new { id = "", text = "" });
//File.AppendAllText(fileName, JsonConvert.SerializeObject(aCTextList, Formatting.Indented));
breakThisShittyCode();
File.WriteAllText(fileName, JsonConvert.SerializeObject(toBeWritten.Select(cNews => new { cNews.Id, cNews.Text }), Formatting.Indented));
action = () => { textBox4.Text = Convert.ToString(toBeWritten.Count); progressBar1.Value += 30; };
if (textBox1.InvokeRequired || progressBar1.InvokeRequired)
Invoke(action);
else
action();
break;
case 2:
breakThisShittyCode();
File.WriteAllText(fileName, JsonConvert.SerializeObject(toBeWritten.Select(cNews => new { cNews.Id, cNews.Link }), Formatting.Indented));
//File.AppendAllText(fileName, new JavaScriptSerializer().Serialize(news.Select(cNews => new { cNews.id, cNews.link })));
action = () => { progressBar1.Value += 30; };
if (textBox1.InvokeRequired || progressBar1.InvokeRequired)
Invoke(action);
else
action();
break;
case 3:
breakThisShittyCode();
File.WriteAllText(fileName, JsonConvert.SerializeObject(toBeWritten.Select(cNews => new { cNews.Id, cNews.ImageLink }), Formatting.Indented));
action = () => { progressBar1.Value += 30; };
if (textBox1.InvokeRequired || progressBar1.InvokeRequired)
Invoke(action);
else
action();
break;
default:
break;
}
//fileIsOpen = false;
fileOperation.DeleteLockMarker();
}
else
{
Action debug = () => MessageBox.Show("something went wrong");
if (InvokeRequired)
Invoke(debug);
else
debug();
}
}
finally
{
if (lockTaken)
{
Monitor.Exit(fileOperation);
}
}
}
/*ref bool[] fileIsOpen,*/
private void ReadFromJson(string[] fileName, ref FileOperation[] fileOperations)
{
// Thread.Sleep(3000);
TextBox[] textBoxes = new TextBox[3] { textBox1, textBox2, textBox3 };
bool[] fileIsReaded = new bool[3] { false, false, false };
bool lockTaken;
while (!(fileIsReaded[0] && fileIsReaded[1] && fileIsReaded[2]))
{
for (int i = 0; i < 3; i++)
{
//Thread.Sleep(100);
lockTaken = false;
try
{
Monitor.TryEnter(fileOperations[i], 100, ref lockTaken);
if (fileOperations[i].IsOpen() || !lockTaken)
continue;
//fileIsOpen[i] = true;
fileOperations[i].CreateLockMarker();
Action action = () => textBoxes[i].Text = File.ReadAllText(fileName[i]);
if (InvokeRequired)
{
Invoke(action);
fileIsReaded[i] = true;
}
else
action();
// fileIsOpen[i] = false;
fileOperations[i].DeleteLockMarker();
}
finally
{
if (lockTaken)
{
Monitor.Exit(fileOperations[i]);
}
}
}
Thread.Sleep(3000);
}
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
List<cNews> news;
news = new List<cNews>();
foreach (var elem in ChDriver.FindElements(By.ClassName("wall_post_cont")))
{
cNews toBeAddedObject = new cNews();
List<IWebElement> postTexts = FindIWIWebElementByClassName(elem, "wall_post_text");
List<IWebElement> imagLinks = FindIWIWebElementByClassName(elem, "page_post_sized_thumbs");
toBeAddedObject.Id = elem.GetAttribute("id");
if (postTexts.Any())
{
toBeAddedObject.Text = postTexts[0].Text;
//foreach (IWebElement link in postTexts[0].FindElements(By.TagName("a")))
//{
// if (string.IsNullOrEmpty(link.GetAttribute("href")))
// continue;
// toBeAddedObject.link.Add(link.GetAttribute("href"));//linq
//}
toBeAddedObject.Link.AddRange((from link in postTexts[0].FindElements(By.TagName("a"))
where !string.IsNullOrEmpty(link.GetAttribute("href"))
select link.GetAttribute("href")));
}
if (imagLinks.Any())
{
foreach (IWebElement iml in imagLinks[0].FindElements(By.TagName("a")))
{
string temp = iml.GetCssValue("background-image");
if (!string.IsNullOrEmpty(temp) && temp.Length > 7)
{
temp = temp.Substring(5);
temp = temp.Remove(temp.Length - 2);
toBeAddedObject.ImageLink.Add(temp);
}
}
}
if (toBeAddedObject.ImageLink.Any() || toBeAddedObject.Link.Any() || !string.IsNullOrEmpty(toBeAddedObject.Text))
{
news.Add(toBeAddedObject);
}
}
progressBar1.Value = 5;
// bool[] fileIsBusy = new bool[3] { false, false, false };
string[] fileName = new string[3] { "JStext.txt", "JSlink.txt", "JSimageLink.txt" };
for (int i = 0; i < 3; i++)
{
if (FileOperation.IsOpen(fileName[i]))
FileOperation.DeleteLockMarker(fileName[i]);
}
// writeToJson(fileName[0], 1, news, ref fileIsBusy[0]);
FileOperation[] fileOperations = new FileOperation[3] {
new FileOperation(fileName[0]),
new FileOperation(fileName[1]),
new FileOperation(fileName[2]), };
Task[] tasks = new Task[4]
{
new Task(() => WriteToJson(fileName[0], 1, news, ref fileOperations[0])),
new Task(() => WriteToJson(fileName[1], 2, news, ref fileOperations[1])),
new Task(() => WriteToJson(fileName[2], 3, news, ref fileOperations[2])),
new Task(() => ReadFromJson(fileName, ref fileOperations)),
};
//Task.Factory.StartNew(() => writeToJson("JStext.txt", 1, news,));
//Task.Factory.StartNew(() => writeToJson("JSlink.txt", 2, news,));
//Task.Factory.StartNew(() => writeToJson("JSimageLink.txt", 3, news,));
for (int i = 0; i < 4; i++)
{
tasks[i].Start();
}
progressBar1.Value += 5;
}
public delegate IWebElement findElements(string by);
private void button2_Click(object sender, EventArgs e)
{
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disk-cache-size=1", "--incognito");
ChDriver = new ChromeDriver(options);
ChDriver.Navigate().GoToUrl("https://vk.com/feed");
Thread.Sleep(1500);
findElements findElementById = id => ChDriver.FindElement(By.Id(id));
string mail = "";
string pass = "";
findElementById("email").SendKeys(mail);
findElementById("pass").SendKeys(pass);
// findElements findElementClick = id => ChDriver.FindElement(By.Id(id)).Click();
findElementById("login_button").Click();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
int iterator;
if (Int32.TryParse(maxPosts.Text, out iterator))
{
IJavaScriptExecutor js = ChDriver as IJavaScriptExecutor;
for (int i = 0; i < iterator; i++)
{
Thread.Sleep(100);
js.ExecuteScript("feed.showMore(10)");
}
maxPosts.Text = "Done";
}
else
{
maxPosts.Text = "Error";
}
}
}
}
//deserializedPosts = JsonConvert.DeserializeObject<List<C_Post>>(fileText);