-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.pas
314 lines (283 loc) · 7.86 KB
/
MainForm.pas
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
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms, Graphics,
Dialogs, RegularExpressionsCore, INIFiles, shellapi, Thread, StrUtils,
ComCtrls, Buttons, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP, Vcl.ExtCtrls;
type
TFormMain = class(TForm)
mmo1: TMemo;
pb1: TProgressBar;
btnStart: TBitBtn;
btnStop: TBitBtn;
btnReset: TBitBtn;
lbl1: TLabel;
lbl2: TLabel;
RGCities: TRadioGroup;
procedure ControlWindow(var Msg: TMessage); message WM_SYSCOMMAND;
procedure IconMouse(var Msg: TMessage); message WM_USER + 1;
procedure Ic(n: Integer; Icon: TIcon);
procedure OnMinimizeProc(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnStartClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
procedure btnResetClick(Sender: TObject);
private
Thread: TSyncThread;
procedure LogFromMain(Str: String);
public
years: TStringList;
runs: TStringList;
towns: TStringList;
last: Integer;
lastpost: string;
dirname: string;
delay: Integer;
finish: string;
tmpfile: string;
csvfile: string;
proxy: string;
port: Integer;
sep: string;
tip: string;
function explode(const delim, str: string): TStringList;
procedure State(St:integer);
procedure GoingErrorState(isDelete:boolean);
end;
var
FormMain: TFormMain;
implementation
{$R *.dfm}
{ TForm1 }
procedure TFormMain.FormCreate(Sender: TObject);
var
cfg: TIniFile;
i: Integer;
str: string;
Regex: TPerlRegEx;
RegionPrefix:string;
begin
if not FileExists(extractfilepath(paramstr(0)) + 'CONFIG.INI') then
begin
LogFromMain('Ôàéë config.ini íå íàéäåí â òåêóùåé ïàïêå. Ïðîãðàììà çàâåðøåíà');
ShowMessage('Ôàéë config.ini íå íàéäåí â òåêóùåé ïàïêå. Ïðîãðàììà çàâåðøåíà');
halt;
end;
cfg := TIniFile.Create(extractfilepath(paramstr(0)) + 'CONFIG.INI');
years := TStringList.Create();
runs := TStringList.Create();
towns := TStringList.Create();
Regex := TPerlRegEx.Create();
cfg.ReadSection('run', years);
for i := 0 to years.Count - 1 do runs.Add(cfg.ReadString('run', years[i], ''));
str := cfg.ReadString('config', 'towns', '');
Regex.Regex := ',';
Regex.Subject := str;
Regex.Split(towns, MAXINT);
for i := 0 to towns.Count - 1 do towns[i] := Trim(towns[i]);
last := StrToInt(cfg.ReadString('config', 'last', ''));
if (last < 1) then last := 1;
if (last > 3) then last := 7;
FormMain.mmo1.Lines.Add('Ñîáèðàåì îáúÿâëåíèÿ çà äèàïàçîí '+IntToStr(last)+' äíÿ');
lastpost := cfg.ReadString('config', 'lastpost', '');
dirname := cfg.ReadString('config', 'dirname', '');
sep := cfg.ReadString('config', 'sep', '');
finish := cfg.ReadString('config', 'finish', '');
delay := StrToInt(cfg.ReadString('http', 'delay', ''));
proxy := cfg.ReadString('http', 'proxy', '');
port := StrToInt(cfg.ReadString('http', 'port', ''));
if FormMain.RGCities.ItemIndex=0 then RegionPrefix:='MOS_';
if FormMain.RGCities.ItemIndex=1 then RegionPrefix:='TV_';
TmpFile := GetCurrentDir() + '\' + FormMain.dirname + '\'+RegionPrefix+ FormatDateTime('yyyy-mm-dd', Now) + IntToStr(last) + '.txt';
CsvFile := GetCurrentDir() + '\' + FormMain.dirname + '\'+RegionPrefix+ FormatDateTime('yyyy-mm-dd', Now) + IntToStr(last) + '.csv';
tip := '';
FreeAndNil(cfg);
FreeAndNil(Regex);
State(1);//Ïåðåõîäèì â ñîñòîÿíèå Íà÷àëî
if FileExists(tmpfile) then
begin
FormMain.mmo1.Lines.Add('Òåëåôîíû óæå ñîáèðàëèñü. ' + #13#10 + 'Íàæìèòå êíîïêó [Ñòàðò] äëÿ ïðîäîëæåíèÿ' + #13#10+ '[Çàíîâî] äëÿ ñáîðà ñíà÷àëà.');
LogFromMain('Ïðîäîëæåíèå ñáîðà ïîñëå âûõîäà èç ïðîãðàììû');
//btnStart.Caption := 'Ñòàðò';
//btnReset.Enabled := true;
end;
end;
procedure TFormMain.GoingErrorState(isDelete:boolean);
begin
if isDelete and FileExists(tmpFile) then DeleteFile(tmpFile);
State(4);
end;
procedure TFormMain.btnStartClick(Sender: TObject);
begin
State(2); //Ïåðåõîäèì â ñîñòîÿíèå Ðàáîòà
// btnStart.Enabled := false;
// btnStop.Enabled := true;
// btnReset.Enabled := false;
lbl2.Caption:='Îáúÿâëåíèÿ êëàä¸ì â êîðçèíó';
if not Assigned(Thread) then
begin
Thread := TSyncThread.Create(false);
Thread.FreeOnTerminate := True;
Thread.Priority := tpLower;
end
else if Thread.Suspended then
Thread.Resume;
end;
procedure TFormMain.btnStopClick(Sender: TObject);
begin
State(3);//Ïåðåõîäèì â ñîñòîÿíèå Ñòîï
// btnStart.Caption := 'Ñòàðò';
// btnStart.Enabled := true;
// btnStop.Enabled := false;
// btnReset.Enabled := true;
try
if not Thread.Suspended then
Thread.Suspend;
except
LogFromMain('Îøèáêà âî âðåìÿ êîìàíäû Suspend äëÿ ïîòîêà');
end;
end;
procedure TFormMain.btnResetClick(Sender: TObject);
begin
State(2);//Ïåðåõîäèì â ñîñòîÿíèå Ðàáîòà
// btnStart.Caption := 'Ñòàðò';
// btnStart.Enabled := true;
FormMain.pb1.Position := 0;
// btnreset.Enabled := False;
if FileExists(tmpfile) then DeleteFile(tmpfile);
if Assigned(Thread) then
begin
Thread.Terminate;
Thread := nil;
Thread := TSyncThread.Create(false);
Thread.FreeOnTerminate := True;
Thread.Priority := tpLower;
end;
end;
procedure TFormMain.IconMouse(var Msg: TMessage);
var
p: tpoint;
begin
GetCursorPos(p);
case Msg.LParam of
WM_LBUTTONUP, WM_LBUTTONDBLCLK:
begin
Ic(2, Application.Icon);
ShowWindow(Application.Handle, SW_SHOW);
ShowWindow(Handle, SW_SHOW);
Update;
end;
WM_RBUTTONUP:
begin
SetForegroundWindow(Handle);
PostMessage(Handle, WM_NULL, 0, 0);
end;
end;
end;
procedure TFormMain.OnMinimizeProc(Sender: TObject);
begin
PostMessage(Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
end;
procedure TFormMain.State(St: integer);
begin
//Îòðèñîâûâàåì ñîñòîÿíèÿ êíîïîê
// 1 Íà÷àëî. Ïðè ñòàðòå ïðîãðàììû
// 2 Ðàáîòà. Âî âðåìÿ ðàáîòû ïðîãðàììû
// 3 Ñòîï. Íàæàòà êíîïêà Ñòîï.
// 4 Îøèáêà. Ãîòîâèì êíîïêó Çàíîâî
case St of
1:
begin
btnStart.Enabled:=true;
btnStop.Enabled:=false;
btnReset.Enabled:=false;
end;
2:
begin
btnStart.Enabled:=false;
btnStop.Enabled:=true;
btnReset.Enabled:=false;
end;
3:
begin
btnStart.Enabled:=true;
btnStop.Enabled:=false;
btnReset.Enabled:=true;
end;
4:
begin
btnStart.Enabled:=false;
btnStop.Enabled:=false;
btnReset.Enabled:=true;
end;
end;
end;
procedure TFormMain.ControlWindow(var Msg: TMessage);
begin
if Msg.WParam = SC_MINIMIZE then
begin
Ic(1, Application.Icon);
ShowWindow(Handle, SW_HIDE);
ShowWindow(Application.Handle, SW_HIDE);
end
else
inherited;
end;
procedure TFormMain.Ic(n: Integer; Icon: TIcon);
var
Nim: TNotifyIconData;
begin
with Nim do
begin
//cbSize := SizeOf(Nim);
Wnd := Self.Handle;
uID := 123;
uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
hicon := Icon.Handle;
uCallbackMessage := wm_user + 1;
if tip <> '' then
StrLCopy(szTip, PChar(tip), High(szTip))
else
szTip := 'Ïîèñê îáúÿâëåíèé auto.ru';
end;
case n of
1: Shell_NotifyIcon(Nim_Add, @Nim);
2: Shell_NotifyIcon(Nim_Delete, @Nim);
3: Shell_NotifyIcon(Nim_Modify, @Nim);
end;
end;
function TFormMain.explode(const delim, str: String): TStringList;
var
offset: integer;
cur: integer;
dl: integer;
begin
Result := TStringList.Create;
dl := Length(delim);
offset := 1;
while True do
begin
cur := PosEx(delim, str, offset);
if cur > 0 then
Result.Add(Copy(str, offset, cur - offset))
else
begin
Result.Add(Copy(str, offset, Length(str) - offset + 1));
Break
end;
offset := cur + dl;
end;
end;
procedure TFormMain.LogFromMain(Str: String);
var TStmp:String;
LogFile:TextFile;
begin
AssignFile(LogFile,ExtractFilePath(ParamStr(0))+'\'+'log.txt');
if not FileExists(ExtractFilePath(ParamStr(0))+'\'+'log.txt') then Rewrite(LogFile) else Append(LogFile);
TStmp:=FormatDateTime('yyyy-mm-dd hh:mi:ss', Now);
Writeln(LogFile,Tstmp+': '+Str);
//Flush(LogFile);
CloseFile(LogFile);
end;
end.