@@ -29,11 +29,9 @@ interface
2929 TNewTrackon = class
3030 private
3131 FTRackerList: array [TNewTrackon_List] of TStringList;
32- FDownloadStatus: boolean;
3332
34- procedure DownloadTracker (NewTrackon_List: TNewTrackon_List);
33+ function DownloadTracker (NewTrackon_List: TNewTrackon_List): boolean ;
3534 procedure CreateTrackerList_Dead ;
36-
3735 public
3836 // all known trackers, dead or alive
3937 property TrackerList_All: TStringList read FTRackerList[ntl_URL_All];
@@ -53,8 +51,15 @@ TNewTrackon = class
5351 // trackers that no longer present in 'live' list
5452 property TrackerList_Dead: TStringList read FTrackerList[ntl_CREATE_DEAD];
5553
56- // Download all the trackers via API
57- function DownloadTrackers : boolean;
54+ // Download all the types the trackers via API
55+ function DownloadEverything : boolean;
56+
57+ // Download only three types of the trackers via API
58+ function Download_All_Live_Stable : boolean;
59+
60+ // Submit tracker to newTrackon via http POST
61+ function SubmitTrackers (TrackerList: TStringList;
62+ out TrackersSendCount: integer): boolean;
5863
5964 // create/destroy class object
6065 constructor Create;
@@ -65,7 +70,7 @@ TNewTrackon = class
6570
6671implementation
6772
68- uses fphttpclient, LazUTF8, torrent_miscellaneous;
73+ uses fphttpclient, LazUTF8, torrent_miscellaneous, httpdefs ;
6974
7075const
7176 URL: array [TNewTrackon_List] of string =
@@ -80,47 +85,129 @@ implementation
8085
8186{ TNewTrackon }
8287
88+
8389procedure TNewTrackon.CreateTrackerList_Dead ;
8490begin
8591 // FTrackerList_Dead = FTrackerList_All - FTrackerList_Live;
8692 TrackerList_Dead.Assign(TrackerList_All);
8793 RemoveTrackersFromList(TrackerList_Live, TrackerList_Dead);
8894end ;
8995
90- procedure TNewTrackon.DownloadTracker (NewTrackon_List: TNewTrackon_List);
96+ function TNewTrackon.DownloadTracker (NewTrackon_List: TNewTrackon_List): boolean ;
9197begin
92- if NewTrackon_List = ntl_CREATE_DEAD then
93- Exit; // there is no Dead tracker list to be downloaded
9498
95- // download via URL and put the data in the TrackerList
96- // will create exception if something is wrong
97- FTRackerList[NewTrackon_List].DelimitedText :=
98- TFPCustomHTTPClient.SimpleGet(URL[NewTrackon_List]);
99+ try
100+ // there is no Dead tracker list to be downloaded. so it can be skip
101+ if NewTrackon_List <> ntl_CREATE_DEAD then
102+ begin
103+ // download via URL and put the data in the TrackerList
104+ // will create exception if something is wrong
105+ FTRackerList[NewTrackon_List].DelimitedText :=
106+ TFPCustomHTTPClient.SimpleGet(URL[NewTrackon_List]);
107+ end ;
99108
100- // Clean up the tracker list
109+ Result := True;
110+ except
111+ // No OpenSSL or web server is down
112+ Result := False;
113+ end ;
114+
115+ // Clean up the tracker list just downloaded
101116 SanatizeTrackerList(FTRackerList[NewTrackon_List]);
102117end ;
103118
104- function TNewTrackon.DownloadTrackers : boolean;
119+ function TNewTrackon.DownloadEverything : boolean;
105120var
106121 i: TNewTrackon_List;
107122begin
108- try
109- // download all the list one by one
110- for i in TNewTrackon_List do
123+ // download all the list one by one
124+ for i in TNewTrackon_List do
125+ begin
126+ Result := DownloadTracker(i);
127+ if not Result then
128+ Exit;
129+ end ;
130+
131+ CreateTrackerList_Dead;
132+ end ;
133+
134+ function TNewTrackon.Download_All_Live_Stable : boolean;
135+ begin
136+ Result := DownloadTracker(ntl_URL_All);
137+ if Result then
138+ begin
139+ Result := DownloadTracker(ntl_URL_Stable);
140+ if Result then
111141 begin
112- DownloadTracker(i); // < may create exception
142+ Result := DownloadTracker(ntl_URL_Live);
113143 end ;
144+ end ;
114145
115- CreateTrackerList_Dead;
146+ CreateTrackerList_Dead;
147+ end ;
116148
117- FDownloadStatus := True;
118- except
119- // No OpenSSL or web server is down
120- FDownloadStatus := False;
121- end ;
149+ function TNewTrackon.SubmitTrackers (TrackerList: TStringList;
150+ out TrackersSendCount: integer): boolean;
151+ var
152+ TrackerListToBeSend: TStringList;
153+ FormData: string;
154+ Trackers: string;
155+ HTTPS: TFPHTTPClient;
156+ Seperator: string;
157+
158+ const
159+ URL_POST = ' https://newtrackon.com/api/add' ;
160+ begin
161+ TrackersSendCount := 0 ;
122162
123- Result := FDownloadStatus;
163+ // Must always first download the ALL tracker list if not already downloaded.
164+ // To make sure it is always checking agains the most recent list
165+ Result := DownloadTracker(ntl_URL_All);
166+
167+ if Result then
168+ begin
169+ try
170+ HTTPS := TFPHTTPClient.Create(nil );
171+ TrackerListToBeSend := TStringList.Create;
172+ TrackerListToBeSend.Assign(TrackerList);
173+
174+ // remove all duplicate trackers before sending,
175+ RemoveTrackersFromList(TrackerList_All, TrackerListToBeSend);
176+
177+ // Give information back about how many unique tracker URL is send.
178+ TrackersSendCount := TrackerListToBeSend.Count;
179+
180+ if TrackersSendCount > 0 then
181+ begin
182+
183+ // this is the 'key'
184+ FormData := ' new_trackers=' ;
185+
186+ // This is the 'values' all seperated with one space '+'
187+ Seperator := ' ' ;
188+ for Trackers in TrackerListToBeSend do
189+ begin
190+ FormData := FormData + Seperator + HTTPEncode(Trackers);
191+ if Seperator = ' ' then
192+ Seperator := ' +' ;
193+ end ;
194+
195+ try
196+ HTTPS.FormPost(URL_POST, FormData);
197+
198+ // Check the response must be 204
199+ Result := HTTPS.ResponseStatusCode = 204 ;
200+ except
201+ // No OpenSSL or web server is down
202+ Result := False;
203+ end ;
204+ end ;
205+
206+ finally
207+ TrackerListToBeSend.Free;
208+ HTTPS.Free;
209+ end ;
210+ end ;
124211end ;
125212
126213constructor TNewTrackon.Create;
0 commit comments