-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadUnit.pas
More file actions
198 lines (154 loc) · 5.01 KB
/
Copy pathThreadUnit.pas
File metadata and controls
198 lines (154 loc) · 5.01 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
unit ThreadUnit;
interface
uses Windows, SysUtils, Classes, ActiveX, ComObj, Variants, SynPdf, Jpeg,
StrUtils;
type
// Çäåñü íåîáõîäèìî îïèñàòü êëàññ TMyThread:
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;
var
MyThread: TMyThread;
implementation
uses Unit2;
procedure DownMeth;
var
MS: TMemoryStream;
i: Integer;
lPdf: TPdfDocument;
lPage: TPdfPage;
Jpegimage: TJPEGImage;
pdfimage: TPdfImage;
f: TextFile;
begin
lPdf := TPdfDocument.Create; // ñîçäàëè äîêóìåíò
lPdf.Info.Author := 'User'; // çàäàëè àâòîðà
lPdf.Info.CreationDate := Now; // çàäàëè äàòó ñîçäàíèÿ äîêóìåíòà
lPdf.DefaultPaperSize := psA4; // óêàçàëè ôîðìàò ñòðàíèö (À4)
Form1.ProgressBar1.Min := 0;
Form1.ProgressBar1.Max := PageNum;
Form1.Memo1.Lines.Add('Downloading process have been started...');
for i := 0 to PageNum - 1 do
begin
MS := TMemoryStream.Create;
Jpegimage := TJPEGImage.Create;
lPage := lPdf.AddPage;
try
try
(Form1.IdHTTP1.Get
('http://elibrary.misis.ru/plugins/SecView/getDoc.php?id=' +
IntToStr(idNum) + '&page=' + IntToStr(i) + '&type=small/fast', MS));
except
Form1.Memo1.Lines.Add('ERROR!!!!');
end;
Form1.Memo1.Lines.Add('Downloaded page:' + IntToStr(i));
MS.Position := 0; // Äëÿ òîãî, ÷òîáû ïîçèöèÿ ÷èòàëàñü ñ 0
Jpegimage.LoadFromStream(MS);
pdfimage := TPdfImage.Create(lPdf, Jpegimage, true);
lPdf.AddXObject(AnsiString(IntToStr(i)), pdfimage);
lPdf.Canvas.DrawXObject(0, lPage.PageHeight - Jpegimage.Height,
Jpegimage.Width, Jpegimage.Height, AnsiString(IntToStr(i)));
Form1.ProgressBar1.Position := i;
finally
MS.Free;
Jpegimage.Free;
end;
end;
try
lPdf.SaveToFile('C:\_Books\' + Form1.BookName.Text + '.pdf');
Form1.ProgressBar1.Position := 0;
PostMessage(Form1.Handle, cmRxByte, 4, 0); // Download compleated
except
PostMessage(Form1.Handle, cmRxByte, 5, 0); // Error
end;
isDownload := false;
MyThread.DoTerminate;
// Çäåñü îïèñûâàåòñÿ êîä, êîòîðûé áóäåò âûïîëíÿòüñÿ â ïîòîêå
end;
procedure AuthMeth;
var
html: String;
stringList : TStringList;
begin
Form1.Memo1.Lines.Add('Waiting for authentication...');
Form1.IdHTTP1.Request.AcceptCharSet := 'windows-1251';
try
html := (Form1.IdHTTP1.Get('http://elibrary.misis.ru/login.php' +
'?action=login&cookieverify=&redirect='
+ '&username=' + login + '&password=' + newPassword + '&language=ru_UN'));
if Pos('Çàïðîñ âûïîëíåí çà', html) <> 0 then
PostMessage(Form1.Handle, cmRxByte, 1, 0) // Succesfully
else
PostMessage(Form1.Handle, cmRxByte, 2, 0) // Check data
except
PostMessage(Form1.Handle, cmRxByte, 3, 0) // Connection error
end;
isAuthorization := false;
MyThread.DoTerminate;
end;
procedure GetMeth;
var
idStartPos: LongInt;
idStopPos: LongInt;
pageStartPos: LongInt;
pageStopPos: LongInt;
titleStartPos: LongInt;
titleStopPos: LongInt;
html: String;
begin
Form1.Memo1.Lines.Add('Waiting for book...');
if (Form1.LinkEd.Text <> '') then
begin
try
html := (Form1.IdHTTP1.Get(Form1.LinkEd.Text));
except
PostMessage(Form1.Handle, cmRxByte, 6, 0) // Link Error
end;
end;
if html <> '' then
begin
// ===================================Ïîèñê àéäè====================================
idStartPos := Pos('plugins/SecView/getDoc.php?id=', html) + 30;
// Ïîèñê id â html-êå
idStopPos := PosEx(char(39), html, idStartPos);
// Char(39) - çíàê îäèíàðíîé êàâû÷êè
idNum := StrToInt(Copy(html, idStartPos, idStopPos - idStartPos));
// ====================================Ïîèñê êîëè÷åñòâà ñòðàíèö=====================
pageStartPos := Pos(char(39) + 'PageCount' + char(39) + ':' + char(39),
html) + 13; // Ïîèñê id â html-êå
pageStopPos := PosEx(char(39), html, pageStartPos);
// Char(39) - çíàê îäèíàðíîé êàâû÷êè
PageNum := StrToInt((Copy(html, pageStartPos, pageStopPos - pageStartPos)));
// =====================================Ïîèñê íàçâàíèÿ===============================
titleStartPos := Pos('—', html) + 8;
title := ((Copy(html, titleStartPos, 10)));
while Pos('.', title) <> 0 do
begin
delete(title, Pos('.', title), 1);
end;
while Pos(' ', title) <> 0 do
begin
delete(title, Pos(' ', title), 1);
end;
Form1.BookName.Text := title;
PostMessage(Form1.Handle, cmRxByte, 7, 0) // Get compleated
end
else
PostMessage(Form1.Handle, cmRxByte, 6, 0); // Get Error
isGet := false;
MyThread.DoTerminate;
end;
// Íóæíî ñîçäàòü ïðîöåäóðó Execute, óæå îïèñàííóþ â êëàññå TMyThread
procedure TMyThread.Execute;
begin
if isDownload = true then
DownMeth;
if isAuthorization = true then
AuthMeth;
if isGet = true then
GetMeth;
end;
end.