-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFPL_Internet.pas
236 lines (193 loc) · 7.39 KB
/
FPL_Internet.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
unit FPL_Internet;
interface
uses
System.Classes;
type
IFPLHttp = interface
function fetchURL(const aURL: string): string;
function fetchURLToFile(const aURL, aFilePath: string): boolean;
function fetchURLToStream(const aURL: string; const [ref] aStream: TStream): boolean;
function login: string;
end;
function FPLHttp: IFPLHttp;
implementation
uses
idHTTP, idSSLOpenSSL, System.SysUtils, Spring, idAuthentication, idCookie, idUri, FMX.Dialogs;
type
TFPLHttp = class(TInterfacedObject, IFPLHttp)
strict private
function fetchURL(const aURL: string): string;
function fetchURLToFile(const aURL, aFilePath: string): boolean;
function fetchURLToStream(const aURL: string; const [ref] aStream: TStream): boolean;
function login: string;
private
procedure HTTPOnRedirect(Sender: TObject; var dest: string; var NumRedirect: Integer; var Handled: boolean; var VMethod: TIdHTTPMethod);
end;
function FPLHttp: IFPLHttp;
begin
result := TFPLHttp.Create;
end;
{ TFPLHTTP }
function TFPLHttp.fetchURL(const aURL: string): string;
var
http: TIdHTTP;
sslHandler: TIdSSLIOHandlerSocketOpenSSL;
ss: TStringStream;
begin
guard.CheckTrue(trim(aURL) <> '', 'FPLHttp: no URL');
http := TIdHTTP.Create(nil);
http.Request.ContentEncoding := 'UTF-8';
http.ProxyParams.ProxyPort := 8888;
http.ProxyParams.ProxyServer := 'localhost';
sslHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
sslHandler.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
http.IOHandler := sslHandler;
ss := TStringStream.Create('', TEncoding.UTF8);
try
http.get(aUrl, ss);
result := ss.DataString;
finally
ss.Free;
sslHandler.Free;
http.Free;
end;
end;
function TFPLHttp.fetchURLToStream(const aURL: string; const [ref] aStream: TStream): boolean;
var
http: TIdHTTP;
sslHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
guard.CheckTrue(trim(aURL) <> '', 'FPLHttp: no URL');
http := TIdHTTP.Create(nil);
sslHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
sslHandler.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
http.IOHandler := sslHandler;
try
http.Get(aURL, aStream);
finally
sslHandler.Free;
http.Free;
end;
end;
function TFPLHttp.fetchURLToFile(const aURL: string; const aFilePath: string): boolean;
begin
var FS := TFileStream.Create(aFilePath, fmCreate);
try
try
fetchURLToStream(aURL, FS);
except
DeleteFile(aFilePath);
end;
finally
if FS.Size = 0 then begin
FS.Free;
DeleteFile(aFilePath);
end
else
FS.Free;
end;
end;
//======= EXPERIMENTAL =======
procedure TFPLHttp.HTTPOnRedirect(Sender: TObject; var dest: string; var NumRedirect: Integer; var Handled: boolean; var VMethod: TIdHTTPMethod);
begin
Handled := TRUE;
end;
function TFPLHttp.login: string;
// couldn't get this to work
const
URL_LOGIN = 'https://users.premierleague.com/accounts/login/';
URL_PUNTER = 'https://fantasy.premierleague.com/api/my-team/4206643/'; // requires login
var
http: TIdHTTP;
sslHandler: TIdSSLIOHandlerSocketOpenSSL;
ss: TStringStream;
params: TStringList;
cookie: TIdCookie;
token: AnsiString;
content: string;
posCookie: integer;
myToken: string;
sPost: string;
mwCookie: TIdCookie;
uri: TIdUri;
begin
http := TIdHTTP.Create(nil);
http.Request.ContentEncoding := 'UTF-8';
params := TStringList.Create;
params.Values['login'] := '';
params.Values['password'] := '';
params.Values['redirecct_uri'] := 'https://users.premierleague.com/';
// params.Values['app'] := 'plfpl-web';
params.Values['app'] := 'plusers';
// params.Values['_fbp'] := 'fb.1.1619305060285.2130919484';
// params.Values['DNT'] := '1';
http.AllowCookies := TRUE;
http.HandleRedirects := TRUE;
http.RedirectMaximum := 999;
http.ProxyParams.ProxyPort := 8888;
http.ProxyParams.ProxyServer := 'localhost';
// HTTP.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9';
// HTTP.Request.AcceptCharSet := 'iso-8859-1, utf-8, utf-16, *;q=0.1';
// HTTP.Request.AcceptEncoding := 'gzip, deflate, br';
// http.Request.AcceptLanguage := 'en-GB,en;q=0.9';
HTTP.Request.Connection := 'Keep-Alive';
// HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
// HTTP.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36';
// HTTP.Request.CharSet := 'utf-8';
HTTP.Request.Referer := 'https://users.premierleague.com/?state=fail&reason=credentials';
sslHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
sslHandler.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
http.IOHandler := sslHandler;
ss := TStringStream.Create('', TEncoding.UTF8);
try
try
content := http.get(URL_LOGIN);
result := content;
posCookie := pos('csrfmiddlewaretoken" value="', content);
myToken := copy(content, posCookie + 28, 64);
params.Values['csrfmiddlewaretoken'] := token;
case http.CookieManager.CookieCollection.Count > 0 of
FALSE: begin ShowMessage('No cookie returned'); EXIT; end;
TRUE: begin
cookie := HTTP.CookieManager.CookieCollection.Cookie['csrftoken', 'users.premierleague.com'];
token := cookie.Value;
params.Values['csrtoken'] := token;
mwCookie := http.CookieManager.CookieCollection.Add;
mwCookie.Assign(cookie);
mwcookie.CookieName := 'csrfmiddlewaretoken';
mwCookie.Domain := 'users.premierleague.com';
mwcookie.Value := myToken;
// uri.URI := URL_LOGIN;
http.HandleRedirects := FALSE;
http.OnRedirect := HttpOnRedirect;
// http.Request.CustomHeaders.Add('Authorization: Token ' + token);
try
http.POST(URL_LOGIN, params, ss);
ShowMessage('Post: ' + ss.DataString);
except on E:Exception do
ShowMessage('postE: ' + E.Message);
end;
//ShowMessage('Post: ' + http.ResponseCode.ToString + ' (' + http.ResponseText + ')');
// HTTP.Request.CustomHeaders.Add('x-csrftoken:'+ token);
// HTTP.Request.CustomHeaders.Add('csrftoken:'+ token);
// HTTP.Request.CustomHeaders.Add('csrfmiddlewaretoken:'+ token);
// http.Request.CustomHeaders.Add('Authorization:' + token);
http.get(URL_PUNTER, ss);
//result := ss.DataString;
end;
end;
except
result := 'Post response: ' + sPost + #13#10 +
'get my team: ' + http.ResponseText + #13#10 + ss.DataString + #13#10 +
'My: ' + myToken + #13#10 +
'CM: ' + http.CookieManager.CookieCollection.Cookie['csrftoken', 'users.premierleague.com'].Value + #13#10
+ result;
end;
finally
params.Free;
ss.Free;
sslHandler.Free;
http.Free;
end;
end;
end.