-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBitlyAPI.pas
207 lines (187 loc) · 5.7 KB
/
BitlyAPI.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
unit BitlyAPI;
interface
uses SysUtils, Classes, httpsend, TypInfo, dialogs;
resourcestring
rcValidateError = 'BIT.LY: Недействительное значение API-key или Login';
rcQueryError = 'Ошибка выполнения запроса';
rcOutputFormatError = 'Формат %s не поддерживается в запросе %s';
const
APIURL = 'http://api.bit.ly/v3/%s?login=%s&apiKey=%s&%s';
http = 'http://';
type
TBitlyDomains = (tdBit, tdJmp);
TBitlyFormat = (xml,json,txt);
type
TBitly = class
private
FLogin: string;
FApiKey: string;
function NormalizeURL(const aURL: string):string;
function GetParamString(const Params:TStringList):string;
function GetFormat(aFormat:TBitlyFormat):string;
function GETQuery(const URL:string):string;
public
constructor Create(const Login, APIKey: string);
function Shorten(const LongURL:string; Domain:TBitlyDomains;OutputFormat:TBitlyFormat=xml):string;
function Validate(const Login, APIKey: string; OutputFormat:TBitlyFormat=xml):string;
function Expand(const URLs,Hashes:TStringList;OutputFormat:TBitlyFormat=xml):string;
function Clicks(const URLs,Hashes:TStringList;OutputFormat:TBitlyFormat=xml):string;
function BitlyProDomain(const domain:string;OutputFormat:TBitlyFormat=xml):string;
property Login: string read FLogin write FLogin;
property ApiKey: string read FApiKey write FApiKey;
end;
implementation
{ TBitly }
function URLEncode(const WStr:String):string;
var UStr:UTF8String;
i:integer;
begin
UStr:=UTF8Encode(WStr);
result:='';
if length(UStr)>0 then
for i:=1 to length(UStr) do
result:=result+'%'+IntToHex(ord(UStr[i]),2);
end;
function TBitly.BitlyProDomain(const domain: string;
OutputFormat: TBitlyFormat): string;
var params:TStringList;
begin
if OutputFormat=txt then
raise Exception.Create(Format(rcOutputFormatError,['txt','bitly_pro_domain']));
try
params:=TStringList.Create;
params.Add('domain='+StringReplace(domain, http,'',[rfReplaceAll]));
params.Add(GetFormat(OutputFormat));
Result:=GETQuery(Format(APIURL,['bitly_pro_domain',FLogin,FAPIKey,GetParamString(Params)]));
finally
FreeAndNil(params);
end;
end;
function TBitly.Clicks(const URLs, Hashes: TStringList;
OutputFormat: TBitlyFormat): string;
var i:integer;
params: TStringList;
begin
if OutputFormat=txt then
raise Exception.Create(Format(rcOutputFormatError,['txt','clicks']));
try
params:=TStringList.Create;
if URLs<>nil then
if URLs.Count>0 then
for I:=0 to URLs.Count - 1 do
params.Add('shortUrl='+NormalizeURL(URLs[i]));
if Hashes<>nil then
if Hashes.Count>0 then
for I:=0 to Hashes.Count - 1 do
params.Add('hash='+Hashes[i]);
params.Add(GetFormat(OutputFormat));
Result:=GETQuery(Format(APIURL,['clicks',FLogin,FAPIKey,GetParamString(Params)]));
finally
FreeAndNil(params);
end;
end;
constructor TBitly.Create(const Login, APIKey: string);
var params: TStringList;
begin
inherited Create;
try
params:=TStringList.Create;
params.Add(Validate(Login,APIKey,txt));
if (params.Count>0)and(params[0]='1') then
begin
FLogin:=Login;
FApiKey:=APIKey;
end
else
raise Exception.Create(rcValidateError);
finally
FreeAndNil(params)
end;
end;
function TBitly.Expand(const URLs, Hashes: TStringList;
OutputFormat: TBitlyFormat): string;
var i:integer;
params: TStringList;
begin
try
params:=TStringList.Create;
if URLs.Count>0 then
for I:=0 to URLs.Count - 1 do
params.Add('shortUrl='+NormalizeURL(URLs[i]));
if Hashes.Count>0 then
for I:=0 to Hashes.Count - 1 do
params.Add('hash='+Hashes[i]);
params.Add(GetFormat(OutputFormat));
Result:=GETQuery(Format(APIURL,['expand',FLogin,FAPIKey,GetParamString(Params)]));
finally
FreeAndNil(params);
end;
end;
function TBitly.GetFormat(aFormat: TBitlyFormat): string;
begin
Result :='format='+GetEnumName(TypeInfo(TBitlyFormat), Ord(aFormat));
end;
function TBitly.GetParamString(const Params: TStringList): string;
begin
Params.Delimiter:='&';
Result:=Params.DelimitedText;
end;
function TBitly.GETQuery(const URL: string): string;
var Res:TStringList;
begin
try
Res:=TStringList.Create;
with THTTPSend.Create do
begin
if HTTPMethod('GET',URL) then
begin
Res.LoadFromStream(Document);
Result:=Trim(Res.Text);
end
else
MessageDlg(rcQueryError,mtError,[mbOK],0);
end;
finally
FreeAndNil(Res);
end;
end;
function TBitly.NormalizeURL(const aURL: string): string;
begin
if pos(http,aURL)<=0 then
Result:=http+aURL
else
Result:=aURL;
Result:=StringReplace(Result,'?','/?',[rfReplaceAll,rfIgnoreCase]);
Result:=URLEncode(Result);
end;
function TBitly.Shorten(const LongURL: string; Domain: TBitlyDomains;OutputFormat:TBitlyFormat): string;
var Params:TStringList;
begin
try
Params:=TStringList.Create;
Params.Add('uri='+NormalizeURL(LongURL));
Params.Add(GetFormat(OutputFormat));
case Domain of
tdBit:Params.Add('domain=bit.ly');
tdJmp:Params.Add('domain=j.mp');
end;
Result:=GETQuery(Format(APIURL,['shorten',FLogin,FAPIKey,GetParamString(Params)]));
finally
FreeAndNil(Params);
end;
end;
function TBitly.Validate(const Login, APIKey: string;
OutputFormat: TBitlyFormat): string;
var params:TStringList;
begin
try
params:=TStringList.Create;
params.Add('x_login='+Login);
params.Add('x_apiKey='+APIKey);
params.Add(GetFormat(OutputFormat));
Result:=GETQuery(Format(APIURL,['validate',Login,APIKey,GetParamString(Params)]));
finally
FreeAndNil(params);
end;
end;
end.