-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.cs
266 lines (242 loc) · 8.9 KB
/
Model.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
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Web;
using System.Net;
using System.ComponentModel;
//using Microsoft.VisualBasic.Devices;
namespace test_task_3
{
public class Model
{
// ãëîáàëüíàÿ ïåðåìåííàÿ êîòîðàÿ õðàíèò ïîñëåäíèé èçìåí¸ííûé ñïèñîê ñàéòîâ â ñòðîêîâ òèïå
public List<string> ViewListBoxText;
// ãëîáàëüíàÿ ïåðåìåííàÿ êîòîðàÿ õðàíèò ïîñëåäíèé èçìåí¸ííûé ñïèñîê ñàéòîâ â òèïå êëàññà sites
public List<Sites> sitesToListbox;
public bool not_changed = true; // ïåðåìåííàÿ äëÿ õðàíåíèÿ ñòàòóñà èçìåíåíèÿ ôàéëà
public BackgroundWorker bw;
public List<String> update_sites_list() // ìåòîä ïîëó÷åíèÿ ñïèñêà ñàéòîâ â ñòðîêîâîì ôîðìàòå èç ôàéëà
{
List<String> lstSites = new List<String>();
int count = 1;
String line;
try
{
StreamReader sr = new StreamReader("files/sites.txt");
line = sr.ReadLine();
while (line != null)
{
string phrase = line;
bool siteCheck;
string[] words = phrase.Split(' ');
bool delay_parse = int.TryParse(words[2], out int delayint);
if (!delay_parse)
{
delayint = 60;
}
if (words[1].StartsWith("http://") || words[1].StartsWith("https://"))
{
siteCheck = testSite(words[1]);
} else
{
siteCheck = false;
}
lstSites.Add("id: " + count.ToString() + " | Íàèìåíîâàíèå: " + words[0] + " | Äîñòóïíîñòü: " + siteCheck.ToString() + " | Èíòåðâàë ïðîâåðêè: " + words[2] + " | URL: " + words[1]);
line = sr.ReadLine();
count++;
}
sr.Close();
return lstSites;
}
catch
{
MessageBox.Show("Îøèáêà c îáíîâëåíèåì äàííûõ èç òåêñòîâîãî ôàéëà");
return lstSites;
}
}
public List<Sites> sites_list() // ìåòîä ïîëó÷åíèÿ ñïèñêà ñàéòîâ â ôîìàòå êëàññà sites èç ôàéëà
{
List<Sites> model_sites = new List<Sites>();
Sites St = new Sites();
int count = 1;
String line, name, url;
int delayed;
bool checks;
try
{
StreamReader sr = new StreamReader("files/sites.txt");
line = sr.ReadLine();
while (line != null)
{
string phrase = line;
string[] words = phrase.Split(' ');
name = words[0];
url = words[1];//----------------------------------------
bool delay_parse = int.TryParse(words[2], out int delayint);
if (delay_parse)
{
delayed = delayint; //----------------------------------------
}
else
{
delayed = 60; //----------------------------------------
}
if (words[1].StartsWith("http://") || words[1].StartsWith("https://"))
{
checks = testSite(url); //----------------------------------------
}
else
{
checks = false; //----------------------------------------
}
//model_sites.Add(St);
model_sites.Add(new Sites() { siteID = count, siteName = name, siteURL = url, siteDelay = delayed, siteCheck = checks});
//parts.Add(new Part() { PartName = "crank arm", PartId = 1234 });
line = sr.ReadLine();
count++;
}
sr.Close();
return model_sites;
}
catch
{
MessageBox.Show("Îøèáêà c îáíîâëåíèåì äàííûõ èç òåêñòîâîãî ôàéëà");
return model_sites;
}
}
public bool testSite(string url) // ìåòîä äëÿ ïîâåðêè äîñòóïíîñòè ñàéòà
{
Uri uri = new Uri(url);
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch
{
return false;
}
return true;
}
public void delete_site(List<Sites> sites, int index) // ìåòîä äëÿ óäàëåíèÿ ñàéòà èç òåêñòîâîãî ôàéëà
{
try
{
List<string> pastFile = read_file();
pastFile.RemoveAt(index);
System.IO.File.WriteAllLines("files/sites.txt", pastFile);
MessageBox.Show("Óäàëåíî");
}
catch
{
MessageBox.Show("Îøèáêà óäàëåíèÿ èç ôàéëà");
}
}
public bool add_site(string name, string url, string delay) // ìåòîä äîáàâëåíèÿ ñàéòà â òåêñòîâûé ôàéë
{
try
{
List<string> pastFile = read_file();
bool parsed = int.TryParse(delay, out int delayParse);
if (parsed)
{
if (delayParse > 30000 || delayParse <= 9)
{
delay = "60";
} else
{
delay = delayParse.ToString();
}
} else delay = "60";
if (url != "")
{
if (!(url.StartsWith("http://") || url.StartsWith("https://") || url == null))
{
url = "https://google.com";
}
} else
{
url = "https://google.com";
}
pastFile.Add(name + " " + url + " " + delay);
System.IO.File.WriteAllLines("files/sites.txt", pastFile);
return true;
}
catch
{
MessageBox.Show("Îøèáêà çàïèñè â ôàéë");
return false;
}
}
List<string> read_file() { // ìåòîä äëÿ ïîëó÷åíèÿ ñïèñêà ñàéòîâ â ñòðîêîâîì òèïå
List<String> lstSites = new List<String>();
String line;
try
{
StreamReader sr = new StreamReader("files/sites.txt");
line = sr.ReadLine();
while (line != null)
{
lstSites.Add(line);
line = sr.ReadLine();
}
sr.Close();
return lstSites;
}
catch
{
MessageBox.Show("Îøèáêà c ÷òåíèåì äàííûõ èç òåêñòîâîãî ôàéëà");
return lstSites;
}
}
// ìåòîä èçìåíåíèÿ ñàéòà â òåêñòîâîì ôàéëå
public bool update_site(List<Sites> sites, int index, string name, string url, string delay)
{
try
{
List<string> pastFile = read_file();
Sites site = sites.ElementAt(index);
if (delay != "")
{
bool parsed = int.TryParse(delay, out int delayParse);
if (parsed)
{
if (delayParse > 30000 || delayParse <= 9)
{
delay = "60";
}
else delay = delayParse.ToString();
}
else delay = "60";
}
else delay = site.siteDelay.ToString();
if (url != "")
{
if (!(url.StartsWith("http://") || url.StartsWith("https://") || url == null))
{
url = "https://google.com";
}
} else
{
url = site.siteURL.ToString();
}
if (name == "")
{
name = site.siteName;
}
pastFile[index] = name + " " + url + " " + delay;
System.IO.File.WriteAllLines("files/sites.txt", pastFile);
return true;
}
catch
{
MessageBox.Show("Îøèáêà çàïèñè â ôàéë");
return false;
}
}
}
}