|
| 1 | +unit torrent_miscellaneous; |
| 2 | + |
| 3 | +{ |
| 4 | + Some generic routine |
| 5 | +
|
| 6 | +} |
| 7 | +{$mode objfpc}{$H+} |
| 8 | + |
| 9 | +interface |
| 10 | + |
| 11 | +uses |
| 12 | + Classes, SysUtils; |
| 13 | + |
| 14 | +type |
| 15 | + //Updated torrent file trackers list order. |
| 16 | + TTrackerListOrder = ( |
| 17 | + |
| 18 | + // Console parameter: -U0 |
| 19 | + // Insert new trackers list BEFORE, the original trackers list inside the torrent file. |
| 20 | + // And remove possible duplicated trackers from the ORIGINAL trackers list. |
| 21 | + tloInsertNewBeforeAndKeepNewIntact = 0, |
| 22 | + |
| 23 | + // Console parameter: -U1 |
| 24 | + // Insert new trackers list BEFORE, the original trackers list inside the torrent file. |
| 25 | + // And remove possible duplicated trackers from the NEW trackers list. |
| 26 | + tloInsertNewBeforeAndKeepOriginalIntact, |
| 27 | + |
| 28 | + // Console parameter: -U2 |
| 29 | + // Append new trackers list AFTER, the original trackers list inside the torrent file. |
| 30 | + // And remove possible duplicated trackers from the ORIGINAL trackers list. |
| 31 | + tloAppendNewAfterAndKeepNewIntact, |
| 32 | + |
| 33 | + // Console parameter: -U3 |
| 34 | + // Append new trackers list AFTER, the original trackers list inside the torrent file. |
| 35 | + // And remove possible duplicated trackers from the NEW trackers list. |
| 36 | + tloAppendNewAfterAndKeepOriginalIntact, |
| 37 | + |
| 38 | + // Console parameter: -U4 |
| 39 | + // Sort the trackers list by name. |
| 40 | + tloSort, |
| 41 | + |
| 42 | + // Console parameter: -U5 |
| 43 | + // Append new trackers list BEFORE, the original trackers list inside the torrent file. |
| 44 | + // Keep original tracker list 'of each individual torrent' unchanged and remove nothing. |
| 45 | + // Every torent may have diferent tracker list! |
| 46 | + tloInsertNewBeforeAndKeepOriginalIntactAndRemoveNothing, |
| 47 | + |
| 48 | + // Console parameter: -U6 |
| 49 | + // Append new trackers list AFTER, the original trackers list inside the torrent file. |
| 50 | + // Keep original tracker list 'of each individual torrent' unchanged and remove nothing. |
| 51 | + // Every torent may have diferent tracker list! |
| 52 | + tloAppendNewAfterAndKeepOriginalIntactAndRemoveNothing, |
| 53 | + |
| 54 | + // Console parameter: -U7 |
| 55 | + // Randomize the trackers list. |
| 56 | + tloRandomize |
| 57 | + |
| 58 | + ); |
| 59 | + |
| 60 | +procedure RemoveTrackersFromList(RemoveList, UpdatedList: TStringList); |
| 61 | +procedure SanatizeTrackerList(StringList: TStringList); |
| 62 | +procedure RandomizeTrackerList(StringList: TStringList); |
| 63 | +procedure AddButIngnoreDuplicates(StringList: TStringList; const Str: UTF8String); |
| 64 | +function ByteSizeToBiggerSizeFormatStr(ByteSize: int64): string; |
| 65 | +function LoadTorrentViaDir(const Dir: UTF8String; |
| 66 | + TorrentFilesNameStringList: TStringList): boolean; |
| 67 | + |
| 68 | +const |
| 69 | + VALID_TRACKERS_URL: array[0..4] of UTF8String = |
| 70 | + ( |
| 71 | + 'udp://', |
| 72 | + 'http://', |
| 73 | + 'https://', |
| 74 | + 'ws://', |
| 75 | + 'wss://' |
| 76 | + ); |
| 77 | + |
| 78 | + |
| 79 | +implementation |
| 80 | + |
| 81 | +uses LazUTF8, LazFileUtils; |
| 82 | + |
| 83 | +procedure RemoveTrackersFromList(RemoveList, UpdatedList: TStringList); |
| 84 | +var |
| 85 | + TrackerStr: string; |
| 86 | + i: integer; |
| 87 | +begin |
| 88 | + //Remove the trackers that we do not want in the list |
| 89 | + for TrackerStr in RemoveList do |
| 90 | + begin |
| 91 | + //Find the tracker and remove it from the list. |
| 92 | + i := UpdatedList.IndexOf(UTF8Trim(TrackerStr)); |
| 93 | + if i >= 0 then |
| 94 | + UpdatedList.Delete(i); |
| 95 | + end; |
| 96 | +end; |
| 97 | + |
| 98 | +procedure SanatizeTrackerList(StringList: TStringList); |
| 99 | +var |
| 100 | + TrackerStr: UTF8String; |
| 101 | + i: integer; |
| 102 | + PositionSpace: PtrInt; |
| 103 | +begin |
| 104 | + //remove all empty space and comment after the URL |
| 105 | + |
| 106 | + if StringList.Count > 0 then |
| 107 | + begin |
| 108 | + for i := 0 to StringList.Count - 1 do |
| 109 | + begin |
| 110 | + //process every line one by one |
| 111 | + TrackerStr := StringList[i]; |
| 112 | + |
| 113 | + //remove empty spaces at the begin/end of line |
| 114 | + TrackerStr := UTF8Trim(TrackerStr); |
| 115 | + |
| 116 | + //find the first 'space' found in line |
| 117 | + PositionSpace := UTF8Pos(' ', TrackerStr); |
| 118 | + if PositionSpace > 0 then |
| 119 | + begin |
| 120 | + // There is a 'space' found |
| 121 | + // Remove everything after this 'space' |
| 122 | + TrackerStr := UTF8LeftStr(TrackerStr, PositionSpace - 1); |
| 123 | + end; |
| 124 | + |
| 125 | + //write the modified string back |
| 126 | + StringList[i] := TrackerStr; |
| 127 | + end; |
| 128 | + end; |
| 129 | +end; |
| 130 | + |
| 131 | +procedure RandomizeTrackerList(StringList: TStringList); |
| 132 | +var |
| 133 | + i: integer; |
| 134 | +begin |
| 135 | + //The order of the string list must be randomize |
| 136 | + if StringList.Count > 1 then |
| 137 | + begin |
| 138 | + Randomize; |
| 139 | + for i := 0 to StringList.Count - 1 do |
| 140 | + begin |
| 141 | + StringList.Exchange(i, Random(StringList.Count)); |
| 142 | + end; |
| 143 | + end; |
| 144 | +end; |
| 145 | + |
| 146 | +procedure AddButIngnoreDuplicates(StringList: TStringList; const Str: UTF8String); |
| 147 | +begin |
| 148 | + //Stringlist that are not sorted must use IndexOf to ignore Duplicates. |
| 149 | + if not StringList.Sorted then |
| 150 | + begin |
| 151 | + //not sorted version |
| 152 | + if StringList.IndexOf(Str) < 0 then |
| 153 | + begin |
| 154 | + StringList.add(Str); |
| 155 | + end; |
| 156 | + end |
| 157 | + else |
| 158 | + begin |
| 159 | + //sorted version |
| 160 | + StringList.add(Str); |
| 161 | + end; |
| 162 | + |
| 163 | +end; |
| 164 | + |
| 165 | +function ByteSizeToBiggerSizeFormatStr(ByteSize: int64): string; |
| 166 | +begin |
| 167 | + if ByteSize >= (1024 * 1024 * 1024) then |
| 168 | + Result := Format('%0.2f GiB', [ByteSize / (1024 * 1024 * 1024)]) |
| 169 | + else |
| 170 | + if ByteSize >= (1024 * 1024) then |
| 171 | + Result := Format('%0.2f MiB', [ByteSize / (1024 * 1024)]) |
| 172 | + else |
| 173 | + if ByteSize >= (1024) then |
| 174 | + Result := Format('%0.2f KiB', [ByteSize / 1024]); |
| 175 | + Result := Result + Format(' (%d Bytes)', [ByteSize]); |
| 176 | +end; |
| 177 | + |
| 178 | + |
| 179 | +function LoadTorrentViaDir(const Dir: UTF8String; |
| 180 | + TorrentFilesNameStringList: TStringList): boolean; |
| 181 | +var |
| 182 | + Info: TSearchRec; |
| 183 | +begin |
| 184 | + //place all the torrent file name in TorrentFilesNameStringList |
| 185 | + // TorrentFilesNameStringList := TStringList.Create; |
| 186 | + |
| 187 | + if FindFirstUTF8(dir + PathDelim + '*.torrent', faAnyFile, Info) = 0 then |
| 188 | + begin |
| 189 | + //Read all the torrent files inside this dir. |
| 190 | + repeat |
| 191 | + TorrentFilesNameStringList.Add(UTF8Trim(dir + PathDelim + Info.Name)); |
| 192 | + until FindNextUTF8(info) <> 0; |
| 193 | + end; |
| 194 | + FindCloseUTF8(Info); |
| 195 | + |
| 196 | + Result := TorrentFilesNameStringList.Count > 0; |
| 197 | + |
| 198 | +end; |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | +function DecodeConsoleUpdateParameter(ConsoleUpdateParameter: UTF8String; |
| 203 | + var TrackerListOrder: TTrackerListOrder; LogStringList: TStringList = nil): boolean; |
| 204 | +var |
| 205 | + i: integer; |
| 206 | +begin |
| 207 | + //Decode the '-Ux' x is number [0..4] |
| 208 | + |
| 209 | + //verify string content. |
| 210 | + Result := (Pos('-U', ConsoleUpdateParameter) = 1) and |
| 211 | + (length(ConsoleUpdateParameter) = 3); |
| 212 | + |
| 213 | + if Result then |
| 214 | + begin |
| 215 | + //get the number |
| 216 | + Result := TryStrToInt(ConsoleUpdateParameter[3], i); |
| 217 | + if Result then |
| 218 | + begin |
| 219 | + //decode number [0..7] |
| 220 | + case i of |
| 221 | + 0: TrackerListOrder := tloInsertNewBeforeAndKeepNewIntact; |
| 222 | + 1: TrackerListOrder := tloInsertNewBeforeAndKeepOriginalIntact; |
| 223 | + 2: TrackerListOrder := tloAppendNewAfterAndKeepNewIntact; |
| 224 | + 3: TrackerListOrder := tloAppendNewAfterAndKeepOriginalIntact; |
| 225 | + 4: TrackerListOrder := tloSort; |
| 226 | + 5: TrackerListOrder := tloInsertNewBeforeAndKeepOriginalIntactAndRemoveNothing; |
| 227 | + 6: TrackerListOrder := tloAppendNewAfterAndKeepOriginalIntactAndRemoveNothing; |
| 228 | + 7: TrackerListOrder := tloRandomize; |
| 229 | + else |
| 230 | + begin |
| 231 | + //the number is out of range. |
| 232 | + Result := False; |
| 233 | + end; |
| 234 | + end; |
| 235 | + end; |
| 236 | + end; |
| 237 | + |
| 238 | + if not Result then |
| 239 | + begin |
| 240 | + if assigned(LogStringList) then |
| 241 | + begin |
| 242 | + LogStringList.Add('ERROR: can not decode update parameter -U : ' + |
| 243 | + ConsoleUpdateParameter); |
| 244 | + end; |
| 245 | + |
| 246 | + end; |
| 247 | +end; |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | +function ConsoleModeDecodeParameter(out FileNameOrDirStr: UTF8String; |
| 253 | + out CountParameter: integer; var TrackerListOrder: TTrackerListOrder; |
| 254 | + LogStringList: TStringList = nil): boolean; |
| 255 | +begin |
| 256 | + |
| 257 | + // Console mode can be started with 2 parameter |
| 258 | + // Update methode: -U0 , -U1, -U2, -U3, -U4 |
| 259 | + // String with a link to folder or to torrent file. 'C:\dir' |
| 260 | + |
| 261 | + CountParameter := Paramcount; |
| 262 | + case CountParameter of |
| 263 | + 0: |
| 264 | + begin |
| 265 | + if assigned(LogStringList) then |
| 266 | + begin |
| 267 | + LogStringList.Add('ERROR: There are no parameter detected.'); |
| 268 | + end; |
| 269 | + Result := False; |
| 270 | + exit; |
| 271 | + end; |
| 272 | + 1: |
| 273 | + begin |
| 274 | + //one parameter. Must be a link. |
| 275 | + FileNameOrDirStr := UTF8Trim(ParamStr(1)); |
| 276 | + //Keep the same behaviour as the previeus software version. |
| 277 | + // FTrackerListOrderForUpdatedTorrent := tloSort; |
| 278 | + TrackerListOrder := tloSort; |
| 279 | + Result := True; |
| 280 | + end; |
| 281 | + 2: |
| 282 | + begin |
| 283 | + //Two parameters. The user can select the update methode. |
| 284 | + //Check for '-U' contruction as first parameter |
| 285 | + if (Pos('-U', ParamStr(1)) = 1) then |
| 286 | + begin |
| 287 | + //Update parameter is the first parameter |
| 288 | + Result := DecodeConsoleUpdateParameter(ParamStr(1), TrackerListOrder, |
| 289 | + LogStringList); |
| 290 | + FileNameOrDirStr := UTF8Trim(ParamStr(2)); |
| 291 | + end |
| 292 | + else |
| 293 | + begin |
| 294 | + //Update parameter is the second parameter |
| 295 | + Result := DecodeConsoleUpdateParameter(ParamStr(2), TrackerListOrder, |
| 296 | + LogStringList); |
| 297 | + FileNameOrDirStr := UTF8Trim(ParamStr(1)); |
| 298 | + end; |
| 299 | + |
| 300 | + end; |
| 301 | + else |
| 302 | + begin |
| 303 | + if assigned(LogStringList) then |
| 304 | + begin |
| 305 | + LogStringList.Add('ERROR: There can only be maximum of 2 parameter. Not: ' + |
| 306 | + IntToStr(ParamCount)); |
| 307 | + end; |
| 308 | + end; |
| 309 | + Result := False; |
| 310 | + exit; |
| 311 | + end; |
| 312 | + |
| 313 | +end; |
| 314 | + |
| 315 | + |
| 316 | + |
| 317 | + |
| 318 | + |
| 319 | + |
| 320 | +end. |
0 commit comments