amri12310 commited on
Commit
ad5d72e
Β·
verified Β·
1 Parent(s): 367c8ab

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +85 -25
main.py CHANGED
@@ -11,32 +11,41 @@ TOKEN = os.environ.get("HF_TOKEN")
11
  CHUNK_SIZE = 100 * 1024 * 1024 # 100MB
12
 
13
  def get_super_trackers():
 
14
  try:
15
- # Fetching from two reliable sources
16
- t1 = requests.get("https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt", timeout=5).text
17
- t2 = requests.get("https://newtrackon.com/api/stable", timeout=5).text
18
- combined = list(set([t.strip() for t in (t1 + t2).split('\n') if t.strip()]))
19
- return ",".join(combined)
 
 
 
 
 
 
20
  except:
21
  return "udp://tracker.opentrackr.org:1337/announce,udp://tracker.internetwarriors.net:1337/announce"
22
 
23
  def do_archive(u_id, magnet):
24
  archive_dir = f"/app/temp_{u_id}"
25
  os.makedirs(archive_dir, exist_ok=True)
26
- print(f"[ENGINE] Attempting to resolve: {u_id}", flush=True)
27
 
 
28
  cmd = [
29
  'aria2c', '--dir', archive_dir,
30
  '--bt-tracker=' + get_super_trackers(),
31
  '--enable-dht=true', '--bt-enable-lpd=true', '--enable-peer-exchange=true',
32
  '--bt-force-encryption=true', '--bt-min-crypto-level=arc4',
33
- '--bt-max-peers=0', '--split=64', # Increased split for speed
34
- '--bt-metadata-only=false',
35
- '--bt-save-metadata=false',
36
  '--file-allocation=falloc',
37
  '--seed-time=0',
38
- '--bt-stop-timeout=300', # Stop if no progress for 5 mins
39
  '--summary-interval=10',
 
40
  magnet
41
  ]
42
 
@@ -47,39 +56,90 @@ def do_archive(u_id, magnet):
47
  glob.glob(f'{archive_dir}/**/*.mp4', recursive=True)
48
 
49
  if not media_files:
50
- print(f"[ENGINE] ❌ Failed to find media or resolve metadata for {u_id}", flush=True)
51
  return
52
 
53
- # (Rest of the fuzzy matching and upload logic remains the same)
54
- video = max(media_files, key=os.path.getsize)
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  file_size = os.path.getsize(video)
56
-
57
- print(f"[ENGINE] Found file: {os.path.basename(video)}. Starting Upload.", flush=True)
 
58
  subprocess.run(['split', '-b', '100M', video, f"/app/{u_id}_chunk_"])
59
 
60
  chunks = sorted(glob.glob(f"/app/{u_id}_chunk_*"))
61
  for i, chunk in enumerate(chunks):
