amri12310 commited on
Commit
288e522
·
verified ·
1 Parent(s): e5dcb7d

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +27 -28
main.py CHANGED
@@ -13,60 +13,51 @@ TOKEN = os.environ.get("HF_TOKEN")
13
  active_downloads = set()
14
 
15
  def get_live_trackers():
16
- """Scrapes the top 100 live trackers to bypass the HF Firewall"""
17
  try:
18
  r = requests.get("https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt", timeout=5)
19
- trackers = [t for t in r.text.split('\n') if t.strip()]
20
- return ",".join(trackers)
21
  except:
22
- # Fallback to standard trackers if GitHub is unreachable
23
- return "udp://tracker.opentrackr.org:1337/announce,udp://tracker.openbittorrent.com:6969/announce,udp://exodus.desync.com:6969/announce"
24
 
25
  def download_and_vault(unique_id, magnet):
26
  if unique_id in active_downloads: return
27
  active_downloads.add(unique_id)
28
 
29
- print(f"[ENGINE] Fetching live tracker list to bypass HF Firewall...", flush=True)
30
  live_trackers = get_live_trackers()
31
 
32
  print(f"[ENGINE] Max Speed Download Started for {unique_id}", flush=True)
33
-
34
- # 1. FIREWALL-BYPASS DOWNLOADER
35
  subprocess.run([
36
  'aria2c', '--seed-time=0', '--allow-overwrite=true',
37
- '--bt-tracker=' + live_trackers, # Injects 100+ live trackers
38
- '--enable-peer-exchange=true', # Forces peers to share other peers' IPs
39
- '--bt-max-peers=0', # Infinite connections
40
- '--bt-request-peer-speed-limit=0',
41
- '--file-allocation=falloc',
42
- '--disk-cache=256M',
43
  magnet
44
  ])
45
 
46
- # 2. SLICE AND VAULT UPLOAD
47
  video_files = glob.glob('**/*.mkv', recursive=True) + glob.glob('**/*.mp4', recursive=True)
48
  if video_files:
49
  target_file = max(video_files, key=os.path.getsize)
50
  print(f"[VAULTING] Moving {unique_id} to permanent storage...", flush=True)
51
 
52
- chunk_size = 10 * 1024 * 1024 * 1024 # 10GB
53
  with open(target_file, 'rb') as f_in:
54
  part_num = 0
55
  while True:
56
  chunk = f_in.read(chunk_size)
57
  if not chunk: break
