nexus33rd commited on
Commit
4eeabd1
·
verified ·
1 Parent(s): aa954f8

Upload 18 files

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +132 -72
README.md CHANGED
@@ -71,7 +71,7 @@ Proyek sekarang memakai satu `Dockerfile` default. File [docker-compose.yml](./d
71
  - `QBT_PROFILE`: direktori config qBittorrent. Default `/config`, atau `/data/qbittorrent` bila `/data` tersedia
72
  - `QBITTORRENT_SAVE_PATH`: direktori hasil download. Default `/downloads`, atau `/data/downloads` bila `/data` tersedia
73
  - `GOFILE_API_TOKEN`: token opsional untuk GoFile
74
- - `GOFILE_UPLOAD_URL`: endpoint upload GoFile. Default `https://upload.gofile.io/uploadfile`
75
  - `IP_LOCATION_API_URL`: endpoint lookup lokasi. Default `https://ipapi.co/json/`
76
  - `TORRENT_STUCK_TIMEOUT_MINUTES`: timeout torrent stuck. Default `5`
77
 
 
71
  - `QBT_PROFILE`: direktori config qBittorrent. Default `/config`, atau `/data/qbittorrent` bila `/data` tersedia
72
  - `QBITTORRENT_SAVE_PATH`: direktori hasil download. Default `/downloads`, atau `/data/downloads` bila `/data` tersedia
73
  - `GOFILE_API_TOKEN`: token opsional untuk GoFile
74
+ - `GOFILE_UPLOAD_URLS`: daftar endpoint upload GoFile dipisahkan koma. Default mencoba beberapa server resmi GoFile secara berurutan
75
  - `IP_LOCATION_API_URL`: endpoint lookup lokasi. Default `https://ipapi.co/json/`
76
  - `TORRENT_STUCK_TIMEOUT_MINUTES`: timeout torrent stuck. Default `5`
77
 
app.py CHANGED
@@ -46,6 +46,24 @@ def default_download_path() -> str:
46
  return "/downloads"
47
 
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  def process_exists(pid: int | None) -> bool:
50
  if not pid or pid <= 0:
51
  return False
@@ -66,7 +84,7 @@ def cancel_upload_process(pid: int | None) -> bool:
66
  return False
67
 
68
 
69
- GOFILE_UPLOAD_URL = env("GOFILE_UPLOAD_URL", "https://upload.gofile.io/uploadfile")
70
  GOFILE_API_TOKEN = env("GOFILE_API_TOKEN", "")
71
  IP_LOCATION_API_URL = env("IP_LOCATION_API_URL", "https://ipapi.co/json/")
72
  CLOUDFLARE_DOWNLOAD_URL = env("CLOUDFLARE_DOWNLOAD_URL", "https://speed.cloudflare.com/__down")
@@ -600,83 +618,125 @@ def action_response(message: str, ok: bool = True, status_code: int = 200, **ext
600
  return jsonify(payload), status_code
601
 
602
 
603
- def start_gofile_upload_background(torrent_hash: str, torrent_name: str, file_path: Path) -> None:
604
- def worker():
605
- response_path = UPLOAD_DIR / f"gofile-{torrent_hash}.json"
606
- try:
607
- command = [
608
- "curl",
609
- "--silent",
610
- "--show-error",
611
- "--location",
612
- "--connect-timeout",
613
- "30",
614
- "--max-time",
615
- "0",
616
- "--output",
617
- str(response_path),
618
- "--write-out",
619
- "%{http_code}",
620
- ]
621
- if GOFILE_API_TOKEN:
622
- command.extend(["-H", f"Authorization: Bearer {GOFILE_API_TOKEN}"])
623
- command.extend(["-F", f"file=@{file_path}", GOFILE_UPLOAD_URL])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624
 
625
- started_at = time.time()
626
- process = subprocess.Popen(
627
- command,
628
- stdout=subprocess.PIPE,
629
- stderr=subprocess.PIPE,
630
- text=True,
631
- )
 
 
 
632
 
633
- update_state(
634
- lambda state: state.setdefault("torrents", {}).setdefault(torrent_hash, {}).update(
635
- {
636
- "gofile_uploading": True,
637
- "gofile_pid": process.pid,
638
- "gofile_status": "Sedang upload ke GoFile via curl...",
639
- "status_message": "Sedang upload ke GoFile via curl...",
640
- }
641
- )
 
 
 
 
 
 
 
 
 
 
642
  )
 
 
643
 
644
- while True:
645
- return_code = process.poll()
646
- current_state = load_monitor_state()
647
- current_entry = current_state.setdefault("torrents", {}).setdefault(torrent_hash, {})
648
- if current_entry.get("gofile_cancel_requested"):
649
- cancel_upload_process(process.pid)
650
 
651
- if return_code is not None:
652
- break
 
 
653
 
654
- elapsed = max(int(time.time() - started_at), 1)
655
- update_state(
656
- lambda state: state.setdefault("torrents", {}).setdefault(torrent_hash, {}).update(
657
- {
658
- "gofile_uploading": True,
659
- "gofile_pid": process.pid,
660
- "gofile_status": "Sedang upload ke GoFile via curl...",
661
- "status_message": f"Sedang upload ke GoFile via curl... {format_duration(elapsed)}",
662
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
663
  )
664
- )
665
- time.sleep(1)
666
-
667
- stdout, stderr = process.communicate()
668
- if current_entry.get("gofile_cancel_requested") or process.returncode == -15:
669
- raise GofileUploadCancelled("Upload GoFile dibatalkan.")
670
- if process.returncode != 0:
671
- error_detail = stderr.strip() or stdout.strip() or f"curl exited with code {process.returncode}"
672
- raise ValueError(error_detail)
673
-
674
- http_code = int((stdout or "0").strip() or "0")
675
- if http_code >= 400 or http_code == 0:
676
- error_body = response_path.read_text(encoding="utf-8", errors="replace") if response_path.exists() else ""
677
- raise ValueError(f"HTTP {http_code}: {error_body[:500]}".strip())
678
-
679
- payload = json.loads(response_path.read_text(encoding="utf-8"))
680
  data = payload.get("data", {})
681
  download_page = data.get("downloadPage")
682
  if not download_page:
@@ -710,7 +770,7 @@ def start_gofile_upload_background(torrent_hash: str, torrent_name: str, file_pa
710
 
711
  update_state(error_mutator)
712
  finally:
713
- response_path.unlink(missing_ok=True)
714
 
715
  thread = threading.Thread(target=worker, daemon=True)
716
  thread.start()
 
46
  return "/downloads"
47
 
48
 
49
+ def default_gofile_upload_urls() -> str:
50
+ return ",".join(
51
+ [
52
+ "https://upload.gofile.io/uploadfile",
53
+ "https://upload-na-phx.gofile.io/uploadfile",
54
+ "https://upload-na-nyc.gofile.io/uploadfile",
55
+ "https://upload-eu-par.gofile.io/uploadfile",
56
+ "https://upload-ap-sgp.gofile.io/uploadfile",
57
+ ]
58
+ )
59
+
60
+
61
+ def parse_gofile_upload_urls() -> list[str]:
62
+ raw_value = env("GOFILE_UPLOAD_URLS", default_gofile_upload_urls())
63
+ urls = [item.strip() for item in raw_value.split(",") if item.strip()]
64
+ return urls or ["https://upload.gofile.io/uploadfile"]
65
+
66
+
67
  def process_exists(pid: int | None) -> bool:
68
  if not pid or pid <= 0:
69
  return False
 
84
  return False
85
 
86
 
87
+ GOFILE_UPLOAD_URLS = parse_gofile_upload_urls()
88
  GOFILE_API_TOKEN = env("GOFILE_API_TOKEN", "")
89
  IP_LOCATION_API_URL = env("IP_LOCATION_API_URL", "https://ipapi.co/json/")
90
  CLOUDFLARE_DOWNLOAD_URL = env("CLOUDFLARE_DOWNLOAD_URL", "https://speed.cloudflare.com/__down")
 
618
  return jsonify(payload), status_code
619
 
620
 
621
+ def gofile_upload_attempt(
622
+ upload_url: str,
623
+ torrent_hash: str,
624
+ file_path: Path,
625
+ started_at: float,
626
+ ) -> dict:
627
+ response_path = UPLOAD_DIR / f"gofile-{torrent_hash}.json"
628
+ command = [
629
+ "curl",
630
+ "--silent",
631
+ "--show-error",
632
+ "--location",
633
+ "--ipv4",
634
+ "--retry",
635
+ "2",
636
+ "--retry-delay",
637
+ "2",
638
+ "--retry-all-errors",
639
+ "--connect-timeout",
640
+ "15",
641
+ "--speed-time",
642
+ "60",
643
+ "--speed-limit",
644
+ "10240",
645
+ "--max-time",
646
+ "0",
647
+ "--output",
648
+ str(response_path),
649
+ "--write-out",
650
+ "%{http_code}",
651
+ ]
652
+ if GOFILE_API_TOKEN:
653
+ command.extend(["-H", f"Authorization: Bearer {GOFILE_API_TOKEN}"])
654
+ command.extend(["-F", f"file=@{file_path}", upload_url])
655
+
656
+ process = subprocess.Popen(
657
+ command,
658
+ stdout=subprocess.PIPE,
659
+ stderr=subprocess.PIPE,
660
+ text=True,
661
+ )
662
 
663
+ update_state(
664
+ lambda state: state.setdefault("torrents", {}).setdefault(torrent_hash, {}).update(
665
+ {
666
+ "gofile_uploading": True,
667
+ "gofile_pid": process.pid,
668
+ "gofile_status": f"Sedang upload ke GoFile via {upload_url}...",
669
+ "status_message": f"Sedang upload ke GoFile via {upload_url}...",
670
+ }
671
+ )
672
+ )
673
 
674
+ while True:
675
+ return_code = process.poll()
676
+ current_state = load_monitor_state()
677
+ current_entry = current_state.setdefault("torrents", {}).setdefault(torrent_hash, {})
678
+ if current_entry.get("gofile_cancel_requested"):
679
+ cancel_upload_process(process.pid)
680
+
681
+ if return_code is not None:
682
+ break
683
+
684
+ elapsed = max(int(time.time() - started_at), 1)
685
+ update_state(
686
+ lambda state: state.setdefault("torrents", {}).setdefault(torrent_hash, {}).update(
687
+ {
688
+ "gofile_uploading": True,
689
+ "gofile_pid": process.pid,
690
+ "gofile_status": f"Sedang upload ke GoFile via {upload_url}...",
691
+ "status_message": f"Sedang upload ke GoFile via {upload_url}... {format_duration(elapsed)}",
692
+ }
693
  )
694
+ )
695
+ time.sleep(1)
696
 
697
+ stdout, stderr = process.communicate()
698
+ if current_entry.get("gofile_cancel_requested") or process.returncode == -15:
699
+ raise GofileUploadCancelled("Upload GoFile dibatalkan.")
700
+ if process.returncode != 0:
701
+ error_detail = stderr.strip() or stdout.strip() or f"curl exited with code {process.returncode}"
702
+ raise ValueError(error_detail)
703
 
704
+ http_code = int((stdout or "0").strip() or "0")
705
+ if http_code >= 400 or http_code == 0:
706
+ error_body = response_path.read_text(encoding="utf-8", errors="replace") if response_path.exists() else ""
707
+ raise ValueError(f"HTTP {http_code}: {error_body[:500]}".strip())
708
 
709
+ payload = json.loads(response_path.read_text(encoding="utf-8"))
710
+ return payload
711
+
712
+
713
+ def start_gofile_upload_background(torrent_hash: str, torrent_name: str, file_path: Path) -> None:
714
+ def worker():
715
+ try:
716
+ started_at = time.time()
717
+ payload = None
718
+ last_error = None
719
+ for upload_url in GOFILE_UPLOAD_URLS:
720
+ try:
721
+ payload = gofile_upload_attempt(upload_url, torrent_hash, file_path, started_at)
722
+ break
723
+ except GofileUploadCancelled:
724
+ raise
725
+ except ValueError as exc:
726
+ last_error = f"{upload_url}: {exc}"
727
+ update_state(
728
+ lambda state: state.setdefault("torrents", {}).setdefault(torrent_hash, {}).update(
729
+ {
730
+ "gofile_uploading": True,
731
+ "gofile_status": f"Endpoint gagal, mencoba server lain: {upload_url}",
732
+ "status_message": f"Endpoint gagal, mencoba server lain: {upload_url}",
733
+ }
734
+ )
735
  )
736
+
737
+ if payload is None:
738
+ raise ValueError(last_error or "Semua endpoint GoFile gagal.")
739
+
 
 
 
 
 
 
 
 
 
 
 
 
740
  data = payload.get("data", {})
741
  download_page = data.get("downloadPage")
742
  if not download_page:
 
770
 
771
  update_state(error_mutator)
772
  finally:
773
+ (UPLOAD_DIR / f"gofile-{torrent_hash}.json").unlink(missing_ok=True)
774
 
775
  thread = threading.Thread(target=worker, daemon=True)
776
  thread.start()