Update app.py
Browse files
app.py
CHANGED
@@ -8,10 +8,8 @@ from datetime import datetime
|
|
8 |
import tempfile
|
9 |
import shutil
|
10 |
import mimetypes
|
11 |
-
from asgiref.wsgi import WsgiToAsgi
|
12 |
|
13 |
app = Flask(__name__)
|
14 |
-
asgi_app = WsgiToAsgi(app)
|
15 |
|
16 |
class MediaDownloader:
|
17 |
def __init__(self):
|
@@ -25,10 +23,7 @@ class MediaDownloader:
|
|
25 |
os.makedirs(self.video_path, exist_ok=True)
|
26 |
|
27 |
def cleanup(self):
|
28 |
-
|
29 |
-
shutil.rmtree(self.temp_dir)
|
30 |
-
except Exception as e:
|
31 |
-
print(f"Cleanup error: {str(e)}")
|
32 |
|
33 |
def download_video(self, url):
|
34 |
output_template = os.path.join(self.video_path, '%(title)s.%(ext)s')
|
@@ -39,36 +34,11 @@ class MediaDownloader:
|
|
39 |
'no_warnings': True,
|
40 |
'quiet': True,
|
41 |
'extract_flat': False,
|
42 |
-
'socket_timeout': 30,
|
43 |
'http_headers': {
|
44 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
|
45 |
-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
46 |
-
'Accept-Language': 'en-US,en;q=0.5',
|
47 |
-
'Accept-Encoding': 'gzip, deflate, br',
|
48 |
-
'Connection': 'keep-alive',
|
49 |
-
'Upgrade-Insecure-Requests': '1'
|
50 |
}
|
51 |
}
|
52 |
|
53 |
-
if 'tiktok.com' in url:
|
54 |
-
ydl_opts.update({
|
55 |
-
'format': 'best',
|
56 |
-
'quiet': False,
|
57 |
-
'no_warnings': False,
|
58 |
-
'extractor_args': {'TikTok': {'download_api': True}},
|
59 |
-
'force_generic_extractor': False,
|
60 |
-
'cookiesfrombrowser': None
|
61 |
-
})
|
62 |
-
elif 'instagram.com' in url:
|
63 |
-
ydl_opts.update({
|
64 |
-
'format': 'best',
|
65 |
-
'extract_flat': True,
|
66 |
-
'quiet': False,
|
67 |
-
'no_warnings': False,
|
68 |
-
'socket_timeout': 30,
|
69 |
-
'retries': 10
|
70 |
-
})
|
71 |
-
|
72 |
try:
|
73 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
74 |
info = ydl.extract_info(url, download=True)
|
@@ -76,8 +46,7 @@ class MediaDownloader:
|
|
76 |
filename = ydl.prepare_filename(info)
|
77 |
return filename if os.path.exists(filename) else None
|
78 |
return None
|
79 |
-
except
|
80 |
-
print(f"Video download error: {str(e)}")
|
81 |
return None
|
82 |
|
83 |
def download_images(self, url):
|
@@ -97,22 +66,11 @@ class MediaDownloader:
|
|
97 |
return None
|
98 |
|
99 |
downloaded_files = []
|
100 |
-
headers = {
|
101 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
|
102 |
-
'Accept': 'image/webp,*/*',
|
103 |
-
'Accept-Language': 'en-US,en;q=0.5',
|
104 |
-
'Accept-Encoding': 'gzip, deflate, br',
|
105 |
-
'Connection': 'keep-alive'
|
106 |
-
}
|
107 |
-
|
108 |
for img_url in j.urls:
|
109 |
try:
|
110 |
-
response = requests.get(
|
111 |
-
|
112 |
-
|
113 |
-
timeout=30,
|
114 |
-
allow_redirects=True
|
115 |
-
)
|
116 |
response.raise_for_status()
|
117 |
|
118 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
@@ -137,9 +95,6 @@ class MediaDownloader:
|
|
137 |
return None
|
138 |
|
139 |
def download_media(self, url):
|
140 |
-
if not url.startswith(('http://', 'https://')):
|
141 |
-
url = 'https://' + url
|
142 |
-
|
143 |
video_path = self.download_video(url)
|
144 |
if video_path:
|
145 |
return [video_path]
|
@@ -153,8 +108,8 @@ class MediaDownloader:
|
|
153 |
@app.route('/')
|
154 |
def home():
|
155 |
return """
|
156 |
-
<h1>
|
157 |
-
<p>Use: /download?url=
|
158 |
"""
|
159 |
|
160 |
@app.route('/download')
|
@@ -162,7 +117,7 @@ def download():
|
|
162 |
try:
|
163 |
url = request.args.get('url')
|
164 |
if not url:
|
165 |
-
return "No URL
|
166 |
|
167 |
downloader = MediaDownloader()
|
168 |
|
@@ -171,12 +126,14 @@ def download():
|
|
171 |
|
172 |
if files and len(files) > 0:
|
173 |
if len(files) == 1:
|
|
|
174 |
response = make_response(send_file(files[0], as_attachment=True))
|
175 |
@response.call_on_close
|
176 |
def cleanup():
|
177 |
downloader.cleanup()
|
178 |
return response
|
179 |
else:
|
|
|
180 |
def generate():
|
181 |
for file_path in files:
|
182 |
with open(file_path, 'rb') as f:
|
@@ -198,7 +155,7 @@ def download():
|
|
198 |
return response
|
199 |
else:
|
200 |
downloader.cleanup()
|
201 |
-
return "Failed
|
202 |
|
203 |
except Exception as e:
|
204 |
downloader.cleanup()
|
@@ -208,4 +165,4 @@ def download():
|
|
208 |
return f"Error: {str(e)}", 500
|
209 |
|
210 |
if __name__ == '__main__':
|
211 |
-
app.run(host='0.0.0.0', port=
|
|
|
8 |
import tempfile
|
9 |
import shutil
|
10 |
import mimetypes
|
|
|
11 |
|
12 |
app = Flask(__name__)
|
|
|
13 |
|
14 |
class MediaDownloader:
|
15 |
def __init__(self):
|
|
|
23 |
os.makedirs(self.video_path, exist_ok=True)
|
24 |
|
25 |
def cleanup(self):
|
26 |
+
shutil.rmtree(self.temp_dir)
|
|
|
|
|
|
|
27 |
|
28 |
def download_video(self, url):
|
29 |
output_template = os.path.join(self.video_path, '%(title)s.%(ext)s')
|
|
|
34 |
'no_warnings': True,
|
35 |
'quiet': True,
|
36 |
'extract_flat': False,
|
|
|
37 |
'http_headers': {
|
38 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
|
|
|
|
|
|
|
|
|
|
39 |
}
|
40 |
}
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
try:
|
43 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
44 |
info = ydl.extract_info(url, download=True)
|
|
|
46 |
filename = ydl.prepare_filename(info)
|
47 |
return filename if os.path.exists(filename) else None
|
48 |
return None
|
49 |
+
except:
|
|
|
50 |
return None
|
51 |
|
52 |
def download_images(self, url):
|
|
|
66 |
return None
|
67 |
|
68 |
downloaded_files = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
for img_url in j.urls:
|
70 |
try:
|
71 |
+
response = requests.get(img_url, headers={
|
72 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
73 |
+
})
|
|
|
|
|
|
|
74 |
response.raise_for_status()
|
75 |
|
76 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
|
|
95 |
return None
|
96 |
|
97 |
def download_media(self, url):
|
|
|
|
|
|
|
98 |
video_path = self.download_video(url)
|
99 |
if video_path:
|
100 |
return [video_path]
|
|
|
108 |
@app.route('/')
|
109 |
def home():
|
110 |
return """
|
111 |
+
<h1>downloader</h1>
|
112 |
+
<p>Use: /download?url=url</p>
|
113 |
"""
|
114 |
|
115 |
@app.route('/download')
|
|
|
117 |
try:
|
118 |
url = request.args.get('url')
|
119 |
if not url:
|
120 |
+
return "No URL", 400
|
121 |
|
122 |
downloader = MediaDownloader()
|
123 |
|
|
|
126 |
|
127 |
if files and len(files) > 0:
|
128 |
if len(files) == 1:
|
129 |
+
# Single file download
|
130 |
response = make_response(send_file(files[0], as_attachment=True))
|
131 |
@response.call_on_close
|
132 |
def cleanup():
|
133 |
downloader.cleanup()
|
134 |
return response
|
135 |
else:
|
136 |
+
# Multiple files download
|
137 |
def generate():
|
138 |
for file_path in files:
|
139 |
with open(file_path, 'rb') as f:
|
|
|
155 |
return response
|
156 |
else:
|
157 |
downloader.cleanup()
|
158 |
+
return "Failed", 400
|
159 |
|
160 |
except Exception as e:
|
161 |
downloader.cleanup()
|
|
|
165 |
return f"Error: {str(e)}", 500
|
166 |
|
167 |
if __name__ == '__main__':
|
168 |
+
app.run(host='0.0.0.0', port=8080, debug=True)
|