58
-
59
  suffix = chr(97 + (part_num // 26)) + chr(97 + (part_num % 26))
60
  part_name = f"{unique_id}_part_{suffix}"
61
-
62
  with open(part_name, 'wb') as f_out: f_out.write(chunk)
 
63
  print(f"Uploading {part_name}...", flush=True)
64
  api.upload_file(path_or_fileobj=part_name, path_in_repo=part_name, repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
65
  os.remove(part_name)
66
  part_num += 1
67
 
68
- # Wait a bit before deleting so active streams don't cut off
69
- time.sleep(1800)
70
  os.remove(target_file)
71
 
72
  active_downloads.remove(unique_id)
@@ -76,21 +67,29 @@ def download_and_vault(unique_id, magnet):
76
  def download_torrent():
77
  magnet = request.args.get('magnet')
78
  unique_id = request.args.get('id')
79
-
80
- # Start download in a background thread
81
  threading.Thread(target=download_and_vault, args=(unique_id, magnet)).start()
82
  return "Started", 200
83
 
84
  @app.route('/live/<unique_id>')
85
  def live_stream(unique_id):
86
  """Serves the file to Stremio while it is actively downloading"""
87
- video_files = glob.glob('**/*.mkv', recursive=True) + glob.glob('**/*.mp4', recursive=True)
88
- if not video_files: return "Buffering...", 404
89
 
90
- target_file = max(video_files, key=os.path.getsize)
 
 
 
 
 
 
 
 
 
 
 
91
  file_size = os.path.getsize(target_file)
92
-
93
  range_header = request.headers.get('Range', None)
 
94
  if not range_header:
95
  return send_file(target_file, mimetype='video/mp4', accept_ranges=True)
96
 
@@ -100,10 +99,10 @@ def live_stream(unique_id):
100
  with open(target_file, 'rb') as f:
101
  f.seek(byte_start)
102
  while True:
103
- chunk = f.read(1024*1024) # 1MB chunks
104
  if not chunk:
105
  if os.path.exists(f"{target_file}.aria2"):
106
- time.sleep(1) # Wait for more data
107
  continue
108
  break
109
  yield chunk
 
13
  active_downloads = set()
14
 
15
  def get_live_trackers():
16
+ """Scrapes top 100 live trackers to bypass HF firewall and find all seeders"""
17
  try:
18
  r = requests.get("https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt", timeout=5)
19
+ return ",".join([t for t in r.text.split('\n') if t.strip()])
 
20
  except:
21
+ return "udp://tracker.opentrackr.org:1337/announce,udp://tracker.openbittorrent.com:6969/announce"
 
22
 
23
  def download_and_vault(unique_id, magnet):
24
  if unique_id in active_downloads: return
25
  active_downloads.add(unique_id)
26
 
27
+ print(f"[ENGINE] Fetching live tracker list...", flush=True)
28
  live_trackers = get_live_trackers()
29
 
30
  print(f"[ENGINE] Max Speed Download Started for {unique_id}", flush=True)
 
 
31
  subprocess.run([
32
  'aria2c', '--seed-time=0', '--allow-overwrite=true',
33
+ '--bt-tracker=' + live_trackers,
34
+ '--enable-peer-exchange=true', '--bt-max-peers=0',
35
+ '--max-connection-per-server=16', '--split=32',
36
+ '--disk-cache=256M', '--file-allocation=falloc',
 
 
37
  magnet
38
  ])
39
 
 
40
  video_files = glob.glob('**/*.mkv', recursive=True) + glob.glob('**/*.mp4', recursive=True)
41
  if video_files:
42
  target_file = max(video_files, key=os.path.getsize)
43
  print(f"[VAULTING] Moving {unique_id} to permanent storage...", flush=True)
44
 
45
+ chunk_size = 10 * 1024 * 1024 * 1024
46
  with open(target_file, 'rb') as f_in:
47
  part_num = 0
48
  while True:
49
  chunk = f_in.read(chunk_size)
50
  if not chunk: break
 
51
  suffix = chr(97 + (part_num // 26)) + chr(97 + (part_num % 26))
52
  part_name = f"{unique_id}_part_{suffix}"
 
53
  with open(part_name, 'wb') as f_out: f_out.write(chunk)
54
+
55
  print(f"Uploading {part_name}...", flush=True)
56
  api.upload_file(path_or_fileobj=part_name, path_in_repo=part_name, repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
57
  os.remove(part_name)
58
  part_num += 1
59
 
60
+ time.sleep(1800) # Keep local file alive for active watchers (30 mins)
 
61
  os.remove(target_file)
62
 
63
  active_downloads.remove(unique_id)
 
67
  def download_torrent():
68
  magnet = request.args.get('magnet')
69
  unique_id = request.args.get('id')
70
+ # Run in background so it doesn't block the server
 
71
  threading.Thread(target=download_and_vault, args=(unique_id, magnet)).start()
72
  return "Started", 200
73
 
74
  @app.route('/live/<unique_id>')
75
  def live_stream(unique_id):
76
  """Serves the file to Stremio while it is actively downloading"""
 
 
77
 
78
+ # --- THE FIX: WAIT FOR FILE INSTEAD OF CRASHING ---
79
+ target_file = None
80
+ for _ in range(120): # Wait up to 2 minutes for Aria2c to create the file
81
+ video_files = glob.glob('**/*.mkv', recursive=True) + glob.glob('**/*.mp4', recursive=True)
82
+ if video_files:
83
+ target_file = max(video_files, key=os.path.getsize)
84
+ break
85
+ time.sleep(1) # Tell Stremio to keep showing the loading spinner
86
+
87
+ if not target_file:
88
+ return "Timeout finding file. Torrent might have 0 seeders.", 404
89
+
90
  file_size = os.path.getsize(target_file)
 
91
  range_header = request.headers.get('Range', None)
92
+
93
  if not range_header:
94
  return send_file(target_file, mimetype='video/mp4', accept_ranges=True)
95
 
 
99
  with open(target_file, 'rb') as f:
100
  f.seek(byte_start)
101
  while True:
102
+ chunk = f.read(1024*1024)
103
  if not chunk:
104
  if os.path.exists(f"{target_file}.aria2"):
105
+ time.sleep(1) # Buffer underrun, wait for download to catch up
106
  continue
107
  break
108
  yield chunk