-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathCMLParsers.pas
102 lines (87 loc) · 3.29 KB
/
CMLParsers.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
unit CMLParsers;
interface
uses CMLTypes, System.SysUtils;
function extractNearValue(Text, Anchor: WideString; StartChar: WideChar = '"'; EndChar: WideChar = '"'): WideString;
function extractTokenFromText(Text: WideString; var token: WideString): Boolean;
function extractPublicTokenFromText(Text: WideString; var PublicToken: WideString): Boolean;
function extract_x_page_id_FromText(Text: WideString; var PageId: WideString): Boolean;
function extract_build_FromText(Text: WideString; var build: WideString): Boolean;
function extract_upload_url_FromText(Text: WideString; var UploadUrl: WideString): Boolean;
function extractPublicShard(Text: WideString; var Shard: WideString): Boolean;
function extractTwostepJson(Text: WideString; var JSON: WideString): Boolean;
implementation
function extractNearValue(Text, Anchor: WideString; StartChar: WideChar = '"'; EndChar: WideChar = '"'): WideString;
var
start, end_: integer;
begin
result := EmptyWideStr;
Text := StringReplace(Text, #$A, EmptyWideStr, [rfReplaceAll]); //òàê íàì ïðîùå êîâûðÿòüñÿ â òåêñòå
Text := StringReplace(Text, #$D, EmptyWideStr, [rfReplaceAll]);
Text := StringReplace(Text, #9, EmptyWideStr, [rfReplaceAll]);
Text := StringReplace(Text, #$20, EmptyWideStr, [rfReplaceAll]);
start := Pos(WideString(Anchor), Text);
if start > 0 then
begin
start := Pos(StartChar, Text, start + length(Anchor)) + 1;
end_ := Pos(EndChar, Text, start);
result := copy(Text, start, end_ - start);
end;
end;
function extractPublicTokenFromText(Text: WideString; var PublicToken: WideString): Boolean;
begin
PublicToken := extractNearValue(Text, '"tokens":{"download":');
result := EmptyWideStr <> PublicToken;
end;
function extract_x_page_id_FromText(Text: WideString; var PageId: WideString): Boolean;
begin
PageId := extractNearValue(Text, '"x-page-id"');
result := PageId <> EmptyWideStr;
end;
function extract_build_FromText(Text: WideString; var build: WideString): Boolean;
begin
build := extractNearValue(Text, '"BUILD"');
result := build <> EmptyWideStr;
end;
function extract_upload_url_FromText(Text: WideString; var UploadUrl: WideString): Boolean;
var
start, start1, start2, finish, length: Cardinal;
temp: WideString;
begin
result := false;
start := Pos(WideString('mail.ru/upload/"'), Text);
if start > 0 then
begin
start1 := start - 50;
finish := start + 15;
length := finish - start1;
temp := copy(Text, start1, length);
start2 := Pos(WideString('https://'), temp);
UploadUrl := copy(temp, start2, Strlen(PWideChar(temp)) - start2);
result := true;
end;
end;
function extractPublicShard(Text: WideString; var Shard: WideString): Boolean;
begin
Shard := extractNearValue(Text, '"weblink_get":', '[', ']');
Shard := extractNearValue(Shard, '"url":');
result := EmptyWideStr <> Shard;
end;
function extractTokenFromText(Text: WideString; var token: WideString): Boolean;
begin
token := extractNearValue(Text, '"csrf"');
result := token <> EmptyWideStr;
end;
function extractTwostepJson(Text: WideString; var JSON: WideString): Boolean;
var
start, finish: integer;
begin
result := false;
start := Pos(WideString('<script type="text/html" id="json">'), Text);
finish := Pos(WideString('</script>'), Text);
if (start > 0) and (finish > 0) then
begin
JSON := copy(Text, start + 35, finish - start - 35);
result := true;
end;
end;
end.