Skip to content

Commit a8f686b

Browse files
refactor: newtracon.pas
1 parent 3b9e03d commit a8f686b

File tree

1 file changed

+64
-101
lines changed

1 file changed

+64
-101
lines changed

source/code/newtrackon.pas

Lines changed: 64 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
{ MIT licence
2-
Copyright (c) Gerry Ferdinandus
3-
}
4-
51
unit newtrackon;
62

73
{
@@ -20,32 +16,41 @@ interface
2016

2117
{ TNewTrackon }
2218

19+
//All the type of tracker list.
20+
TNewTrackon_List = (
21+
ntl_URL_All,//< Download from internet
22+
ntl_URL_Live,//< Download from internet
23+
ntl_URL_Stable,//< Download from internet
24+
ntl_URL_UDP,//< Download from internet
25+
ntl_URL_HTTP,//< Download from internet
26+
ntl_CREATE_DEAD//< ntl_CREATE_DEAD is NOT download but created by comparing betwean tracker list
27+
);
28+
2329
TNewTrackon = class
2430
private
25-
FTrackerList_All, FTrackerList_Live, FTrackerList_Stable,
26-
FTrackerList_Dead: TStringList;
31+
FTRackerList: array [TNewTrackon_List] of TStringList;
2732

33+
procedure DownloadTracker(NewTrackon_List: TNewTrackon_List);
2834
procedure CreateTrackerList_Dead;
2935

30-
//need to move to miscellaneous.pas
31-
procedure RemoveTrackersFromList(RemoveList, UpdatedList: TStringList);
32-
procedure SanatizeTrackerList(StringList: TStringList);
33-
3436
public
3537
// all known trackers, dead or alive
36-
property TrackerList_All: TStringList read FTrackerList_All;
38+
property TrackerList_All: TStringList read FTRackerList[ntl_URL_All];
3739

3840
// currently active and responding trackers.
39-
property TrackerList_Live: TStringList read FTrackerList_Live;
41+
property TrackerList_Live: TStringList read FTrackerList[ntl_URL_Live];
4042

4143
// 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;
44+
property TrackerList_Stable: TStringList read FTrackerList[ntl_URL_Stable];
4645

46+
// stable UDP trackers.
47+
property TrackerList_Udp: TStringList read FTrackerList[ntl_URL_UDP];
4748

49+
// stable HTTP/HTTPS trackers.
50+
property TrackerList_Http: TStringList read FTrackerList[ntl_URL_UDP];
4851

52+
// trackers that no longer present in 'live' list
53+
property TrackerList_Dead: TStringList read FTrackerList[ntl_CREATE_DEAD];
4954

5055
//Download all the trackers via API
5156
function DownloadTrackers: boolean;
@@ -59,92 +64,51 @@ TNewTrackon = class
5964

6065
implementation
6166

62-
uses fphttpclient, LazUTF8;
67+
uses fphttpclient, LazUTF8, torrent_miscellaneous;
6368

6469
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';
70+
URL: array [TNewTrackon_List] of string =
71+
(//Warning: the URL strings must be in the same order as TNewTrackon_List
72+
'https://newtrackon.com/api/all',
73+
'https://newtrackon.com/api/live',
74+
'https://newtrackon.com/api/stable',
75+
'https://newtrackon.com/api/udp',
76+
'https://newtrackon.com/api/http',
77+
''//there is no dead tracker list api
78+
);
6879

6980
{ TNewTrackon }
7081

71-
72-
73-
procedure TNewTrackon.RemoveTrackersFromList(RemoveList, UpdatedList: TStringList);
74-
var
75-
TrackerStr: string;
76-
i: integer;
82+
procedure TNewTrackon.CreateTrackerList_Dead;
7783
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;
84+
//FTrackerList_Dead = FTrackerList_All - FTrackerList_Live;
85+
TrackerList_Dead.Assign(TrackerList_All);
86+
RemoveTrackersFromList(TrackerList_Live, TrackerList_Dead);
8687
end;
8788

88-
procedure TNewTrackon.SanatizeTrackerList(StringList: TStringList);
89-
var
90-
TrackerStr: UTF8String;
91-
i: integer;
92-
PositionSpace: PtrInt;
89+
procedure TNewTrackon.DownloadTracker(NewTrackon_List: TNewTrackon_List);
9390
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;
91+
if NewTrackon_List = ntl_CREATE_DEAD then
92+
Exit; //there is no Dead tracker list to be downloaded
12193

94+
//download via URL and put the data in the TrackerList
95+
FTRackerList[NewTrackon_List].DelimitedText :=
96+
TFPCustomHTTPClient.SimpleGet(URL[NewTrackon_List]);
12297

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);
98+
//Clean up the tracker list
99+
SanatizeTrackerList(FTRackerList[NewTrackon_List]);
128100
end;
129101

130102
function TNewTrackon.DownloadTrackers: boolean;
131103
var
132-
str: UTF8String;
104+
i: TNewTrackon_List;
133105
begin
134106
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);
107+
//download all the list one by one
108+
for i in TNewTrackon_List do
109+
begin
110+
DownloadTracker(i);
111+
end;
148112

149113
CreateTrackerList_Dead;
150114

@@ -156,27 +120,26 @@ function TNewTrackon.DownloadTrackers: boolean;
156120
end;
157121

158122
constructor TNewTrackon.Create;
123+
var
124+
i: TNewTrackon_List;
159125
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-
126+
//Create all the TStringList
127+
for i in TNewTrackon_List do
128+
begin
129+
FTrackerList[i] := TStringList.Create;
130+
FTrackerList[i].Duplicates := dupIgnore;
131+
end;
172132
end;
173133

174134
destructor TNewTrackon.Destroy;
135+
var
136+
i: TNewTrackon_List;
175137
begin
176-
FTrackerList_All.Free;
177-
FTrackerList_Live.Free;
178-
FTrackerList_Stable.Free;
179-
FTrackerList_Dead.Free;
138+
//Release all the TStringList
139+
for i in TNewTrackon_List do
140+
begin
141+
FTrackerList[i].Free;
142+
end;
180143

181144
inherited Destroy;
182145
end;

0 commit comments

Comments
 (0)