Spaces:
Build error
Build error
Update srv.py
Browse files
srv.py
CHANGED
|
@@ -1,78 +1,76 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, send_file
|
| 2 |
-
import
|
| 3 |
import os
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
@app.route('/
|
| 8 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
data = request.json
|
| 10 |
url = data.get('url')
|
| 11 |
-
quality = data.get('quality')
|
| 12 |
-
cookies = data.get('cookies')
|
| 13 |
-
|
| 14 |
-
if not url or not quality:
|
| 15 |
-
return jsonify({'error': 'URL and quality are required'}), 400
|
| 16 |
-
|
| 17 |
-
# Define quality options
|
| 18 |
-
quality_options = {
|
| 19 |
-
'144': '144p',
|
| 20 |
-
'240': '240p',
|
| 21 |
-
'360': '360p',
|
| 22 |
-
'480': '480p',
|
| 23 |
-
'720': '720p',
|
| 24 |
-
'1080': '1080p'
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
if quality not in quality_options:
|
| 28 |
-
return jsonify({'error': 'Invalid quality option'}), 400
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
with open(cookie_file, 'w') as f:
|
| 33 |
-
f.write(cookies)
|
| 34 |
-
|
| 35 |
-
# Download video options
|
| 36 |
-
ydl_opts = {
|
| 37 |
-
'format': f'bestvideo[height<={quality}]',
|
| 38 |
-
'outtmpl': '%(title)s.%(ext)s',
|
| 39 |
-
'cookiefile': cookie_file,
|
| 40 |
-
'noplaylist': True,
|
| 41 |
-
}
|
| 42 |
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
return jsonify({'error': str(e)}), 500
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
url = request.args.get('url')
|
| 55 |
|
| 56 |
if not url:
|
| 57 |
return jsonify({'error': 'URL is required'}), 400
|
| 58 |
|
| 59 |
try:
|
| 60 |
ydl_opts = {
|
| 61 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
}
|
| 63 |
|
| 64 |
-
with
|
| 65 |
-
info = ydl.extract_info(url, download=
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
return jsonify({'error': str(e)}), 500
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
if __name__ == '__main__':
|
| 78 |
-
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_file, render_template
|
| 2 |
+
from yt_dlp import YoutubeDL
|
| 3 |
import os
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
+
@app.route('/')
|
| 8 |
+
def home():
|
| 9 |
+
return render_template('index.html')
|
| 10 |
+
|
| 11 |
+
@app.route('/info', methods=['POST'])
|
| 12 |
+
def info():
|
| 13 |
data = request.json
|
| 14 |
url = data.get('url')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
if not url:
|
| 17 |
+
return jsonify({'error': 'URL is required'}), 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
try:
|
| 20 |
+
ydl_opts = {
|
| 21 |
+
'cookiefile': 'www.youtube.com_cookies.txt'
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
with YoutubeDL(ydl_opts) as ydl:
|
| 25 |
+
info = ydl.extract_info(url, download=False)
|
| 26 |
+
return jsonify({
|
| 27 |
+
'title': info['title'],
|
| 28 |
+
'thumbnail': info.get('thumbnail'),
|
| 29 |
+
'duration': info.get('duration'),
|
| 30 |
+
'channel': info.get('channel')
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
except Exception as e:
|
| 34 |
return jsonify({'error': str(e)}), 500
|
| 35 |
|
| 36 |
+
@app.route('/ytmp3', methods=['POST'])
|
| 37 |
+
def ytmp3():
|
| 38 |
+
data = request.json
|
| 39 |
+
url = data.get('url')
|
|
|
|
| 40 |
|
| 41 |
if not url:
|
| 42 |
return jsonify({'error': 'URL is required'}), 400
|
| 43 |
|
| 44 |
try:
|
| 45 |
ydl_opts = {
|
| 46 |
+
'format': '251/bestaudio', # Format untuk audio terbaik
|
| 47 |
+
'outtmpl': '%(title)s.%(ext)s',
|
| 48 |
+
'cookiefile': 'www.youtube.com_cookies.txt',
|
| 49 |
+
'postprocessors': [{
|
| 50 |
+
'key': 'FFmpegExtractAudio',
|
| 51 |
+
'preferredcodec': 'mp3',
|
| 52 |
+
'preferredquality': '128',
|
| 53 |
+
}],
|
| 54 |
}
|
| 55 |
|
| 56 |
+
with YoutubeDL(ydl_opts) as ydl:
|
| 57 |
+
info = ydl.extract_info(url, download=True)
|
| 58 |
+
file_name = ydl.prepare_filename(info).rsplit(".", 1)[0] + ".mp3"
|
| 59 |
+
|
| 60 |
+
# Kirim file ke client
|
| 61 |
+
return send_file(
|
| 62 |
+
file_name,
|
| 63 |
+
as_attachment=True,
|
| 64 |
+
download_name=os.path.basename(file_name)
|
| 65 |
+
)
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
return jsonify({'error': str(e)}), 500
|
| 69 |
|
| 70 |
+
finally:
|
| 71 |
+
# Bersihkan file setelah dikirim
|
| 72 |
+
if 'file_name' in locals() and os.path.exists(file_name):
|
| 73 |
+
os.remove(file_name)
|
| 74 |
+
|
| 75 |
if __name__ == '__main__':
|
| 76 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|