Skip to content

Commit 4d1f8a5

Browse files
committed
More torrent options
1 parent 491c492 commit 4d1f8a5

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

newshomepages/extract/consolidate.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,28 @@ def _get_items_torrent(output_path: Path) -> Path:
209209
print("Starting BitTorrent download...")
210210
# Initialize libtorrent session
211211
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
213234

214235
# Load torrent info directly from memory
215236
torrent_info = lt.torrent_info(torrent_data)
@@ -225,17 +246,28 @@ def _get_items_torrent(output_path: Path) -> Path:
225246
print(f"✓ Total size: {torrent_info.total_size() / (1024*1024):.1f} MB")
226247
print(f"✓ Number of files: {torrent_info.num_files()}")
227248

228-
# Monitor download progress
229-
print("\nDownloading content...")
249+
# Enhanced monitoring with timeout
250+
print("\nSearching for peers and downloading...")
251+
start_time = time.time()
252+
timeout_minutes = 30 # Set timeout in minutes
253+
timeout_seconds = timeout_minutes * 60
254+
230255
while not handle.is_seed():
231256
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")
232263

233264
progress_info = {
234265
"Progress": f"{status.progress * 100:.1f}%",
235266
"Download Rate": f"{status.download_rate / 1024:.1f} KB/s",
236267
"Upload Rate": f"{status.upload_rate / 1024:.1f} KB/s",
237268
"Peers": status.num_peers,
238269
"Seeds": status.num_seeds,
270+
"Elapsed": f"{elapsed_time/60:.1f}m",
239271
}
240272

241273
# Clear line and print progress
@@ -245,6 +277,7 @@ def _get_items_torrent(output_path: Path) -> Path:
245277
f"↑{progress_info['Upload Rate']} | "
246278
f"Peers: {progress_info['Peers']} | "
247279
f"Seeds: {progress_info['Seeds']}",
280+
f"Time: {progress_info['Elapsed']}",
248281
end="\n",
249282
)
250283

0 commit comments

Comments
 (0)