-
Notifications
You must be signed in to change notification settings - Fork 0
/
FetchStats.pas
151 lines (126 loc) · 3.21 KB
/
FetchStats.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
unit FetchStats;
interface
uses IdHTTP, html, IdURI;
type
TStatRecord = record
value_points: integer;
value_fleets: integer;
time_u: Int64;
end;
TStatsArray = array of TStatRecord;
TFetchStats = class
private
list: TStatsArray;
http: TIdHTTP;
procedure parseContent(content: string);
procedure parseLink(link: string);
procedure parseData(data: string);
function readCommaSeperatedList(const list: string;
var pos: integer; out item: string): boolean;
public
function getLastStats(player: string): TStatsArray;
constructor Create;
destructor Destroy; override;
end;
implementation
uses SysUtils, StrUtils;
{ TFetchStats }
constructor TFetchStats.Create;
begin
inherited;
http := TIdHTTP.Create(nil);
end;
destructor TFetchStats.Destroy;
begin
http.Free;
inherited;
end;
function TFetchStats.getLastStats(player: string): TStatsArray;
var url, content: string;
p: integer;
begin
// fix error in name:
p := pos('...', player);
if (p > 0) and (p = length(player)-3+1) then
player := copy(player, 1, length(player)-3);
url := 'http://www.war-riders.de/de/109/details/player/' + player;
url := TIdURI.URLEncode(url);
content := http.Get(url);
parseContent(content);
Result := list;
end;
procedure TFetchStats.parseContent(content: string);
var root, chartnode, paramnode: THTMLElement;
link: string;
begin
root := THTMLElement.Create(nil, 'root');
try
root.ParseHTMLCode(content);
chartnode := HTMLFindRoutine_NameAttribute(root, 'object', 'classid', 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000');
if chartnode <> nil then
begin
paramnode := HTMLFindRoutine_NameAttribute(chartnode, 'param', 'name', 'movie');
if paramnode <> nil then
begin
link := paramnode.AttributeValue['value'];
parseLink(link);
end;
end;
finally
root.Free;
end;
end;
procedure TFetchStats.parseData(data: string);
var p, i: integer;
item, kw: string;
begin
kw := '&values=';
p := pos(kw, data);
if p > 0 then
begin
p := p + length(kw);
i := 0;
while readCommaSeperatedList(data, p, item) do
begin
SetLength(list, i+1);
list[i].value_points := StrToIntDef(item, -1);
inc(i);
end;
end;
p := pos('&values_2=', data);
if p > 0 then
begin
end;
p := pos('&x_labels=', data);
if p > 0 then
begin
end;
end;
procedure TFetchStats.parseLink(link: string);
var p: integer;
datalink: string;
begin
p := pos('data=/data/', link);
if p > 0 then
begin
datalink := copy(link, p+6, 99999);
parseData(
http.Get('http://www.war-riders.de/' + datalink));
end;
end;
function TFetchStats.readCommaSeperatedList(const list: string;
var pos: integer; out item: string): boolean;
var e, n: integer;
begin
Result := list[pos] <> '&';
if Result then
begin
e := PosEx('&', list, pos);
n := PosEx(',', list, pos);
if n > e then n := e;
item := copy(list, pos, (n-pos));
if n <> e then inc(n); // am ende bleiben wir auf dem '&' stehen!
pos := n;
end;
end;
end.