Spaces:
Build error
Build error
Update srv.py
Browse files
srv.py
CHANGED
|
@@ -2,7 +2,6 @@ from flask import Flask, request, jsonify, send_file, render_template
|
|
| 2 |
from yt_dlp import YoutubeDL
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
@app.route('/')
|
|
@@ -34,7 +33,6 @@ def get_info():
|
|
| 34 |
except Exception as e:
|
| 35 |
return jsonify({'error': str(e)}), 500
|
| 36 |
|
| 37 |
-
@app.route('/download', methods=['POST'])
|
| 38 |
@app.route('/download', methods=['POST'])
|
| 39 |
def download():
|
| 40 |
data = request.json
|
|
@@ -45,6 +43,8 @@ def download():
|
|
| 45 |
if not url:
|
| 46 |
return jsonify({'error': 'URL is required'}), 400
|
| 47 |
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
if format_type == 'mp3':
|
| 50 |
ydl_opts = {
|
|
@@ -57,7 +57,7 @@ def download():
|
|
| 57 |
'preferredquality': '192',
|
| 58 |
}],
|
| 59 |
}
|
| 60 |
-
else:
|
| 61 |
quality_map = {
|
| 62 |
'144': 'bv*[height=144][ext=mp4]+ba[ext=m4a]/b[height=144][ext=mp4]',
|
| 63 |
'240': 'bv*[height=240][ext=mp4]+ba[ext=m4a]/b[height=240][ext=mp4]',
|
|
@@ -66,9 +66,9 @@ def download():
|
|
| 66 |
'720': 'bv*[height=720][ext=mp4]+ba[ext=m4a]/b[height=720][ext=mp4]',
|
| 67 |
'1080': 'bv*[height=1080][ext=mp4]+ba[ext=m4a]/b[height=1080][ext=mp4]',
|
| 68 |
'best': 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]',
|
| 69 |
-
}
|
| 70 |
|
| 71 |
-
selected_quality = quality_map.get(quality, quality_map['best']) + '?'
|
| 72 |
|
| 73 |
ydl_opts = {
|
| 74 |
'format': selected_quality,
|
|
@@ -89,15 +89,15 @@ def download():
|
|
| 89 |
file_name,
|
| 90 |
as_attachment=True,
|
| 91 |
download_name=os.path.basename(file_name),
|
| 92 |
-
mimetype='
|
| 93 |
)
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
return jsonify({'error': str(e)}), 500
|
| 97 |
|
| 98 |
finally:
|
| 99 |
-
if
|
| 100 |
os.remove(file_name)
|
| 101 |
|
| 102 |
if __name__ == '__main__':
|
| 103 |
-
|
|
|
|
| 2 |
from yt_dlp import YoutubeDL
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
@app.route('/')
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
return jsonify({'error': str(e)}), 500
|
| 35 |
|
|
|
|
| 36 |
@app.route('/download', methods=['POST'])
|
| 37 |
def download():
|
| 38 |
data = request.json
|
|
|
|
| 43 |
if not url:
|
| 44 |
return jsonify({'error': 'URL is required'}), 400
|
| 45 |
|
| 46 |
+
file_name = None
|
| 47 |
+
|
| 48 |
try:
|
| 49 |
if format_type == 'mp3':
|
| 50 |
ydl_opts = {
|
|
|
|
| 57 |
'preferredquality': '192',
|
| 58 |
}],
|
| 59 |
}
|
| 60 |
+
else:
|
| 61 |
quality_map = {
|
| 62 |
'144': 'bv*[height=144][ext=mp4]+ba[ext=m4a]/b[height=144][ext=mp4]',
|
| 63 |
'240': 'bv*[height=240][ext=mp4]+ba[ext=m4a]/b[height=240][ext=mp4]',
|
|
|
|
| 66 |
'720': 'bv*[height=720][ext=mp4]+ba[ext=m4a]/b[height=720][ext=mp4]',
|
| 67 |
'1080': 'bv*[height=1080][ext=mp4]+ba[ext=m4a]/b[height=1080][ext=mp4]',
|
| 68 |
'best': 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]',
|
| 69 |
+
}
|
| 70 |
|
| 71 |
+
selected_quality = quality_map.get(quality, quality_map['best']) + '?'
|
| 72 |
|
| 73 |
ydl_opts = {
|
| 74 |
'format': selected_quality,
|
|
|
|
| 89 |
file_name,
|
| 90 |
as_attachment=True,
|
| 91 |
download_name=os.path.basename(file_name),
|
| 92 |
+
mimetype='audio/mp3' if format_type == 'mp3' else 'video/mp4'
|
| 93 |
)
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
return jsonify({'error': str(e)}), 500
|
| 97 |
|
| 98 |
finally:
|
| 99 |
+
if file_name and os.path.exists(file_name):
|
| 100 |
os.remove(file_name)
|
| 101 |
|
| 102 |
if __name__ == '__main__':
|
| 103 |
+
app.run(host='0.0.0.0', port=7860, debug=True, threaded=True)
|