Spaces:
Paused
Paused
Upload 18 files
Browse files- Dockerfile +1 -1
- app.py +101 -45
- requirements.txt +0 -1
Dockerfile
CHANGED
|
@@ -8,7 +8,7 @@ ENV PYTHONUNBUFFERED=1 \
|
|
| 8 |
PORT=5000
|
| 9 |
|
| 10 |
RUN apt-get update \
|
| 11 |
-
&& apt-get install -y --no-install-recommends qbittorrent-nox ca-certificates bash tini \
|
| 12 |
&& rm -rf /var/lib/apt/lists/*
|
| 13 |
|
| 14 |
RUN useradd -m -u 1000 qbittorrent
|
|
|
|
| 8 |
PORT=5000
|
| 9 |
|
| 10 |
RUN apt-get update \
|
| 11 |
+
&& apt-get install -y --no-install-recommends qbittorrent-nox ca-certificates bash tini curl \
|
| 12 |
&& rm -rf /var/lib/apt/lists/*
|
| 13 |
|
| 14 |
RUN useradd -m -u 1000 qbittorrent
|
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import json
|
| 2 |
import os
|
| 3 |
import platform
|
|
|
|
| 4 |
import shutil
|
|
|
|
| 5 |
import threading
|
| 6 |
import time
|
| 7 |
from pathlib import Path
|
|
@@ -10,7 +12,6 @@ from flask import Flask, flash, jsonify, redirect, render_template, request, url
|
|
| 10 |
import psutil
|
| 11 |
import qbittorrentapi
|
| 12 |
import requests
|
| 13 |
-
from requests_toolbelt.multipart.encoder import MultipartEncoder, MultipartEncoderMonitor
|
| 14 |
|
| 15 |
|
| 16 |
BASE_DIR = Path(__file__).resolve().parent
|
|
@@ -45,6 +46,26 @@ def default_download_path() -> str:
|
|
| 45 |
return "/downloads"
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
GOFILE_UPLOAD_URL = env("GOFILE_UPLOAD_URL", "https://upload.gofile.io/uploadfile")
|
| 49 |
GOFILE_API_TOKEN = env("GOFILE_API_TOKEN", "")
|
| 50 |
IP_LOCATION_API_URL = env("IP_LOCATION_API_URL", "https://ipapi.co/json/")
|
|
@@ -328,6 +349,7 @@ def clear_gofile_runtime_fields(entry: dict) -> None:
|
|
| 328 |
entry["gofile_speed"] = ""
|
| 329 |
entry["gofile_eta_seconds"] = 0
|
| 330 |
entry["gofile_cancel_requested"] = False
|
|
|
|
| 331 |
|
| 332 |
|
| 333 |
def evaluate_torrent_health(client, torrents: list, state: dict) -> bool:
|
|
@@ -358,6 +380,7 @@ def evaluate_torrent_health(client, torrents: list, state: dict) -> bool:
|
|
| 358 |
"gofile_speed": "",
|
| 359 |
"gofile_status": "",
|
| 360 |
"gofile_link": "",
|
|
|
|
| 361 |
},
|
| 362 |
)
|
| 363 |
|
|
@@ -579,52 +602,81 @@ def action_response(message: str, ok: bool = True, status_code: int = 200, **ext
|
|
| 579 |
|
| 580 |
def start_gofile_upload_background(torrent_hash: str, torrent_name: str, file_path: Path) -> None:
|
| 581 |
def worker():
|
|
|
|
| 582 |
try:
|
| 583 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 584 |
if GOFILE_API_TOKEN:
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
)
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
data = payload.get("data", {})
|
| 629 |
download_page = data.get("downloadPage")
|
| 630 |
if not download_page:
|
|
@@ -648,7 +700,7 @@ def start_gofile_upload_background(torrent_hash: str, torrent_name: str, file_pa
|
|
| 648 |
add_event(state, "warning", f"Upload GoFile dibatalkan untuk {torrent_name}.")
|
| 649 |
|
| 650 |
update_state(cancel_mutator)
|
| 651 |
-
except
|
| 652 |
def error_mutator(state: dict):
|
| 653 |
entry = state.setdefault("torrents", {}).setdefault(torrent_hash, {})
|
| 654 |
clear_gofile_runtime_fields(entry)
|
|
@@ -657,6 +709,8 @@ def start_gofile_upload_background(torrent_hash: str, torrent_name: str, file_pa
|
|
| 657 |
add_event(state, "error", f"Upload GoFile gagal untuk {torrent_name}: {exc}")
|
| 658 |
|
| 659 |
update_state(error_mutator)
|
|
|
|
|
|
|
| 660 |
|
| 661 |
thread = threading.Thread(target=worker, daemon=True)
|
| 662 |
thread.start()
|
|
@@ -807,6 +861,7 @@ def upload_torrent_to_gofile(torrent_hash: str):
|
|
| 807 |
entry["gofile_speed"] = ""
|
| 808 |
entry["gofile_eta_seconds"] = 0
|
| 809 |
entry["gofile_cancel_requested"] = False
|
|
|
|
| 810 |
entry["gofile_status"] = "Sedang upload ke GoFile..."
|
| 811 |
entry["status_message"] = "Sedang upload ke GoFile..."
|
| 812 |
save_monitor_state(state)
|
|
@@ -827,6 +882,7 @@ def cancel_gofile_upload(torrent_hash: str):
|
|
| 827 |
entry["gofile_cancel_requested"] = True
|
| 828 |
entry["status_message"] = "Membatalkan upload GoFile..."
|
| 829 |
save_monitor_state(state)
|
|
|
|
| 830 |
return action_response("Pembatalan upload GoFile diproses.")
|
| 831 |
|
| 832 |
|
|
|
|
| 1 |
import json
|
| 2 |
import os
|
| 3 |
import platform
|
| 4 |
+
import signal
|
| 5 |
import shutil
|
| 6 |
+
import subprocess
|
| 7 |
import threading
|
| 8 |
import time
|
| 9 |
from pathlib import Path
|
|
|
|
| 12 |
import psutil
|
| 13 |
import qbittorrentapi
|
| 14 |
import requests
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
BASE_DIR = Path(__file__).resolve().parent
|
|
|
|
| 46 |
return "/downloads"
|
| 47 |
|
| 48 |
|
| 49 |
+
def process_exists(pid: int | None) -> bool:
|
| 50 |
+
if not pid or pid <= 0:
|
| 51 |
+
return False
|
| 52 |
+
try:
|
| 53 |
+
os.kill(pid, 0)
|
| 54 |
+
except OSError:
|
| 55 |
+
return False
|
| 56 |
+
return True
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def cancel_upload_process(pid: int | None) -> bool:
|
| 60 |
+
if not process_exists(pid):
|
| 61 |
+
return False
|
| 62 |
+
try:
|
| 63 |
+
os.kill(pid, signal.SIGTERM)
|
| 64 |
+
return True
|
| 65 |
+
except OSError:
|
| 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/")
|
|
|
|
| 349 |
entry["gofile_speed"] = ""
|
| 350 |
entry["gofile_eta_seconds"] = 0
|
| 351 |
entry["gofile_cancel_requested"] = False
|
| 352 |
+
entry["gofile_pid"] = 0
|
| 353 |
|
| 354 |
|
| 355 |
def evaluate_torrent_health(client, torrents: list, state: dict) -> bool:
|
|
|
|
| 380 |
"gofile_speed": "",
|
| 381 |
"gofile_status": "",
|
| 382 |
"gofile_link": "",
|
| 383 |
+
"gofile_pid": 0,
|
| 384 |
},
|
| 385 |
)
|
| 386 |
|
|
|
|
| 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:
|
|
|
|
| 700 |
add_event(state, "warning", f"Upload GoFile dibatalkan untuk {torrent_name}.")
|
| 701 |
|
| 702 |
update_state(cancel_mutator)
|
| 703 |
+
except ValueError as exc:
|
| 704 |
def error_mutator(state: dict):
|
| 705 |
entry = state.setdefault("torrents", {}).setdefault(torrent_hash, {})
|
| 706 |
clear_gofile_runtime_fields(entry)
|
|
|
|
| 709 |
add_event(state, "error", f"Upload GoFile gagal untuk {torrent_name}: {exc}")
|
| 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()
|
|
|
|
| 861 |
entry["gofile_speed"] = ""
|
| 862 |
entry["gofile_eta_seconds"] = 0
|
| 863 |
entry["gofile_cancel_requested"] = False
|
| 864 |
+
entry["gofile_pid"] = 0
|
| 865 |
entry["gofile_status"] = "Sedang upload ke GoFile..."
|
| 866 |
entry["status_message"] = "Sedang upload ke GoFile..."
|
| 867 |
save_monitor_state(state)
|
|
|
|
| 882 |
entry["gofile_cancel_requested"] = True
|
| 883 |
entry["status_message"] = "Membatalkan upload GoFile..."
|
| 884 |
save_monitor_state(state)
|
| 885 |
+
cancel_upload_process(int(entry.get("gofile_pid", 0) or 0))
|
| 886 |
return action_response("Pembatalan upload GoFile diproses.")
|
| 887 |
|
| 888 |
|
requirements.txt
CHANGED
|
@@ -2,5 +2,4 @@ Flask==3.1.1
|
|
| 2 |
qbittorrent-api==2025.7.0
|
| 3 |
requests==2.33.1
|
| 4 |
psutil==7.1.0
|
| 5 |
-
requests-toolbelt==1.0.0
|
| 6 |
gunicorn==23.0.0
|
|
|
|
| 2 |
qbittorrent-api==2025.7.0
|
| 3 |
requests==2.33.1
|
| 4 |
psutil==7.1.0
|
|
|
|
| 5 |
gunicorn==23.0.0
|