-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBitlyParser.pas
More file actions
90 lines (74 loc) · 2.35 KB
/
BitlyParser.pas
File metadata and controls
90 lines (74 loc) · 2.35 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
unit BitlyParser;
interface
uses SysUtils, Classes, NativeXML,BitlyAPI, Generics.Collections;
resourcestring
rcErrorResponse='Ответ сервера bit.ly: %s';
type
Tag_ShortURL = Record
URL: string;
Hash:string;
GlobaCash:string;
NewHash: boolean;
end;
type
Tag_Click = record
short_url: string;
global_hash:string;
user_clicks:integer;
user_hash: string;
global_clicks: integer;
end;
type
TClicks = class (TLIst<Tag_Click>);
type
TBitlyParser = class
public
class function ParseShorten(const Responce:string):Tag_ShortURL;
class function ParseClicks(const Responce:string):TClicks;
end;
implementation
{ TBitlyParser }
{ TBitlyParser }
class function TBitlyParser.ParseClicks(const Responce:string): TClicks;
var XMLdoc: TNativeXml;
i:integer;
List:TXmlNodeList;
Click: Tag_Click;
begin
XMLdoc:=TNativeXml.Create;
XMLdoc.ReadFromString(Responce);
if XMLdoc.Root.ReadInteger('status_code')=200 then
begin
List:=TXmlNodeList.Create;
Result:=TClicks.Create;
XMLdoc.Root.FindNodes('clicks',List);
for i:=0 to List.Count - 1 do
begin
Click.short_url:=List.Items[i].ReadString('short_url');
Click.global_hash:=List.Items[i].ReadString('global_hash');
Click.user_clicks:=List.Items[i].ReadInteger('user_clicks');
Click.user_hash:=List.Items[i].ReadString('user_hash');
Click.global_clicks:=List.Items[i].ReadInteger('global_clicks');
Result.Add(Click);
end;
end
else
raise Exception.Create(Format(rcErrorResponse,[XMLdoc.Root.ReadInteger('status_txt')]));
end;
class function TBitlyParser.ParseShorten(
const Responce: string): Tag_ShortURL;
var XMLdoc: TNativeXml;
begin
XMLdoc:=TNativeXml.Create;
XMLdoc.ReadFromString(Responce);
if XMLdoc.Root.ReadInteger('status_code')=200 then
begin
Result.URL:=XMLdoc.Root.FindNode('data').ReadString('url');
Result.Hash:=XMLdoc.Root.FindNode('data').ReadString('hash');
Result.GlobaCash:=XMLdoc.Root.FindNode('data').ReadString('global_hash');
Result.NewHash:=XMLdoc.Root.FindNode('data').ReadBool('new_hash');
end
else
raise Exception.Create(Format(rcErrorResponse,[XMLdoc.Root.ReadInteger('status_txt')]));
end;
end.