bigbossmonster commited on
Commit
623b0d9
·
verified ·
1 Parent(s): 3eaf70f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import threading
4
+ import asyncio
5
+ from torrentp import TorrentDownloader
6
+
7
+ # --- Configuration ---
8
+
9
+ if os.path.exists("/data"):
10
+ SAVE_PATH = "/data/torrents"
11
+ else:
12
+ SAVE_PATH = "./torrents"
13
+ print("WARNING: /data directory not found. Using ephemeral storage.")
14
+ print("Files will be DELETED when the Space restarts.")
15
+
16
+ os.makedirs(SAVE_PATH, exist_ok=True)
17
+ print(f"File save location: {SAVE_PATH}")
18
+
19
+ # Global dictionary to track the status from our threads
20
+ # We will store: {magnet_link: "Status Message"}
21
+ download_status = {}
22
+
23
+ def run_download_in_thread(magnet_link):
24
+ """
25
+ This function runs in a separate thread.
26
+ It's what allows the GUI to stay responsive.
27
+ """
28
+ try:
29
+ # Set status to "Starting"
30
+ download_status[magnet_link] = "🔽 Starting download..."
31
+ print(f"Thread started for: {magnet_link}")
32
+
33
+ # Initialize the downloader
34
+ torrent_downloader = TorrentDownloader(magnet_link, SAVE_PATH)
35
+
36
+ # This is a blocking call.
37
+ # We must run it in a new event loop inside this thread.
38
+ asyncio.run(torrent_downloader.start_download())
39
+
40
+ # If the line above finishes, the download is done.
41
+ print(f"Thread finished for: {magnet_link}")
42
+ download_status[magnet_link] = "✅ Download Finished"
43
+
44
+ except Exception as e:
45
+ print(f"Error in download thread: {e}")
46
+ download_status[magnet_link] = f"Error: {e}"
47
+
48
+ def add_torrent(magnet_link):
49
+ """
50
+ This is called by the Gradio button.
51
+ It *starts* the background thread but does not wait for it.
52
+ """
53
+ if not magnet_link or not magnet_link.startswith("magnet:?"):
54
+ return "Error: Invalid magnet link."
55
+
56
+ if magnet_link in download_status and download_status[magnet_link] not in ["✅ Download Finished", "Error"]:
57
+ return "Download for this link is already in progress."
58
+
59
+ # Create and start the new thread
60
+ # target=run_download_in_thread is the function to run
61
+ # args=(magnet_link,) are the arguments to pass to that function
62
+ thread = threading.Thread(target=run_download_in_thread, args=(magnet_link,))
63
+ thread.start()
64
+
65
+ download_status[magnet_link] = "ℹ️ Queued. Starting thread..."
66
+ return "Download process started in background."
67
+
68
+ def get_status_report():
69
+ """
70
+ Reads the global 'download_status' dictionary to show progress.
71
+ This is not real-time speed, but it shows "Starting" or "Finished".
72
+ """
73
+ if not download_status:
74
+ return "No downloads started yet."
75
+
76
+ report_lines = []
77
+ for link, status in download_status.items():
78
+ # Show the first 50 chars of the magnet link as an ID
79
+ link_id = link[0:50] + "..."
80
+ report_lines.append(f"{status}\n ({link_id})\n")
81
+
82
+ return "\n".join(report_lines)
83
+
84
+ # --- Build the Gradio App ---
85
+ with gr.Blocks(title="Torrent Downloader") as demo:
86
+ gr.Markdown(
87
+ "# Hugging Face Torrent Downloader (using 'torrentp')\n"
88
+ f"Shows basic status. Files are saved in: `{SAVE_PATH}`"
89
+ )
90
+
91
+ with gr.Row():
92
+ magnet_input = gr.Textbox(
93
+ label="Magnet Link",
94
+ placeholder="magnet:?xt=urn:btih:...",
95
+ scale=4
96
+ )
97
+ add_button = gr.Button("Add Torrent", variant="primary", scale=1)
98
+
99
+ status_output = gr.Textbox(
100
+ label="Download Status (refreshes every 5s)",
101
+ lines=15,
102
+ interactive=False,
103
+ value=get_status_report,
104
+ every=5
105
+ )
106
+
107
+ add_result_output = gr.Textbox(label="System Message", interactive=False)
108
+
109
+ add_button.click(
110
+ fn=add_torrent,
111
+ inputs=magnet_input,
112
+ outputs=add_result_output
113
+ )
114
+ add_button.click(lambda: "", outputs=magnet_input)
115
+
116
+ # Launch the app
117
+ demo.launch()