@@ -209,7 +209,28 @@ def _get_items_torrent(output_path: Path) -> Path:
209
209
print ("Starting BitTorrent download..." )
210
210
# Initialize libtorrent session
211
211
session = lt .session ()
212
- session .listen_on (6881 , 6891 )
212
+
213
+ # Enhanced session settings for better peer discovery
214
+ settings = {
215
+ "enable_dht" : True , # Enable DHT for trackerless torrents
216
+ "enable_lsd" : True , # Enable Local Service Discovery
217
+ "enable_upnp" : True , # Enable UPnP port mapping
218
+ "enable_natpmp" : True , # Enable NAT-PMP port mapping
219
+ "announce_to_all_trackers" : True , # Announce to all trackers
220
+ "announce_to_all_tiers" : True , # Announce to all tiers
221
+ }
222
+ session .apply_settings (settings )
223
+
224
+ # Set ports
225
+ port_ranges = [(6881 , 6891 ), (6901 , 6911 ), (51413 , 51423 )]
226
+ for start_port , end_port in port_ranges :
227
+ try :
228
+ session .listen_on (start_port , end_port )
229
+ print (f"✓ Listening on ports { start_port } -{ end_port } " )
230
+ break
231
+ except Exception as e :
232
+ print (f"✗ Failed to listen on ports { start_port } -{ end_port } : { e } " )
233
+ continue
213
234
214
235
# Load torrent info directly from memory
215
236
torrent_info = lt .torrent_info (torrent_data )
@@ -225,17 +246,28 @@ def _get_items_torrent(output_path: Path) -> Path:
225
246
print (f"✓ Total size: { torrent_info .total_size () / (1024 * 1024 ):.1f} MB" )
226
247
print (f"✓ Number of files: { torrent_info .num_files ()} " )
227
248
228
- # Monitor download progress
229
- print ("\n Downloading content..." )
249
+ # Enhanced monitoring with timeout
250
+ print ("\n Searching for peers and downloading..." )
251
+ start_time = time .time ()
252
+ timeout_minutes = 30 # Set timeout in minutes
253
+ timeout_seconds = timeout_minutes * 60
254
+
230
255
while not handle .is_seed ():
231
256
status = handle .status ()
257
+ elapsed_time = time .time () - start_time
258
+
259
+ # Check for timeout
260
+ if elapsed_time > timeout_seconds :
261
+ print (f"\n ✗ Timeout after { timeout_minutes } minutes" )
262
+ raise TimeoutError (f"Download timed out after { timeout_minutes } minutes" )
232
263
233
264
progress_info = {
234
265
"Progress" : f"{ status .progress * 100 :.1f} %" ,
235
266
"Download Rate" : f"{ status .download_rate / 1024 :.1f} KB/s" ,
236
267
"Upload Rate" : f"{ status .upload_rate / 1024 :.1f} KB/s" ,
237
268
"Peers" : status .num_peers ,
238
269
"Seeds" : status .num_seeds ,
270
+ "Elapsed" : f"{ elapsed_time / 60 :.1f} m" ,
239
271
}
240
272
241
273
# Clear line and print progress
@@ -245,6 +277,7 @@ def _get_items_torrent(output_path: Path) -> Path:
245
277
f"↑{ progress_info ['Upload Rate' ]} | "
246
278
f"Peers: { progress_info ['Peers' ]} | "
247
279
f"Seeds: { progress_info ['Seeds' ]} " ,
280
+ f"Time: { progress_info ['Elapsed' ]} " ,
248
281
end = "\n " ,
249
282
)
250
283
0 commit comments