Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,6 +36,12 @@ def is_http_url(s: str) -> bool:
|
|
| 36 |
s = s.strip().lower()
|
| 37 |
return s.startswith("http://") or s.startswith("https://")
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def parse_magnet(magnet: str) -> Dict[str, Any]:
|
| 40 |
out: Dict[str, Any] = {"ok": False, "error": None}
|
| 41 |
try:
|
|
@@ -80,7 +86,7 @@ def fetch_bytes(url: str, timeout: int = 45) -> bytes:
|
|
| 80 |
return r.content
|
| 81 |
|
| 82 |
def parse_torrent(raw: bytes) -> Dict[str, Any]:
|
| 83 |
-
#
|
| 84 |
data = bencodepy.decode(raw)
|
| 85 |
if not isinstance(data, dict) or b"info" not in data:
|
| 86 |
raise ValueError("Invalid .torrent: missing 'info' dictionary.")
|
|
@@ -219,7 +225,9 @@ def handle_input(input_text: str, uploaded_file) -> Tuple[str, pd.DataFrame, str
|
|
| 219 |
raise gr.Error(f"Could not parse magnet: {mag.get('error')}")
|
| 220 |
# Magnet on Spaces → no file list (no DHT)
|
| 221 |
empty = pd.DataFrame(columns=["Path", "Length (bytes)", "Length (HR)"])
|
| 222 |
-
csvp = f"/mnt/data/files_{int(time.time())}.csv"
|
|
|
|
|
|
|
| 223 |
state = {"summary": {"Name": mag.get("display_name") or "(magnet)",
|
| 224 |
"Infohash (v1)": mag.get("infohash_hex") or "—",
|
| 225 |
"Web seeds": []},
|
|
@@ -242,6 +250,7 @@ def handle_input(input_text: str, uploaded_file) -> Tuple[str, pd.DataFrame, str
|
|
| 242 |
|
| 243 |
# For .torrent path:
|
| 244 |
csv_path = f"/mnt/data/files_{int(time.time())}.csv"
|
|
|
|
| 245 |
parsed["files_df"].to_csv(csv_path, index=False)
|
| 246 |
# pack state (DataFrame → records) to reuse in Download tab
|
| 247 |
state = {"summary": parsed["summary"], "files_df": parsed["files_df"].to_dict(orient="list")}
|
|
|
|
| 36 |
s = s.strip().lower()
|
| 37 |
return s.startswith("http://") or s.startswith("https://")
|
| 38 |
|
| 39 |
+
def ensure_parent_dir(path: str) -> None:
|
| 40 |
+
"""Create parent directory for a file path if missing."""
|
| 41 |
+
parent = os.path.dirname(path)
|
| 42 |
+
if parent:
|
| 43 |
+
os.makedirs(parent, exist_ok=True)
|
| 44 |
+
|
| 45 |
def parse_magnet(magnet: str) -> Dict[str, Any]:
|
| 46 |
out: Dict[str, Any] = {"ok": False, "error": None}
|
| 47 |
try:
|
|
|
|
| 86 |
return r.content
|
| 87 |
|
| 88 |
def parse_torrent(raw: bytes) -> Dict[str, Any]:
|
| 89 |
+
# Use top-level decode; current bencodepy has no Bencode class
|
| 90 |
data = bencodepy.decode(raw)
|
| 91 |
if not isinstance(data, dict) or b"info" not in data:
|
| 92 |
raise ValueError("Invalid .torrent: missing 'info' dictionary.")
|
|
|
|
| 225 |
raise gr.Error(f"Could not parse magnet: {mag.get('error')}")
|
| 226 |
# Magnet on Spaces → no file list (no DHT)
|
| 227 |
empty = pd.DataFrame(columns=["Path", "Length (bytes)", "Length (HR)"])
|
| 228 |
+
csvp = f"/mnt/data/files_{int(time.time())}.csv"
|
| 229 |
+
ensure_parent_dir(csvp)
|
| 230 |
+
empty.to_csv(csvp, index=False)
|
| 231 |
state = {"summary": {"Name": mag.get("display_name") or "(magnet)",
|
| 232 |
"Infohash (v1)": mag.get("infohash_hex") or "—",
|
| 233 |
"Web seeds": []},
|
|
|
|
| 250 |
|
| 251 |
# For .torrent path:
|
| 252 |
csv_path = f"/mnt/data/files_{int(time.time())}.csv"
|
| 253 |
+
ensure_parent_dir(csv_path)
|
| 254 |
parsed["files_df"].to_csv(csv_path, index=False)
|
| 255 |
# pack state (DataFrame → records) to reuse in Download tab
|
| 256 |
state = {"summary": parsed["summary"], "files_df": parsed["files_df"].to_dict(orient="list")}
|