ssiisnsksksnsjsksk commited on
Commit
4e1a8f9
·
verified ·
1 Parent(s): 940c88d

Update srv.py

Browse files
Files changed (1) hide show
  1. srv.py +64 -46
srv.py CHANGED
@@ -13,9 +13,14 @@
13
  from flask import Flask, request, jsonify, send_file, render_template
14
  from yt_dlp import YoutubeDL
15
  import os
 
 
16
 
17
  app = Flask(__name__)
18
 
 
 
 
19
  @app.route('/')
20
  def home():
21
  return render_template('index.html')
@@ -43,64 +48,77 @@ def get_info():
43
  })
44
 
45
  except Exception as e:
 
46
  return jsonify({'error': str(e)}), 500
47
 
48
  @app.route('/download', methods=['POST'])
49
  def download_media():
50
  data = request.json
51
  url = data.get('url')
52
- format_type = data.get('format_type', 'mp3')
53
- quality = str(data.get('quality', 'best'))
54
 
55
  if not url:
56
  return jsonify({'error': 'URL is required'}), 400
57
 
58
- try:
59
- if format_type == 'mp4':
60
- quality_map = {
61
- '144': 'bv*[height=144][ext=mp4]+ba[ext=m4a]/b[height=144][ext=mp4]',
62
- '240': 'bv*[height=240][ext=mp4]+ba[ext=m4a]/b[height=240][ext=mp4]',
63
- '360': 'bv*[height=360][ext=mp4]+ba[ext=m4a]/b[height=360][ext=mp4]',
64
- '480': 'bv*[height=480][ext=mp4]+ba[ext=m4a]/b[height=480][ext=mp4]',
65
- '720': 'bv*[height=720][ext=mp4]+ba[ext=m4a]/b[height=720][ext=mp4]',
66
- '1080': 'bv*[height=1080][ext=mp4]+ba[ext=m4a]/b[height=1080][ext=mp4]',
67
- 'best': 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]'
68
- }
69
-
70
- ydl_opts = {
71
- 'format': quality_map.get(quality, quality_map['best']),
72
- 'merge_output_format': 'mp4',
73
- 'outtmpl': '%(title)s.%(ext)s',
74
- 'cookiefile': 'www.youtube.com_cookies.txt'
75
- }
76
- else:
77
- ydl_opts = {
78
- 'format': 'bestaudio',
79
- 'outtmpl': '%(title)s.%(ext)s',
80
- 'cookiefile': 'www.youtube.com_cookies.txt',
81
- 'postprocessors': [{
82
- 'key': 'FFmpegExtractAudio',
83
- 'preferredcodec': 'mp3',
84
- 'preferredquality': '64',
85
- }],
86
- }
87
- with YoutubeDL(ydl_opts) as ydl:
88
- info = ydl.extract_info(url, download=True)
89
- file_ext = "mp4" if format_type == "mp4" else "mp3"
90
- file_name = ydl.prepare_filename(info).rsplit(".", 1)[0] + f".{file_ext}"
91
 
92
- return send_file(
93
- file_name,
94
- as_attachment=True,
95
- download_name=os.path.basename(file_name)
96
- )
 
 
 
 
 
 
 
 
97
 
98
- except Exception as e:
99
- return jsonify({'error': str(e)}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
- finally:
102
- if 'file_name' in locals() and os.path.exists(file_name):
103
- os.remove(file_name)
104
 
105
  if __name__ == '__main__':
106
- app.run(host='0.0.0.0', port=7860, debug=True ,threaded=True)
 
13
  from flask import Flask, request, jsonify, send_file, render_template
14
  from yt_dlp import YoutubeDL
15
  import os
16
+ import uuid
17
+ import logging
18
 
19
  app = Flask(__name__)
20
 
21
+ # Set up logging
22
+ logging.basicConfig(level=logging.DEBUG)
23
+
24
  @app.route('/')
25
  def home():
26
  return render_template('index.html')
 
48
  })
49
 
50
  except Exception as e:
51
+ app.logger.error(f"Error fetching video info: {str(e)}")
52
  return jsonify({'error': str(e)}), 500
53
 
54
  @app.route('/download', methods=['POST'])
55
  def download_media():
56
  data = request.json
57
  url = data.get('url')
58
+ format_type = data.get('format_type', 'mp3')
59
+ quality = str(data.get('quality', 'best'))
60
 
61
  if not url:
62
  return jsonify({'error': 'URL is required'}), 400
63
 
64
+ max_retries = 3
65
+ retry_delay = 5 # seconds
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ for attempt in range(max_retries):
68
+ try:
69
+ unique_id = str(uuid.uuid4()) # Generate a unique ID for the file
70
+ if format_type == 'mp4':
71
+ quality_map = {
72
+ '144': 'bv*[height=144][ext=mp4]+ba[ext=m4a]/b[height=144][ext=mp4]',
73
+ '240': 'bv*[height=240][ext=mp4]+ba[ext=m4a]/b[height=240][ext=mp4]',
74
+ '360': 'bv*[height=360][ext=mp4]+ba[ext=m4a]/b[height=360][ext=mp4]',
75
+ '480': 'bv*[height=480][ext=mp4]+ba[ext=m4a]/b[height=480][ext=mp4]',
76
+ '720': 'bv*[height=720][ext=mp4]+ba[ext=m4a]/b[height=720][ext=mp4]',
77
+ '1080': 'bv*[height=1080][ext=mp4]+ba[ext=m4a]/b[height=1080][ext=mp4]',
78
+ 'best': 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]'
79
+ }
80
 
81
+ ydl_opts = {
82
+ 'format': quality_map.get(quality, quality_map['best']),
83
+ 'merge_output_format': 'mp4',
84
+ 'outtmpl': f'%(title)s_{unique_id}.%(ext)s', # Unique filename
85
+ 'cookiefile': 'www.youtube.com_cookies.txt'
86
+ }
87
+ else:
88
+ ydl_opts = {
89
+ 'format': 'bestaudio',
90
+ 'outtmpl': f'%(title)s_{unique_id}.%(ext)s', # Unique filename
91
+ 'cookiefile': 'www.youtube.com_cookies.txt',
92
+ 'postprocessors': [{
93
+ 'key': 'FFmpegExtractAudio',
94
+ 'preferredcodec': 'mp3',
95
+ 'preferredquality': '64',
96
+ }],
97
+ }
98
+
99
+ with YoutubeDL(ydl_opts) as ydl:
100
+ info = ydl.extract_info(url, download=True)
101
+ file_ext = "mp4" if format_type == "mp4" else "mp3"
102
+ file_name = ydl.prepare_filename(info).rsplit(".", 1)[0] + f".{file_ext}"
103
+
104
+ return send_file(
105
+ file_name,
106
+ as_attachment=True,
107
+ download_name=os.path.basename(file_name)
108
+ )
109
+
110
+ except Exception as e:
111
+ if attempt < max_retries - 1:
112
+ app.logger.warning(f"Attempt {attempt + 1} failed. Retrying in {retry_delay} seconds...")
113
+ time.sleep(retry_delay)
114
+ continue
115
+ else:
116
+ app.logger.error(f"Error downloading media: {str(e)}")
117
+ return jsonify({'error': str(e)}), 500
118
 
119
+ finally:
120
+ if 'file_name' in locals() and os.path.exists(file_name):
121
+ os.remove(file_name)
122
 
123
  if __name__ == '__main__':
124
+ app.run(host='0.0.0.0', port=7860, debug=True, threaded=True)