This repository has been archived by the owner on Jan 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
292 lines (214 loc) · 7.51 KB
/
app.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from __future__ import print_function
from __future__ import unicode_literals
import mimetypes
import os
import socket
import sys
import threading
import uuid
from functools import wraps
from os.path import splitext
import six
from six.moves.urllib.parse import urlparse
import requests
import ruamel.yaml as yaml
from flask import Flask, request, jsonify, Response, render_template
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = str(uuid.uuid4())
socketio = SocketIO(app, async_mode='threading')
all_downloads = {}
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.8', 1))
IP = '.'.join(s.getsockname()[0].split(".")[0:-1])
except Exception:
IP = '192.168.0'
print("Cannot detect local IP, instead using: 192.168.0", file=sys.stderr)
finally:
s.close()
return IP
def load_conf():
directory = os.path.split(os.path.realpath(__file__))[0]
with open(os.path.join(directory, "config.yml"), "r") as stream:
try:
return yaml.safe_load(stream)
except yaml.YAMLError as e:
raise SystemExit(e)
conf = load_conf()
allowed_ip_prefix = get_ip()
try:
import youtube_dl
ydl_installed = True
except ImportError:
ydl_installed = False
def can_access(username, password):
users = conf["users"]
for user_settings in users:
if username == user_settings["username"] and password == user_settings["password"]:
return True
return False
def is_allowed_ip(ip):
return conf["local_network_without_login"] and (ip.startswith(allowed_ip_prefix) or ip == "127.0.0.1")
def authenticate():
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
ip = request.remote_addr
if not is_allowed_ip(ip) and (not auth or not can_access(auth.username, auth.password)):
return authenticate()
return f(*args, **kwargs)
return decorated
@app.route('/')
@requires_auth
def login_page():
return render_template('index.html')
@app.route('/downloads/')
@requires_auth
def downloads():
return render_template('downloads.html')
@app.route('/download/', methods=['POST'])
@requires_auth
def download():
try:
data = request.get_json()
if data is None:
raise Exception("Missing or wrong Content-Type, should be: application/json")
data["user"] = request.authorization.username if (request.authorization is not None) else None
extension = get_extension(data["url"])
data["extension"] = extension
threading.Thread(target=lambda: download_in_background(data)).start()
result = {"success": True}
return jsonify(result)
except Exception as e:
result = {"error": True, "reason": str(e)}
return jsonify(result), 500
@socketio.on("get_downloads")
def get_downloads():
return all_downloads
def send_pushbullet_notification(curr_user, filename):
try:
from pushbullet import Pushbullet
pb = Pushbullet(curr_user["pushbullet_token"])
device = pb.devices[0] if app.testing else None
pb.push_note("MiniRemoteDownloader - Download finished", filename, device)
return True
except ImportError:
print("Cant notify via PushBullet. Missing librabry. Install via pip install pushbullet.py",
file=sys.stderr)
return False
except Exception:
print("Invalid Api key for PushBullet. Please insert correct one.", file=sys.stderr)
return False
def on_complete(user, filename):
if not user:
return
if six.PY2:
curr_user = filter(lambda person: person['username'] == user, conf["users"])[0]
else:
curr_user = next(filter(lambda person: person['username'] == user, conf["users"]))
if curr_user["notify_via_pushbullet"]:
return send_pushbullet_notification(curr_user, filename)
def download_with_ydl(data, url, name, path):
if name:
out_tmpl = path
else:
out_tmpl = os.path.join(path, "%(title)s.%(ext)s")
ydl_options = {
"outtmpl": out_tmpl,
'progress_hooks': [progress_hook]
}
if "audioOnly" in data:
ydl_options["out_format"] = "bestaudio/best"
ydl_options["postprocessors"] = [{
'key': 'FFmpegExtractAudio',
'preferredcodec': conf["audio_codec"],
'preferredquality': str(conf["audio_kbps"]),
}]
else:
ydl_options["out_format"] = "best[ext=mp4]/best"
ydl = youtube_dl.YoutubeDL(ydl_options)
with ydl:
info = ydl.extract_info(url=url, download=True)
on_complete(data["user"], info["title"])
def standard_download(data, url, name, path):
r = requests.get(url)
with open(path, "wb+") as code:
code.write(r.content)
on_complete(data["user"], name)
def get_name(data):
url = data["url"]
name = ""
if "name" in data:
name = data["name"] + data["extension"]
elif "youtube" not in url and "oload" not in url:
name = url.split("/")[-1] + data["extension"]
return name
def get_path(data, name):
if "category" in data:
path = os.path.join(conf["path"], data["category"])
if not os.path.exists(path):
os.makedirs(path)
path = os.path.join(path, name)
else:
path = os.path.join(conf["path"], name)
return path
def download_in_background(data):
url = data["url"]
name = get_name(data)
path = get_path(data, name)
if "youtube" in url or "oload" in url:
download_with_ydl(data, url, name, path)
else:
standard_download(data, url, name, path)
return True
def progress_hook(data):
filename = data["filename"]
resp = {}
if filename not in all_downloads.keys():
socketio.emit("new_download", {"filename": filename, "size": data["_total_bytes_str"]})
resp["status"] = data["status"]
resp["filename"] = filename
if data['status'] == 'finished' and filename in all_downloads.keys():
del all_downloads[filename]
elif data['status'] == 'downloading':
resp["eta"] = data["_eta_str"]
resp["progress"] = data["_percent_str"]
resp["speed"] = data["_speed_str"]
all_downloads[filename] = data
socketio.emit(filename, resp)
def already_has_extension(url):
parsed = urlparse(url)
ext = splitext(parsed.path)[1]
return len(ext) != 0
def get_extension_from_content_type(url):
try:
res = requests.get(url)
content_type = res.headers['content-type']
return mimetypes.guess_extension(content_type)
except requests.ConnectionError:
raise Exception("Invalid or malformed URL")
def get_extension(url):
if not already_has_extension(url):
if is_streaming_site(url):
if not ydl_installed:
raise Exception("Trying to download from Youtube/Openload without youtube-dl library installed")
else:
return ".mp4"
else:
return get_extension_from_content_type(url)
return ""
def is_streaming_site(url):
video_urls = ["oload", "openload", "youtube", "youtu"]
for site in video_urls:
if site in url:
return True
return False
if __name__ == '__main__':
socketio.run(app, host="0.0.0.0", port=9000)