-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.pas
354 lines (314 loc) · 9.62 KB
/
main.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
unit main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls, LCLIntf,
LCLType, Graphics, synacode, fphttpclient, regexpr,
openssl, opensslsockets, strutils;
type
{ TForm1 }TForm1 = class(TForm)
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
ListBox1: TListBox;
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
function GetLastPageNo(const anHTMLText: string): integer;
procedure ListBox1DrawItem(Control: TWinControl; Index: integer;
ARect: TRect; State: TOwnerDrawState);
private
procedure DoOnWriteStream(Sender: TObject; APosition: int64);
procedure Download(AFrom, ATo: string);
function FormatSize(aSize: int64): string;
function get1webpage(url: string): string;
function getrightfilename(x: string): string;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
type
{ TDownloadStream }
TOnWriteStream = procedure(Sender: TObject; APos: int64) of object;
TDownloadStream = class(TStream)
private
FOnWriteStream: TOnWriteStream;
FStream: TStream;
public
constructor Create(AStream: TStream);
destructor Destroy; override;
function Read(var Buffer; Count: longint): longint; override;
function Write(const Buffer; Count: longint): longint; override;
function Seek(Offset: longint; Origin: word): longint; override;
procedure DoProgress;
published
property OnWriteStream: TOnWriteStream read FOnWriteStream write FOnWriteStream;
end;
{ TForm1 }
{ TDownloadStream }
constructor TDownloadStream.Create(AStream: TStream);
begin
inherited Create;
FStream := AStream;
FStream.Position := 0;
end;
destructor TDownloadStream.Destroy;
begin
FStream.Free;
inherited Destroy;
end;
function TDownloadStream.Read(var Buffer; Count: longint): longint;
begin
Result := FStream.Read(Buffer, Count);
end;
function TDownloadStream.Write(const Buffer; Count: longint): longint;
begin
Result := FStream.Write(Buffer, Count);
DoProgress;
end;
function TDownloadStream.Seek(Offset: longint; Origin: word): longint;
begin
Result := FStream.Seek(Offset, Origin);
end;
procedure TDownloadStream.DoProgress;
begin
// fpercent:= Trunc((FStream.Position) / (FStream.Size) * 100);
if Assigned(FOnWriteStream) then
FOnWriteStream(Self, Self.Position);
end;
{ TForm1 }
procedure TForm1.Download(AFrom, ATo: string);
var
DS: TDownloadStream;
x: string;
FHTTPClient: TFPHTTPClient;
begin
FHTTPClient := TFPHTTPClient.Create(nil);
DS := TDownloadStream.Create(TFileStream.Create(ATo, fmCreate));
FHTTPClient.AllowRedirect := False;
FHTTPClient.AddHeader('User-Agent', 'Wget/1.20.1 (linux-gnu)');
try
DS.FOnWriteStream := @DoOnWriteStream;
try
FHTTPClient.HTTPMethod('GET', AFrom, DS, [302, 200]);
if FHTTPClient.ResponseStatusCode = 302 then
begin
x := FHTTPClient.GetHeader(FHTTPClient.ResponseHeaders, 'Location');
x := StringReplace(x, ' ', '%20', [rfReplaceAll]);
end;
FHTTPClient.HTTPMethod('GET', x, DS, [200]);
except
on E: Exception do
begin
ShowMessage(e.Message);
end;
end;
finally
DS.Free;
FHTTPClient.Free;
end;
end;
function TForm1.FormatSize(aSize: int64): string;
const
KB = 1024;
MB = 1024 * KB;
GB = 1024 * MB;
begin
if aSize < KB then
Result := FormatFloat('#,##0 Bytes', aSize)
else if aSize < MB then
Result := FormatFloat('#,##0.0 KB', aSize / KB)
else if aSize < GB then
Result := FormatFloat('#,##0.0 MB', aSize / MB)
else
Result := FormatFloat('#,##0.0 GB', aSize / GB);
end;
procedure TForm1.DoOnWriteStream(Sender: TObject; APosition: int64);
begin
Label2.Caption := 'Downloading.... ' + FormatSize(APosition);
Application.ProcessMessages;
end;
var
targetDirectory: ansistring;
baseurl, x1: string;
//--------------------------------------------------------------------------------------
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: integer;
ARect: TRect; State: TOwnerDrawState);
var
aColor: TColor; //Background color
begin
if (Index mod 2 = 0) //Index tells which item it is
then
aColor := $ffffff //every second item gets white as the background color
else
aColor := $e1edf6; //every second item gets pink background color
if odSelected in State then
aColor := $6aa8d4; //If item is selected, then red as background color
ListBox1.Canvas.Brush.Color := aColor; //Set background color
ListBox1.Canvas.FillRect(ARect); //Draw a filled rectangle
ListBox1.Canvas.TextRect(ARect, 2, ARect.Top + 2, ListBox1.Items[Index]);
//Draw Itemtext
end;
procedure TForm1.Button2Click(Sender: TObject); //search button
var
page, bookname: ansistring;
re: TRegExpr;
n, n1, sum: integer;
http1: TFPHTTPClient;
label
here, sortlist;
begin
Label2.Caption := 'Searching for your files......';
ListBox1.Items.Clear;
ListBox1.Enabled := False;
form1.Cursor := crHourGlass;
baseurl := 'https://www.pdfdrive.com/search?q=' + EncodeURL(Edit1.Text) +
'&pagecount=&pubyear=&searchin=&more=true';
http1 := TFPHttpClient.Create(nil);
with http1 do
try
http1.AllowRedirect := True;
page := http1.SimpleGet(baseUrl);// Find all book urls
finally
Free;
end;
n1 := GetLastPageNo(page);
sum := 1;
here:
re := TRegExpr.Create('<a href="(.*?)" data-id="(.*?)".*?>');
try
if re.Exec(page) then
begin
while re.ExecNext do
begin
bookname := re.Match[1];
if (RightStr(bookname, 4) = 'html') and (LeftStr(bookname, 1) = '/') then
listbox1.items.add(bookname);
Application.ProcessMessages;
end;
end;
finally
re.Free;
end;
baseurl := '';
page := '';
for n := 1 to n1 do
begin
sum := sum + n;
if sum > n1 then
goto sortlist;
baseurl := ('https://www.pdfdrive.com/search?q=' + EncodeURL(
Edit1.Text) + '&pagecount=&pubyear=&searchin=&page=' + IntToStr(sum));
//page:=Getexplorer(baseurl);
http1 := TFPHttpClient.Create(nil);
with http1 do
try
http1.AllowRedirect := True;
page := http1.SimpleGet(baseUrl);// Find all book urls
finally
Free;
end;
goto here;
end;
sortlist:
Label2.Caption := 'You Got ' + IntToStr(ListBox1.Items.Count) +
' Books Enjoy downloading list below';
ListBox1.Enabled := True;
Form1.Cursor := crDefault;
end;
function TForm1.getrightfilename(x: string): string;
var
xf: string;
ndx, ndx2: integer;
begin
ndx := x.IndexOf('/');
ndx2 := x.IndexOf('-e', ndx);
xf := x.Substring(ndx, ndx2 - ndx);
xf := xf.Replace('-', ' ', [rfReplaceAll]);
Result := xf.Replace('/', ' ', [rfReplaceAll]);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
targetDirectory := GetUserDir + 'downloads' + DirectorySeparator +
'alaa' + DirectorySeparator;
if not DirectoryExists(targetDirectory) then
CreateDir(targetDirectory);
end;
function TForm1.GetLastPageNo(const anHTMLText: string): integer; //new
var
re1: TRegExpr;
begin
Result := -1;
re1 := TRegExpr.Create('&page=(.*?)">');
if re1.Exec(anHTMLText) then
while re1.ExecNext do
if StrToIntDef(re1.Match[1], 0) > Result then
Result := StrToIntDef(re1.Match[1], 0);
re1.Free;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
var
x2, x3: string;
begin
Label2.Caption := 'Please be Patient while downloading your book';
form1.Cursor := crHourGlass;
ListBox1.Enabled := False;
x1 := ListBox1.Items.Strings[ListBox1.ItemIndex];
x3 := 'https://www.pdfdrive.com' + x1;
x2 := get1webpage(x3);
Download(x2, targetDirectory + getrightfilename(x1) + '.pdf'); // GetfileName(x1)
x2 := '';
Label2.Caption := 'Congratulations Done!';
ListBox1.Enabled := True;
form1.Cursor := crDefault;
OpenDocument(targetDirectory);
end;
function tform1.get1webpage(url: string): string; //required
var
cli: TFPHTTPClient;
html, previw, session: string;
Mstream: TMemoryStream;
ndx, ndx2: integer;
sndx, sndx2: integer;
begin
cli := TFPHTTPClient.Create(nil);
Mstream := TMemoryStream.Create;
cli.AllowRedirect := True;
cli.AddHeader('User-Agent', 'Mozilla/5.0 (Compatible; fpweb)');
cli.AddHeader('Accept',
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8');
cli.AddHeader('Accept-Encoding', 'deflate');
cli.AddHeader('Accept-Language', 'en-US,*');
try
cli.Get(url, Mstream);
Mstream.Position := 0;
SetLength(html, Mstream.Size);
Mstream.ReadBuffer(html[1], Mstream.Size);
if html.Contains('previewButtonMain') then
begin
ndx := html.IndexOf('preview?id=');
ndx := html.IndexOf('=', ndx);
ndx2 := html.IndexOf('&', ndx);
previw := html.Substring(ndx, ndx2 - ndx);
//------------
sndx := html.IndexOf('session=');
sndx := html.IndexOf('=', sndx);
sndx2 := html.IndexOf('" data-download-page=', sndx);
session := html.Substring(sndx, sndx2 - sndx);
Result := 'https://www.pdfdrive.com/download.pdf?id' + previw + '&h' +
session + '&u=cache&ext=pdf';
end;
except
on E: EHttpclient do
begin
if IsConsole then
else;
end;
end;
Mstream.Free;
cli.Free;
end;
end.