Skip to content

Commit 4ee4b45

Browse files
[Issue GerryFerdinandus#23] Upload trackers to newTrackon
All the trackers in 'Present trackers in all torrent files.' will be uploaded. This will reduce the manual labour of copy and paste to newTrackon.
1 parent 2352459 commit 4ee4b45

File tree

4 files changed

+165
-33
lines changed

4 files changed

+165
-33
lines changed

source/code/controler_trackerlist_online.pas

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface
1212

1313
type
1414

15-
TDefaultChecked = function(const TrackerURL: UTF8String): Boolean of object;
15+
TDefaultChecked = function(const TrackerURL: UTF8String): boolean of object;
1616

1717
{ TControlerTrackerListOnline }
1818

@@ -45,9 +45,14 @@ TControlerTrackerListOnline = class
4545
//must be called if the tracker list is updated
4646
procedure UpdateView;
4747

48-
function DownloadTrackers: boolean;
48+
function DownloadTrackers_All_Live_Stable: boolean;
4949

50-
constructor Create(StringGridTorrentURL: TStringGrid; TrackerList: TStringList; DefaultChecked: TDefaultChecked);
50+
//Submit tracker to newTrackon via http POST
51+
function SubmitTrackers(TrackerList: TStringList;
52+
out TrackersSendCount: integer): boolean;
53+
54+
constructor Create(StringGridTorrentURL: TStringGrid; TrackerList: TStringList;
55+
DefaultChecked: TDefaultChecked);
5156
destructor Destroy; override;
5257
end;
5358

@@ -59,13 +64,19 @@ implementation
5964
{ TControlerTrackerListOnline }
6065

6166

62-
function TControlerTrackerListOnline.DownloadTrackers: boolean;
67+
function TControlerTrackerListOnline.DownloadTrackers_All_Live_Stable: boolean;
6368
begin
64-
Result := FNewTrackon.DownloadTrackers;
69+
Result := FNewTrackon.Download_All_Live_Stable;
6570
UpdateView;
6671
ShowTrackerStatus(Result);
6772
end;
6873

74+
function TControlerTrackerListOnline.SubmitTrackers(TrackerList: TStringList;
75+
out TrackersSendCount: integer): boolean;
76+
begin
77+
Result := FNewTrackon.SubmitTrackers(TrackerList, TrackersSendCount);
78+
end;
79+
6980
constructor TControlerTrackerListOnline.Create(StringGridTorrentURL: TStringGrid;
7081
TrackerList: TStringList; DefaultChecked: TDefaultChecked);
7182
begin
@@ -177,7 +188,8 @@ procedure TControlerTrackerListOnline.UpdateView;
177188
//Show the TrackerList list in string grid view
178189
for tracker in FTrackerList do
179190
begin
180-
AppendRow(FDefaultChecked(tracker), FTrackerListOnline.TrackerStatus(tracker), tracker);
191+
AppendRow(FDefaultChecked(tracker), FTrackerListOnline.TrackerStatus(
192+
tracker), tracker);
181193
end;
182194

183195
//make sure all text are fit inside the columns

source/code/main.lfm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ object FormTrackerModify: TFormTrackerModify
375375
Caption = 'Append Stable Trackers (From newTrackon)'
376376
OnClick = MenuItemOnlineCheckAppendStableTrackersClick
377377
end
378+
object MenuItemOnlineCheckSubmitNewTrackon: TMenuItem
379+
Caption = 'Upload trackers to newTrackon'
380+
OnClick = MenuItemOnlineCheckSubmitNewTrackonClick
381+
end
378382
end
379383
object MenuHelp: TMenuItem
380384
Caption = '&Help'

source/code/main.pas

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ TFormTrackerModify = class(TForm)
3939
MenuFileTorrentFolder: TMenuItem;
4040
MenuFileOpenTrackerList: TMenuItem;
4141
MenuHelpReportingIssue: TMenuItem;
42+
MenuItemOnlineCheckSubmitNewTrackon: TMenuItem;
4243
MenuItemOnlineCheckAppendStableTrackers: TMenuItem;
4344
MenuTrackersDeleteDeadTrackers: TMenuItem;
4445
MenuTrackersDeleteUnstableTrackers: TMenuItem;
@@ -95,6 +96,7 @@ TFormTrackerModify = class(TForm)
9596
procedure FormShow(Sender: TObject);
9697
procedure MenuHelpReportingIssueClick(Sender: TObject);
9798
procedure MenuHelpVisitWebsiteClick(Sender: TObject);
99+
procedure MenuItemOnlineCheckSubmitNewTrackonClick(Sender: TObject);
98100

99101
//Popup menu in treeview show all/hide all/ individual items selection.
100102
procedure MenuItemTorrentFilesTreeShowAllClick(Sender: TObject);
@@ -328,6 +330,33 @@ procedure TFormTrackerModify.MenuHelpVisitWebsiteClick(Sender: TObject);
328330
OpenURL('https://github.com/GerryFerdinandus/bittorrent-tracker-editor');
329331
end;
330332

333+
procedure TFormTrackerModify.MenuItemOnlineCheckSubmitNewTrackonClick(Sender: TObject);
334+
var
335+
SendStatus: boolean;
336+
TrackerSendCount: integer;
337+
PopupStr: string;
338+
begin
339+
340+
SendStatus := FControlerTrackerListOnline.SubmitTrackers(
341+
FTrackerList.TrackerFromInsideTorrentFilesList, TrackerSendCount);
342+
343+
if SendStatus then
344+
begin
345+
//Succesful upload
346+
PopupStr := format('Successful upload of %d unique tracker URL',[TrackerSendCount]);
347+
Application.MessageBox(
348+
PChar(@PopupStr[1]),
349+
'', MB_ICONINFORMATION + MB_OK);
350+
end
351+
else
352+
begin
353+
//something is wrong with uploading
354+
Application.MessageBox(
355+
PChar('Can not uploading the tracker list'),
356+
'', MB_ICONINFORMATION + MB_OK);
357+
end;
358+
end;
359+
331360
procedure TFormTrackerModify.MenuItemOnlineCheckAppendStableTrackersClick(
332361
Sender: TObject);
333362
var
@@ -362,7 +391,7 @@ procedure TFormTrackerModify.MenuItemOnlineCheckDownloadNewTrackonClick(
362391
begin
363392
try
364393
screen.Cursor := crHourGlass;
365-
FDownloadStatus := FControlerTrackerListOnline.DownloadTrackers;
394+
FDownloadStatus := FControlerTrackerListOnline.DownloadTrackers_All_Live_Stable;
366395
finally
367396
screen.Cursor := crDefault;
368397
end;

source/code/newtrackon.pas

Lines changed: 113 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6671
implementation
6772

68-
uses fphttpclient, LazUTF8, torrent_miscellaneous;
73+
uses fphttpclient, LazUTF8, torrent_miscellaneous, httpdefs;
6974

7075
const
7176
URL: array [TNewTrackon_List] of string =
@@ -80,47 +85,129 @@ implementation
8085

8186
{ TNewTrackon }
8287

88+
8389
procedure TNewTrackon.CreateTrackerList_Dead;
8490
begin
8591
//FTrackerList_Dead = FTrackerList_All - FTrackerList_Live;
8692
TrackerList_Dead.Assign(TrackerList_All);
8793
RemoveTrackersFromList(TrackerList_Live, TrackerList_Dead);
8894
end;
8995

90-
procedure TNewTrackon.DownloadTracker(NewTrackon_List: TNewTrackon_List);
96+
function TNewTrackon.DownloadTracker(NewTrackon_List: TNewTrackon_List): boolean;
9197
begin
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]);
102117
end;
103118

104-
function TNewTrackon.DownloadTrackers: boolean;
119+
function TNewTrackon.DownloadEverything: boolean;
105120
var
106121
i: TNewTrackon_List;
107122
begin
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;
124211
end;
125212

126213
constructor TNewTrackon.Create;

0 commit comments

Comments
 (0)