62
- api.upload_file(path_or_fileobj=chunk, path_in_repo=f"{u_id}/{i}.bin", repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
 
 
 
 
 
 
63
  os.remove(chunk)
64
 
 
65
  meta = {"size": file_size, "chunks": len(chunks)}
66
- api.upload_file(path_or_fileobj=json.dumps(meta).encode(), path_in_repo=f"{u_id}/meta.json", repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
67
- print(f"[ENGINE] βœ… Archived: {u_id}", flush=True)
 
 
 
 
 
 
68
 
69
- except Exception as e:
70
- print(f"[ENGINE] ❌ Error: {e}")
71
- finally:
72
- shutil.rmtree(archive_dir, ignore_errors=True)
73
 
74
  @app.route('/archive')
75
  def archive():
76
  threading.Thread(target=do_archive, args=(request.args.get('id'), request.args.get('magnet'))).start()
77
- return "Archive request received", 200
78
 
79
  @app.route('/stream/<u_id>')
80
  def stream_proxy(u_id):
81
- # (Same stream_proxy code as before)
82
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  if __name__ == "__main__":
85
  app.run(host="0.0.0.0", port=7860)
 
11
  CHUNK_SIZE = 100 * 1024 * 1024 # 100MB
12
 
13
  def get_super_trackers():
14
+ """Fetches a massive list of trackers to wake up the swarm."""
15
  try:
16
+ # Pull from the 3 best-maintained tracker lists
17
+ urls = [
18
+ "https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt",
19
+ "https://newtrackon.com/api/stable",
20
+ "https://raw.githubusercontent.com/XIU2/TrackersListCollection/master/all.txt"
21
+ ]
22
+ trackers = []
23
+ for url in urls:
24
+ try: trackers.extend(requests.get(url, timeout=3).text.split('\n'))
25
+ except: continue
26
+ return ",".join(list(set([t.strip() for t in trackers if t.strip()])))
27
  except:
28
  return "udp://tracker.opentrackr.org:1337/announce,udp://tracker.internetwarriors.net:1337/announce"
29
 
30
  def do_archive(u_id, magnet):
31
  archive_dir = f"/app/temp_{u_id}"
32
  os.makedirs(archive_dir, exist_ok=True)
33
+ print(f"[ENGINE] πŸš€ Forcing metadata resolution for: {u_id}", flush=True)
34
 
35
+ # 1. THE AGGRESSIVE DOWNLOAD COMMAND
36
  cmd = [
37
  'aria2c', '--dir', archive_dir,
38
  '--bt-tracker=' + get_super_trackers(),
39
  '--enable-dht=true', '--bt-enable-lpd=true', '--enable-peer-exchange=true',
40
  '--bt-force-encryption=true', '--bt-min-crypto-level=arc4',
41
+ '--bt-max-peers=0', # Unlimited peers
42
+ '--bt-request-peer-speed-limit=0', # No throttling
43
+ '--split=64', '--max-connection-per-server=16',
44
  '--file-allocation=falloc',
45
  '--seed-time=0',
46
+ '--bt-stop-timeout=300', # Give up after 5 mins of 0 speed
47
  '--summary-interval=10',
48
+ '--dht-entry-point=dht.transmissionbt.com:6881', # Public DHT node
49
  magnet
50
  ]
51
 
 
56
  glob.glob(f'{archive_dir}/**/*.mp4', recursive=True)
57
 
58
  if not media_files:
59
+ print(f"[ENGINE] ❌ Swarm was dead or metadata failed for {u_id}", flush=True)
60
  return
61
 
62
+ # --- SERIES SMART LOGIC ---
63
+ video = None
64
+ if "_S" in u_id and "_E" in u_id:
65
+ try:
66
+ parts = u_id.split('_')
67
+ s_val = next(p for p in parts if p.startswith('S'))
68
+ e_val = next(p for p in parts if p.startswith('E'))
69
+ pattern = f"{s_val}{e_val}".lower()
70
+ match = [f for f in media_files if pattern in f.lower().replace(" ", "")]
71
+ if match: video = match[0]
72
+ except: pass
73
+
74
+ if not video:
75
+ video = max(media_files, key=os.path.getsize)
76
+
77
  file_size = os.path.getsize(video)
78
+
79
+ # --- CHUNK & UPLOAD ---
80
+ print(f"[ENGINE] πŸ“¦ Creating Secure Vault pieces...", flush=True)
81
  subprocess.run(['split', '-b', '100M', video, f"/app/{u_id}_chunk_"])
82
 
83
  chunks = sorted(glob.glob(f"/app/{u_id}_chunk_*"))
84
  for i, chunk in enumerate(chunks):
85
+ api.upload_file(
86
+ path_or_fileobj=chunk,
87
+ path_in_repo=f"{u_id}/{i}.bin",
88
+ repo_id=REPO_ID,
89
+ repo_type="dataset",
90
+ token=TOKEN
91
+ )
92
  os.remove(chunk)
93
 
94
+ # --- METADATA ---
95
  meta = {"size": file_size, "chunks": len(chunks)}
96
+ api.upload_file(
97
+ path_or_fileobj=json.dumps(meta).encode(),
98
+ path_in_repo=f"{u_id}/meta.json",
99
+ repo_id=REPO_ID,
100
+ repo_type="dataset",
101
+ token=TOKEN
102
+ )
103
+ print(f"[ENGINE] βœ… Mission Accomplished: {u_id}", flush=True)
104
 
105
+ except Exception as e: print(f"[ENGINE] ❌ Fatal Error: {e}")
106
+ finally: shutil.rmtree(archive_dir, ignore_errors=True)
 
 
107
 
108
  @app.route('/archive')
109
  def archive():
110
  threading.Thread(target=do_archive, args=(request.args.get('id'), request.args.get('magnet'))).start()
111
+ return "Archive command accepted", 200
112
 
113
  @app.route('/stream/<u_id>')
114
  def stream_proxy(u_id):
115
+ try:
116
+ meta_url = f"https://huggingface.co/datasets/{REPO_ID}/raw/main/{u_id}/meta.json"
117
+ meta = requests.get(meta_url).json()
118
+ total_size = meta['size']
119
+
120
+ range_header = request.headers.get('Range', 'bytes=0-')
121
+ byte_start = int(range_header.replace('bytes=', '').split('-')[0])
122
+ byte_end = total_size - 1
123
+
124
+ def generate():
125
+ curr = byte_start
126
+ while curr < total_size:
127
+ chunk_idx = curr // CHUNK_SIZE
128
+ offset = curr % CHUNK_SIZE
129
+ chunk_url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{u_id}/{chunk_idx}.bin"
130
+
131
+ with requests.get(chunk_url, headers={"Range": f"bytes={offset}-"}, stream=True) as r:
132
+ for chunk_data in r.iter_content(chunk_size=1024*1024):
133
+ if not chunk_data: break
134
+ yield chunk_data
135
+ curr += len(chunk_data)
136
+
137
+ return Response(stream_with_context(generate()), status=206, headers={
138
+ 'Accept-Ranges': 'bytes', 'Content-Type': 'video/x-matroska',
139
+ 'Content-Range': f'bytes {byte_start}-{byte_end}/{total_size}',
140
+ 'Content-Length': str(total_size - byte_start)
141
+ })
142
+ except: return "Episode not ready in Vault", 404
143
 
144
  if __name__ == "__main__":
145
  app.run(host="0.0.0.0", port=7860)