amri12310 commited on
Commit
d109791
·
verified ·
1 Parent(s): 772c295

Update engine.py

Browse files
Files changed (1) hide show
  1. engine.py +2 -177
engine.py CHANGED
@@ -29,6 +29,7 @@ TOKEN = os.environ.get("HF_TOKEN")
29
  GH_TOKEN = os.environ.get("GH_PAT")
30
  ADMIN_KEY = os.environ.get("ADMIN_KEY", "Amrit2026")
31
  ADDON_URL = "https://amri12310-sys-b-addon.hf.space"
 
32
 
33
  # ⚡ ADDED: ENGINE URLS FOR UNIFIED DASHBOARD MATH
34
  ENGINE_URLS = [
@@ -247,7 +248,7 @@ VAULT_HTML = """
247
  </div>
248
  </div>
249
  <div class="flex justify-between items-center mt-3 pt-3 border-t border-slate-700/50">
250
- <a href="/stream/{{ item.id }}?dl=1" class="px-2 py-1 bg-blue-600 hover:bg-blue-500 text-white rounded text-[10px] font-bold transition-colors">DOWNLOAD EP</a>
251
  <form action="/delete_vaulted/{{ item.id }}?key={{ key }}" method="POST">
252
  <button class="text-slate-500 hover:text-red-500 text-[10px] font-bold hover:underline transition-colors">DELETE EP</button>
253
  </form>
@@ -864,182 +865,6 @@ def internal_kill(u_id):
864
  active_vaults.discard(u_id)
865
  return "Node Aborted", 200
866
 
867
- # ==========================================
868
- # TURBO LEAD-PIPE PROXY (WITH ⚡ DOWNLOAD SUPPORT)
869
- # ==========================================
870
- @app.route('/stream/<u_id>', methods=['GET', 'HEAD'])
871
- @app.route('/stream/<u_id>/<filename>', methods=['GET', 'HEAD'])
872
- def stream_vault(u_id, filename=None):
873
- auth = {"Authorization": f"Bearer {TOKEN}"}
874
-
875
- try:
876
- r_meta = stream_session.get(f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{u_id}/meta.json", headers=auth, timeout=5)
877
- if r_meta.status_code == 200:
878
- meta = r_meta.json()
879
- total_size, num_chunks, c_size_mb = meta.get('size', 0), meta.get('chunks', 0), meta.get('chunk_size_mb', 500)
880
- chunk_size_bytes = c_size_mb * 1024 * 1024
881
-
882
- # ⚡ LIGHTNING FAST HEAD RESPONSE FOR EXOPLAYER RESUME
883
- if request.method == 'HEAD':
884
- return Response("", status=200, headers={"Content-Length": str(total_size), "Content-Type": "video/x-matroska", "Accept-Ranges": "bytes"})
885
-
886
- # ⚡ FIXED RANGE PARSING FOR EXOPLAYER RESUME
887
- range_header = request.headers.get('Range', None)
888
- req_start = 0
889
- req_end = total_size - 1
890
-
891
- if range_header:
892
- match = re.search(r'bytes=(\d+)-(\d*)', range_header)
893
- if match:
894
- req_start = int(match.group(1))
895
- if match.group(2):
896
- req_end = int(match.group(2))
897
-
898
- bytes_to_send = (req_end - req_start) + 1
899
-
900
- def generate_turbo_new():
901
- start_chunk_idx = req_start // chunk_size_bytes
902
- first_offset = req_start % chunk_size_bytes
903
- data_queue = queue.Queue(maxsize=400)
904
-
905
- def turbo_worker():
906
- try:
907
- for i in range(start_chunk_idx, num_chunks):
908
- current_offset = first_offset if i == start_chunk_idx else 0
909
- while True:
910
- chunk_url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{u_id}/{i}.bin"
911
- h = auth.copy()
912
- if current_offset > 0: h["Range"] = f"bytes={current_offset}-"
913
- try:
914
- with stream_session.get(chunk_url, headers=h, stream=True, timeout=(3.0, 5.0)) as r:
915
- r.raise_for_status()
916
- expected_len = int(r.headers.get('Content-Length', 0))
917
- bytes_this_req = 0
918
- for data in r.iter_content(chunk_size=1024*1024):
919
- if data:
920
- data_queue.put(data)
921
- bytes_this_req += len(data)
922
- current_offset += len(data)
923
- if expected_len > 0 and bytes_this_req < expected_len:
924
- print(f"⚠️ [NETWORK] HF dropped chunk {i} early. Resuming at {current_offset}...", flush=True)
925
- continue
926
- else: break
927
- except Exception as e:
928
- time.sleep(1)
929
- except Exception as e: print(f"Turbo Worker Error: {e}", flush=True)
930
- finally: data_queue.put(None)
931
-
932
- threading.Thread(target=turbo_worker, daemon=True).start()
933
-
934
- bytes_sent = 0
935
- while bytes_sent < bytes_to_send:
936
- chunk_data = data_queue.get()
937
- if chunk_data is None: break
938
- if bytes_sent + len(chunk_data) > bytes_to_send:
939
- yield chunk_data[:bytes_to_send - bytes_sent]
940
- break
941
- yield chunk_data
942
- bytes_sent += len(chunk_data)
943
-
944
- headers = {"Content-Length": str(bytes_to_send), "Content-Type": "video/x-matroska", "Accept-Ranges": "bytes", "Connection": "keep-alive"}
945
- if range_header: headers["Content-Range"] = f"bytes {req_start}-{req_end}/{total_size}"
946
-
947
- # ⚡ ADDED: FORCE BROWSER TO DOWNLOAD FILE IF ?dl=1
948
- if request.args.get('dl', '0') == '1':
949
- headers["Content-Disposition"] = f'attachment; filename="{u_id}.mkv"'
950
-
951
- # ⚡ DIRECT PASSTHROUGH: Forces TV to recognize it as a standard movie, not a Live Stream!
952
- r = Response(stream_with_context(generate_turbo_new()), status=206 if range_header else 200, headers=headers)
953
- r.direct_passthrough = True
954
- return r
955
-
956
- except Exception as e: print(f"Meta A error: {e}", flush=True)
957
-
958
- # ⚡ ROUTE B: OLD Sharded format
959
- all_chunks, total_size = [], 0
960
- for p in [1, 2, 3]:
961
- try:
962
- r = stream_session.get(f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{u_id}/meta_part{p}.json", headers=auth, timeout=5)
963
- if r.status_code == 200:
964
- m = r.json()
965
- total_size = max(total_size, m.get('total_size', 0))
966
- all_chunks.extend(m.get('chunks', []))
967
- except: pass
968
-
969
- if not all_chunks: return "Metadata not found.", 404
970
-
971
- all_chunks.sort(key=lambda x: x['start'])
972
-
973
- # ⚡ LIGHTNING FAST HEAD RESPONSE FOR EXOPLAYER RESUME
974
- if request.method == 'HEAD':
975
- return Response("", status=200, headers={"Content-Length": str(total_size), "Content-Type": "video/x-matroska", "Accept-Ranges": "bytes"})
976
-
977
- # ⚡ FIXED RANGE PARSING FOR EXOPLAYER RESUME
978
- range_header = request.headers.get('Range', None)
979
- req_start = 0
980
- req_end = total_size - 1
981
-
982
- if range_header:
983
- match = re.search(r'bytes=(\d+)-(\d*)', range_header)
984
- if match:
985
- req_start = int(match.group(1))
986
- if match.group(2):
987
- req_end = int(match.group(2))
988
-
989
- bytes_to_send = (req_end - req_start) + 1
990
-
991
- def generate_turbo_old():
992
- data_queue = queue.Queue(maxsize=400)
993
- def turbo_worker_old():
994
- current_start = req_start
995
- try:
996
- for chunk in all_chunks:
997
- if chunk['end'] <= current_start: continue
998
- while current_start < chunk['end']:
999
- url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{u_id}/{chunk['name']}"
1000
- h = auth.copy()
1001
- if current_start > chunk['start']: h["Range"] = f"bytes={current_start - chunk['start']}-"
1002
- try:
1003
- with stream_session.get(url, headers=h, stream=True, timeout=(3.0, 5.0)) as r:
1004
- if r.status_code not in [200, 206]: break
1005
- expected_len = int(r.headers.get('Content-Length', 0))
1006
- bytes_this_req = 0
1007
- for data in r.iter_content(chunk_size=1024*1024):
1008
- if data:
1009
- data_queue.put(data)
1010
- current_start += len(data)
1011
- bytes_this_req += len(data)
1012
- if expected_len > 0 and bytes_this_req < expected_len:
1013
- print(f"⚠️ [NETWORK] Old chunk truncated. Resuming...", flush=True)
1014
- continue
1015
- else: break
1016
- except: time.sleep(1)
1017
- except Exception as e: print(f"Legacy Worker Error: {e}", flush=True)
1018
- finally: data_queue.put(None)
1019
-
1020
- threading.Thread(target=turbo_worker_old, daemon=True).start()
1021
-
1022
- bytes_sent = 0
1023
- while bytes_sent < bytes_to_send:
1024
- chunk_data = data_queue.get()
1025
- if chunk_data is None: break
1026
- if bytes_sent + len(chunk_data) > bytes_to_send:
1027
- yield chunk_data[:bytes_to_send - bytes_sent]
1028
- break
1029
- yield chunk_data
1030
- bytes_sent += len(chunk_data)
1031
-
1032
- headers = {"Content-Length": str(bytes_to_send), "Content-Type": "video/x-matroska", "Accept-Ranges": "bytes", "Connection": "keep-alive"}
1033
- if range_header: headers["Content-Range"] = f"bytes {req_start}-{req_end}/{total_size}"
1034
-
1035
- # ⚡ ADDED: FORCE BROWSER TO DOWNLOAD FILE IF ?dl=1
1036
- if request.args.get('dl', '0') == '1':
1037
- headers["Content-Disposition"] = f'attachment; filename="{u_id}.mkv"'
1038
-
1039
- # ⚡ DIRECT PASSTHROUGH: Forces TV to recognize it as a standard movie, not a Live Stream!
1040
- r = Response(stream_with_context(generate_turbo_old()), status=206 if range_header else 200, headers=headers)
1041
- r.direct_passthrough = True
1042
- return r
1043
 
1044
  @app.route('/delete_active/<u_id>', methods=['POST'])
1045
  def delete_active(u_id):
 
29
  GH_TOKEN = os.environ.get("GH_PAT")
30
  ADMIN_KEY = os.environ.get("ADMIN_KEY", "Amrit2026")
31
  ADDON_URL = "https://amri12310-sys-b-addon.hf.space"
32
+ SYS_E_URL = "https://amri12310-sys-e-engine.hf.space"
33
 
34
  # ⚡ ADDED: ENGINE URLS FOR UNIFIED DASHBOARD MATH
35
  ENGINE_URLS = [
 
248
  </div>
249
  </div>
250
  <div class="flex justify-between items-center mt-3 pt-3 border-t border-slate-700/50">
251
+ <a href="{{ sys_e_url }}/stream/{{ item.id }}?dl=1" class="px-2 py-1 bg-blue-600 hover:bg-blue-500 text-white rounded text-[10px] font-bold transition-colors">DOWNLOAD EP</a>
252
  <form action="/delete_vaulted/{{ item.id }}?key={{ key }}" method="POST">
253
  <button class="text-slate-500 hover:text-red-500 text-[10px] font-bold hover:underline transition-colors">DELETE EP</button>
254
  </form>
 
865
  active_vaults.discard(u_id)
866
  return "Node Aborted", 200
867
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
868
 
869
  @app.route('/delete_active/<u_id>', methods=['POST'])
870
  def delete_active(u_id):