-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagniPlay_Linux.py
More file actions
199 lines (163 loc) · 7.25 KB
/
MagniPlay_Linux.py
File metadata and controls
199 lines (163 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import os
import subprocess
import threading
import time
import webbrowser
from flask import Flask, send_from_directory
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLineEdit, QPushButton, QLabel, QMessageBox
from werkzeug.serving import make_server
app = Flask(__name__)
DOWNLOAD_DIR = "downloads"
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
class FlaskServerThread:
def __init__(self, app, port=5000):
self.server = make_server("127.0.0.1", port, app)
self.thread = threading.Thread(target=self.server.serve_forever)
self.thread.daemon = True
def start(self):
self.thread.start()
def stop(self):
self.server.shutdown()
self.thread.join()
class Aria2TorrentStreamer(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Aria2 Magnet Link Streamer")
self.setGeometry(100, 100, 600, 400)
# GUI layout
self.layout = QVBoxLayout()
self.label = QLabel("Enter Magnet Link:")
self.layout.addWidget(self.label)
self.magnet_input = QLineEdit(self)
self.layout.addWidget(self.magnet_input)
self.start_button = QPushButton("Start Streaming")
self.start_button.clicked.connect(self.start_streaming)
self.layout.addWidget(self.start_button)
self.status_label = QLabel("Status: Waiting for user input.")
self.layout.addWidget(self.status_label)
self.open_file_button = QPushButton("Open Downloaded File")
self.open_file_button.clicked.connect(self.open_downloaded_file)
self.open_file_button.setEnabled(False)
self.layout.addWidget(self.open_file_button)
self.open_location_button = QPushButton("Open File Location")
self.open_location_button.clicked.connect(self.open_file_location)
self.open_location_button.setEnabled(False)
self.layout.addWidget(self.open_location_button)
self.delete_file_button = QPushButton("Permanently Delete Downloaded File")
self.delete_file_button.clicked.connect(self.delete_downloaded_file)
self.delete_file_button.setEnabled(False)
self.layout.addWidget(self.delete_file_button)
self.copy_link_button = QPushButton("Copy Stream Link")
self.copy_link_button.clicked.connect(self.copy_stream_link)
self.copy_link_button.setEnabled(False)
self.layout.addWidget(self.copy_link_button)
self.stop_streaming_button = QPushButton("Stop Streaming")
self.stop_streaming_button.clicked.connect(self.stop_streaming)
self.stop_streaming_button.setEnabled(False)
self.layout.addWidget(self.stop_streaming_button)
# Set central widget
central_widget = QWidget()
central_widget.setLayout(self.layout)
self.setCentralWidget(central_widget)
self.server_thread = None
self.video_file = None
def start_streaming(self):
magnet_link = self.magnet_input.text()
if not magnet_link:
self.status_label.setText("Status: Please enter a valid magnet link.")
return
self.status_label.setText("Status: Starting aria2 download...")
threading.Thread(target=self.start_aria2_download, args=(magnet_link,)).start()
def start_aria2_download(self, magnet_link):
cmd = [
"aria2c",
magnet_link,
"--dir", DOWNLOAD_DIR,
"--seed-time=0",
"--file-allocation=none",
]
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
self.status_label.setText(f"Status: Download failed: {e}")
return
except FileNotFoundError:
self.status_label.setText("Status: aria2c not found. Please install aria2.")
return
# After download, find the video file
self.video_file = self.find_video_file()
if self.video_file:
self.status_label.setText(f"Status: Download complete. Streaming {self.video_file}.")
self.open_file_button.setEnabled(True)
self.open_location_button.setEnabled(True)
self.delete_file_button.setEnabled(True)
self.stop_streaming_button.setEnabled(True)
self.copy_link_button.setEnabled(True)
self.start_flask_server()
else:
self.status_label.setText("Status: No video file found in torrent.")
def find_video_file(self):
for root, _, files in os.walk(DOWNLOAD_DIR):
for file in files:
if file.endswith((".mp4", ".mkv", ".avi", ".mov", ".webm", ".flv")):
return file
return None
def start_flask_server(self):
if self.server_thread is not None:
return
self.server_thread = FlaskServerThread(app)
self.server_thread.start()
self.status_label.setText(f"Streaming started: http://127.0.0.1:5000/stream/{self.video_file}")
def stop_streaming(self):
if self.server_thread:
self.server_thread.stop()
self.server_thread = None
self.status_label.setText("Streaming stopped.")
self.stop_streaming_button.setEnabled(False)
self.copy_link_button.setEnabled(False)
def open_downloaded_file(self):
if self.video_file:
video_path = os.path.join(DOWNLOAD_DIR, self.video_file)
try:
subprocess.Popen(['xdg-open', video_path])
except Exception as e:
QMessageBox.critical(self, "Error", f"Could not open file: {e}")
def open_file_location(self):
if self.video_file:
folder_path = os.path.abspath(DOWNLOAD_DIR)
try:
subprocess.Popen(['xdg-open', folder_path])
except Exception as e:
QMessageBox.critical(self, "Error", f"Could not open location: {e}")
def delete_downloaded_file(self):
if self.video_file:
if self.server_thread:
self.stop_streaming()
video_path = os.path.join(DOWNLOAD_DIR, self.video_file)
try:
os.remove(video_path)
self.status_label.setText("File deleted successfully.")
self.video_file = None
self.open_file_button.setEnabled(False)
self.open_location_button.setEnabled(False)
self.delete_file_button.setEnabled(False)
self.stop_streaming_button.setEnabled(False)
self.copy_link_button.setEnabled(False)
except Exception as e:
QMessageBox.critical(self, "Error", f"Could not delete file: {e}")
def copy_stream_link(self):
if self.video_file:
stream_link = f"http://127.0.0.1:5000/stream/{self.video_file}"
QApplication.clipboard().setText(stream_link)
QMessageBox.information(self, "Stream Link Copied", "The stream link has been copied to your clipboard.")
@app.route("/stream/<path:filename>")
def stream_video(filename):
"""Serve video file for streaming."""
return send_from_directory(DOWNLOAD_DIR, filename, as_attachment=False)
def main():
app_qt = QApplication([])
window = Aria2TorrentStreamer()
window.show()
app_qt.exec_()
if __name__ == "__main__":
main()