Skip to content

Commit 20d689f

Browse files
feat: add NewTrackon API routine
1 parent 93935c9 commit 20d689f

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed

source/code/newtrackon.pas

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
{ MIT licence
2+
Copyright (c) Gerry Ferdinandus
3+
}
4+
5+
unit newtrackon;
6+
7+
{
8+
Use api from newtrackon to get tracker list that are working or not.
9+
see: https://newtrackon.com/
10+
}
11+
{$mode objfpc}{$H+}
12+
13+
interface
14+
15+
uses
16+
Classes, SysUtils;
17+
18+
type
19+
20+
21+
{ TNewTrackon }
22+
23+
TNewTrackon = class
24+
private
25+
FTrackerList_All, FTrackerList_Live, FTrackerList_Stable,
26+
FTrackerList_Dead: TStringList;
27+
28+
procedure CreateTrackerList_Dead;
29+
30+
//need to move to miscellaneous.pas
31+
procedure RemoveTrackersFromList(RemoveList, UpdatedList: TStringList);
32+
procedure SanatizeTrackerList(StringList: TStringList);
33+
34+
public
35+
// all known trackers, dead or alive
36+
property TrackerList_All: TStringList read FTrackerList_All;
37+
38+
// currently active and responding trackers.
39+
property TrackerList_Live: TStringList read FTrackerList_Live;
40+
41+
// trackers that have an uptime of equal or more than 95%.
42+
property TrackerList_Stable: TStringList read FTrackerList_Stable;
43+
44+
// trackers that no longer present in 'live' list
45+
property TrackerList_Dead: TStringList read FTrackerList_Dead;
46+
47+
48+
49+
50+
//Download all the trackers via API
51+
function DownloadTrackers: boolean;
52+
53+
//create/destroy class object
54+
constructor Create;
55+
destructor Destroy; override;
56+
end;
57+
58+
59+
60+
implementation
61+
62+
uses fphttpclient, LazUTF8;
63+
64+
const
65+
URL_ALL: string = 'https://newtrackon.com/api/all';
66+
URL_LIVE: string = 'https://newtrackon.com/api/live';
67+
URL_STABLE: string = 'https://newtrackon.com/api/stable';
68+
69+
{ TNewTrackon }
70+
71+
72+
73+
procedure TNewTrackon.RemoveTrackersFromList(RemoveList, UpdatedList: TStringList);
74+
var
75+
TrackerStr: string;
76+
i: integer;
77+
begin
78+
//Remove the trackers that we do not want in the list
79+
for TrackerStr in RemoveList do
80+
begin
81+
//Find the tracker and remove it from the list.
82+
i := UpdatedList.IndexOf(UTF8Trim(TrackerStr));
83+
if i >= 0 then
84+
UpdatedList.Delete(i);
85+
end;
86+
end;
87+
88+
procedure TNewTrackon.SanatizeTrackerList(StringList: TStringList);
89+
var
90+
TrackerStr: UTF8String;
91+
i: integer;
92+
PositionSpace: PtrInt;
93+
begin
94+
//remove all empty space and comment after the URL
95+
96+
if StringList.Count > 0 then
97+
begin
98+
for i := 0 to StringList.Count - 1 do
99+
begin
100+
//process every line one by one
101+
TrackerStr := StringList[i];
102+
103+
//remove empty spaces at the begin/end of line
104+
TrackerStr := UTF8Trim(TrackerStr);
105+
106+
//find the first 'space' found in line
107+
PositionSpace := UTF8Pos(' ', TrackerStr);
108+
if PositionSpace > 0 then
109+
begin
110+
// There is a 'space' found
111+
// Remove everything after this 'space'
112+
TrackerStr := UTF8LeftStr(TrackerStr, PositionSpace - 1);
113+
end;
114+
115+
//write the modified string back
116+
StringList[i] := TrackerStr;
117+
end;
118+
end;
119+
120+
end;
121+
122+
123+
procedure TNewTrackon.CreateTrackerList_Dead;
124+
begin
125+
//FTrackerList_Dead = FTrackerList_All - FTrackerList_Live;
126+
FTrackerList_Dead.Assign(FTrackerList_All);
127+
RemoveTrackersFromList(FTrackerList_Live, FTrackerList_Dead);
128+
end;
129+
130+
function TNewTrackon.DownloadTrackers: boolean;
131+
var
132+
str: UTF8String;
133+
begin
134+
try
135+
//fill all the list one by one
136+
str := TFPCustomHTTPClient.SimpleGet(URL_ALL);
137+
FTrackerList_All.DelimitedText := str;
138+
139+
str := TFPCustomHTTPClient.SimpleGet(URL_LIVE);
140+
FTrackerList_Live.DelimitedText := str;
141+
142+
str := TFPCustomHTTPClient.SimpleGet(URL_STABLE);
143+
FTrackerList_Stable.DelimitedText := str;
144+
145+
SanatizeTrackerList(FTrackerList_All);
146+
SanatizeTrackerList(FTrackerList_Live);
147+
SanatizeTrackerList(FTrackerList_Stable);
148+
149+
CreateTrackerList_Dead;
150+
151+
Result := True;
152+
except
153+
//No OpenSSL or web server is down
154+
Result := False;
155+
end;
156+
end;
157+
158+
constructor TNewTrackon.Create;
159+
begin
160+
161+
FTrackerList_All := TStringList.Create;
162+
FTrackerList_Live := TStringList.Create;
163+
FTrackerList_Stable := TStringList.Create;
164+
FTrackerList_Dead := TStringList.Create;
165+
166+
167+
FTrackerList_All.Duplicates := dupIgnore;
168+
FTrackerList_Live.Duplicates := dupIgnore;
169+
FTrackerList_Stable.Duplicates := dupIgnore;
170+
FTrackerList_Dead.Duplicates := dupIgnore;
171+
172+
end;
173+
174+
destructor TNewTrackon.Destroy;
175+
begin
176+
FTrackerList_All.Free;
177+
FTrackerList_Live.Free;
178+
FTrackerList_Stable.Free;
179+
FTrackerList_Dead.Free;
180+
181+
inherited Destroy;
182+
end;
183+
184+
185+
end.

0 commit comments

Comments
 (0)