This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEvilWorks.Web.Twitter.pas
411 lines (344 loc) · 9.78 KB
/
EvilWorks.Web.Twitter.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//
// EvilLibrary by Vedran Vuk 2010-2012
//
// Name: EvilWorks.Web.Twitter
// Description: Twitter client.
// File last change date: October 30th. 2012
// File version: Dev 0.0.0
// Licence: Free.
//
unit EvilWorks.Web.Twitter;
interface
uses
Winapi.Windows,
System.Classes,
System.SysUtils,
EvilWorks.Api.Winsock2,
EvilWorks.Api.ZLib,
EvilWorks.System.SysUtils,
EvilWorks.System.StrUtils,
EvilWorks.Web.AsyncSockets,
EvilWorks.Web.HTTP,
EvilWorks.Web.Base64,
EvilWorks.Web.URI;
type
{ Forward declarations. }
TTwitterClient = class;
{ Events. }
TOnResponse = procedure(aSender: TTwitterClient; const aResponse: string) of object;
{ TTwitterClient }
TTwitterClient = class(TCustomHTTPClient)
private
FResponse : TStringStream;
FOnResponse: TOnResponse;
protected
procedure EventBodyAvailable(aData: TStream); override;
procedure EventResponse(const aResponse: string);
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
published
property AddressFamily;
property Authorization;
property BindHost;
property BindPort;
property ConnectTimeout;
property ProxyChain;
property RemoteHost;
property RemotePort;
property RequestHeaders;
property SocketState;
property SSL;
property UserAgent;
property OnConnected;
property OnConnecting;
property OnConnectTimeout;
property OnDisconnected;
property OnError;
property OnLog;
property OnProxyChainConnected;
property OnProxyConnecting;
property OnProxyConnected;
property OnResolved;
property OnResolving;
property OnRequestHeaders;
property OnResponseHeaders;
property OnResponse: TOnResponse read FOnResponse write FOnResponse;
end;
{ TTwitterClient }
TTwitterRESTClient = class(TTwitterClient)
public
constructor Create(aOwner: TComponent); override;
public
{ ======== }
{ Timeline }
{ ======== }
procedure TimelineHomeTimeline(
const aCount: integer = 20;
const aSinceID: int64 = - 1;
const aMaxID: int64 = - 1;
const aTrimUser: boolean = False;
const aExcludeReplies: boolean = False;
const aContributorDetails: boolean = False;
const aIncludeEntities: boolean = True
);
{ ====== }
{ Tweets }
{ ====== }
procedure TweetsStatusUpdate(
const aStatus: string;
const aInReplyToID: int64 = - 1;
const aLatitude: double = - 180;
const aLongitude: double = - 360;
const aPlaceID: string = '';
const aDisplayCoordinates: boolean = False;
const aTrimUser: boolean = False
);
{ ==== }
{ Help }
{ ==== }
procedure HelpConfiguration;
{ =========== }
{ Unsupported }
{ =========== }
procedure RelatedResults(
const aID: int64;
const aIncludeEntities: boolean = True;
const aIncludeUserEntities: boolean = True;
const aIncludeCards: boolean = True;
const aSendErrorCodes: boolean = True
);
procedure StatusActivity(const aID: int64);
end;
{ TTwitterStreamClient }
TTwitterStreamClient = class(TTwitterClient)
private
FMessageSize: integer;
FMessageMark: integer;
protected
procedure EventBodyAvailable(aData: TStream); override;
public
constructor Create(aOwner: TComponent); override;
procedure StatusesFilter(
const aFollow: TStrings; // List of user IDs, indicating the users to return statuses for.
const aTrack: TStrings; // Keywords to track.
const aLocations: TStrings // Set of bounding boxes to track.
);
procedure User(
const aInclFollowers: boolean = False;
const aInclReplies: boolean = False;
const aKeywords: TStrings = nil;
const aLocations: TStrings = nil
);
end;
implementation
{ TTwitterClient }
constructor TTwitterClient.Create(aOwner: TComponent);
begin
inherited;
UserAgent := 'EvilTwitter/0.1';
Authorization.AuthorizationType := hatOAuth1;
Authorization.Enabled := True;
FResponse := TStringStream.Create;
end;
destructor TTwitterClient.Destroy;
begin
FResponse.Free;
inherited;
end;
procedure TTwitterClient.EventBodyAvailable(aData: TStream);
begin
aData.Seek(0, soFromBeginning);
EventResponse(TStringStream(aData).ReadString(aData.Size));
FResponse.Clear;
end;
procedure TTwitterClient.EventResponse(const aResponse: string);
begin
if (Assigned(FOnResponse)) then
FOnResponse(Self, aResponse);
end;
{ TTwitterRESTClient }
constructor TTwitterRESTClient.Create(aOwner: TComponent);
begin
inherited;
Options := (Options + [hcoCombineChunks]);
end;
procedure TTwitterRESTClient.TimelineHomeTimeline(
const aCount: integer; const aSinceID, aMaxID: int64;
const aTrimUser, aExcludeReplies, aContributorDetails, aIncludeEntities: boolean);
const
ResourceURI = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
var
t: TTokens;
r: string;
begin
r := ResourceURI;
t.Clear;
t.Add('count', TextFromInt(aCount));
if (aSinceID > - 1) then
t.Add('since_id', aSinceID);
if (aMaxID > - 1) then
t.Add('max_id', aMaxID);
t.Add('trim_user', aTrimUser);
t.Add('exclude_replies', aExcludeReplies);
t.Add('contributor_details', aContributorDetails);
t.Add('include_entities', aIncludeEntities);
if (t.Empty = False) then
r := r + '?' + t.AllTokens('&');
Get(r, FResponse);
end;
procedure TTwitterRESTClient.TweetsStatusUpdate(
const aStatus: string; const aInReplyToID: int64; const aLatitude, aLongitude: double;
const aPlaceID: string; const aDisplayCoordinates, aTrimUser: boolean);
const
ResourceURI = 'https://api.twitter.com/1.1/statuses/update.json';
var
t: TTokens;
r: string;
begin
r := ResourceURI;
t.Clear;
if (aInReplyToID > - 1) then
t.Add('in_reply_to_id', TextFromInt(aInReplyToID));
if (aLatitude >= - 90) and (aLatitude <= 90) then
t.Add('lat', TextFromFloat(aLatitude));
if (aLongitude >= - 180) and (aLongitude <= 180) then
t.Add('long', TextFromFloat(aLongitude));
if (aPlaceID <> '') then
t.Add('place_id', aPlaceID);
if (aDisplayCoordinates) then
t.Add('display_coordinates', TextFromBool(aDisplayCoordinates));
if (aTrimUser) then
t.Add('trim_user', TextFromBool(aTrimUser));
if (t.Empty = False) then
r := r + '?' + t.AllTokens('&');
t.Clear;
t.Add('status', aStatus);
PostForm(r, t.AllTokens('&'), FResponse);
end;
procedure TTwitterRESTClient.HelpConfiguration;
const
ResourceURI = 'https://api.twitter.com/1.1/help/configuration.json';
begin
Get(ResourceURI, FResponse);
end;
procedure TTwitterRESTClient.RelatedResults(
const aID: int64; const aIncludeEntities, aIncludeUserEntities, aIncludeCards, aSendErrorCodes: boolean);
const
ResourceURI = 'https://api.twitter.com/1/related_results/show/';
var
t: TTokens;
r: string;
begin
r := ResourceURI;
t.Clear;
if (aIncludeEntities) then
t.Add('include_entities', 'true');
if (aIncludeUserEntities) then
t.Add('include_user_entities', 'true');
if (aIncludeCards) then
t.Add('include_cards', 'true');
if (aSendErrorCodes) then
t.Add('send_error_codes', 'true');
if (t.Empty = False) then
r := r + TextFromInt(aID) + '.json?' + t.AllTokens('&');
Get(r, FResponse);
end;
procedure TTwitterRESTClient.StatusActivity(const aID: int64);
begin
Get('https://api.twitter.com/i/statuses/' + TextFromInt(aID) + '/activity/summary.json', FResponse);
end;
{ TTwitterStreamClient }
constructor TTwitterStreamClient.Create(aOwner: TComponent);
begin
inherited;
Connection := CKeepALive;
FMessageSize := 0;
FMessageMark := 0;
end;
procedure TTwitterStreamClient.EventBodyAvailable(aData: TStream);
var
temp: string;
begin
if (aData.Size < 2) then
Exit;
// Read in message size or Keep-alives.
if (FMessageSize = 0) then
begin
aData.Seek(0, soFromBeginning);
// Optional Keep-alive linefeed between messages.
if (TStringStream(aData).DataString = #13#10) then
begin
FResponse.Clear;
Exit;
end;
// Get message size text.
temp := TextFetchLine(TStringStream(aData).DataString);
if (temp = '') then
Exit; // If we are getting incomplete line, exit to buffer it.
// Mark message start position in buffer.
FMessageMark := (Length(temp) + 2);
// Convert message size text to integer.
FMessageSize := TextToInt(temp, - 1);
if (FMessageSize = - 1) then
begin
HandleError( - 2, 'Error parsing length delimited message: Message size to integer failed.');
Exit;
end;
end;
// Check if we have complete message.
aData.Seek(FMessageMark, soFromBeginning);
if ((aData.Size - aData.Position) >= FMessageSize) then
begin
EventResponse(string(TStringStream(aData).ReadString(FMessageSize)));
FMessageSize := 0;
FMessageMark := 0;
FResponse.Clear;
end;
end;
procedure TTwitterStreamClient.StatusesFilter(const aFollow, aTrack, aLocations: TStrings);
const
ResourceURI = 'https://stream.twitter.com/1.1/statuses/filter.json';
var
t: TTokens;
begin
aFollow.Delimiter := ',';
aTrack.Delimiter := ',';
aLocations.Delimiter := ',';
t.Clear;
if (aFollow.Count > 0) then
t.Add('follow', aFollow.DelimitedText);
if (aTrack.Count > 0) then
t.Add('track', aTrack.DelimitedText);
if (aLocations.Count > 0) then
t.Add('locations', aLocations.DelimitedText);
t.Add('delimited', 'length');
PostForm(ResourceURI, t.AllTokens('&'), FResponse);
end;
procedure TTwitterStreamClient.User(
const aInclFollowers, aInclReplies: boolean; const aKeywords, aLocations: TStrings);
const
ResourceURI = 'https://userstream.twitter.com/1.1/user.json';
var
t: TTokens;
begin
t.Clear;
if (aInclFollowers) then
t.Add('with', 'followings')
else
t.Add('with', 'user');
if (aInclReplies) then
t.Add('replies', 'all');
if (aKeywords <> nil) then
begin
aKeywords.Delimiter := ',';
t.Add('track', aKeywords.DelimitedText);
end;
if (aLocations <> nil) then
begin
aKeywords.Delimiter := ',';
t.Add('locations', aLocations.DelimitedText);
end;
Get(ResourceURI + '?delimited=length&' + t.AllTokens('&'), FResponse);
end;
end.