48leewsypc commited on
Commit
f05937f
·
verified ·
1 Parent(s): ab2e2c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -24
app.py CHANGED
@@ -1,11 +1,15 @@
1
  from flask import Flask, render_template, request, send_file, jsonify
2
- from pytube import YouTube
3
  from yt_dlp import YoutubeDL
4
  import instaloader
5
  import os
6
  import uuid
7
  import shutil
8
  from waitress import serve
 
 
 
 
 
9
 
10
  app = Flask(__name__)
11
 
@@ -36,20 +40,43 @@ def download():
36
 
37
  def download_youtube(url):
38
  try:
 
 
 
39
  ydl_opts = {
40
- 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
 
41
  'nocheckcertificate': True,
42
- 'no_warnings': True,
43
  'quiet': True,
44
- 'extract_flat': False
 
 
 
 
 
 
45
  }
46
 
47
  with YoutubeDL(ydl_opts) as ydl:
48
  info = ydl.extract_info(url, download=True)
49
- filename = ydl.prepare_filename(info)
50
- return send_file(filename, as_attachment=True, download_name=f"{info['title']}.mp4")
 
 
 
 
 
 
 
51
  except Exception as e:
52
  return jsonify({'error': f'YouTube download failed: {str(e)}'}), 500
 
 
 
 
 
 
53
 
54
  def download_instagram(url):
55
  try:
@@ -57,21 +84,33 @@ def download_instagram(url):
57
  temp_dir = f"downloads/insta_{uuid.uuid4().hex}"
58
  os.makedirs(temp_dir, exist_ok=True)
59
 
60
- shortcode = url.split("/p/")[1].split("/")[0] if "/p/" in url else url.split("/reel/")[1].split("/")[0]
61
- post = instaloader.Post.from_shortcode(L.context, shortcode)
62
- L.download_post(post, target=temp_dir)
 
 
 
 
63
 
64
- for file in os.listdir(temp_dir):
65
- if file.endswith('.mp4'):
66
- filepath = os.path.join(temp_dir, file)
67
- return send_file(filepath, as_attachment=True)
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- raise Exception("No video found in this post")
70
  except Exception as e:
71
  return jsonify({'error': f'Instagram download failed: {str(e)}'}), 500
72
- finally:
73
- if os.path.exists(temp_dir):
74
- shutil.rmtree(temp_dir)
75
 
76
  def download_tiktok(url):
77
  try:
@@ -81,24 +120,40 @@ def download_tiktok(url):
81
  ydl_opts = {
82
  'format': 'best',
83
  'outtmpl': filepath,
84
- 'quiet': True
 
 
85
  }
86
 
87
  with YoutubeDL(ydl_opts) as ydl:
88
  ydl.download([url])
89
- return send_file(filepath, as_attachment=True)
 
 
 
 
 
 
 
 
90
  except Exception as e:
91
  return jsonify({'error': f'TikTok download failed: {str(e)}'}), 500
 
 
 
 
 
 
92
 
93
  @app.after_request
94
  def cleanup(response):
95
- for file in os.listdir('downloads'):
96
- filepath = os.path.join('downloads', file)
97
- try:
98
  if os.path.isfile(filepath):
99
  os.remove(filepath)
100
- except Exception:
101
- pass
102
  return response
103
 
104
  if __name__ == "__main__":
 
1
  from flask import Flask, render_template, request, send_file, jsonify
 
2
  from yt_dlp import YoutubeDL
3
  import instaloader
4
  import os
5
  import uuid
6
  import shutil
7
  from waitress import serve
8
+ import ssl
9
+ import certifi
10
+
11
+ # Disable SSL verification warnings
12
+ ssl._create_default_https_context = ssl._create_unverified_context
13
 
14
  app = Flask(__name__)
15
 
 
40
 
41
  def download_youtube(url):
42
  try:
43
+ filename = f"youtube_{uuid.uuid4().hex}.mp4"
44
+ filepath = os.path.join('downloads', filename)
45
+
46
  ydl_opts = {
47
+ 'format': 'best',
48
+ 'outtmpl': filepath,
49
  'nocheckcertificate': True,
50
+ 'no_check_certificates': True,
51
  'quiet': True,
52
+ 'no_warnings': True,
53
+ 'ignoreerrors': True,
54
+ 'extract_flat': True,
55
+ 'force_generic_extractor': True,
56
+ 'http_headers': {
57
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
58
+ }
59
  }
60
 
61
  with YoutubeDL(ydl_opts) as ydl:
62
  info = ydl.extract_info(url, download=True)
63
+ if info:
64
+ return send_file(
65
+ filepath,
66
+ as_attachment=True,
67
+ download_name=f"{info.get('title', 'video')}.mp4"
68
+ )
69
+ else:
70
+ raise Exception("Failed to extract video information")
71
+
72
  except Exception as e:
73
  return jsonify({'error': f'YouTube download failed: {str(e)}'}), 500
74
+ finally:
75
+ if os.path.exists(filepath):
76
+ try:
77
+ os.remove(filepath)
78
+ except:
79
+ pass
80
 
81
  def download_instagram(url):
82
  try:
 
84
  temp_dir = f"downloads/insta_{uuid.uuid4().hex}"
85
  os.makedirs(temp_dir, exist_ok=True)
86
 
87
+ try:
88
+ if "/reel/" in url:
89
+ shortcode = url.split("/reel/")[1].split("/")[0]
90
+ elif "/p/" in url:
91
+ shortcode = url.split("/p/")[1].split("/")[0]
92
+ else:
93
+ raise ValueError("Invalid Instagram URL")
94
 
95
+ post = instaloader.Post.from_shortcode(L.context, shortcode)
96
+ L.download_post(post, target=temp_dir)
97
+
98
+ for file in os.listdir(temp_dir):
99
+ if file.endswith('.mp4'):
100
+ filepath = os.path.join(temp_dir, file)
101
+ return send_file(
102
+ filepath,
103
+ as_attachment=True,
104
+ download_name=f"instagram_{shortcode}.mp4"
105
+ )
106
+
107
+ raise Exception("No video found in this post")
108
+ finally:
109
+ if os.path.exists(temp_dir):
110
+ shutil.rmtree(temp_dir, ignore_errors=True)
111
 
 
112
  except Exception as e:
113
  return jsonify({'error': f'Instagram download failed: {str(e)}'}), 500
 
 
 
114
 
115
  def download_tiktok(url):
116
  try:
 
120
  ydl_opts = {
121
  'format': 'best',
122
  'outtmpl': filepath,
123
+ 'quiet': True,
124
+ 'no_warnings': True,
125
+ 'nocheckcertificate': True
126
  }
127
 
128
  with YoutubeDL(ydl_opts) as ydl:
129
  ydl.download([url])
130
+ if os.path.exists(filepath):
131
+ return send_file(
132
+ filepath,
133
+ as_attachment=True,
134
+ download_name=f"tiktok_video.mp4"
135
+ )
136
+ else:
137
+ raise Exception("Failed to download video")
138
+
139
  except Exception as e:
140
  return jsonify({'error': f'TikTok download failed: {str(e)}'}), 500
141
+ finally:
142
+ if os.path.exists(filepath):
143
+ try:
144
+ os.remove(filepath)
145
+ except:
146
+ pass
147
 
148
  @app.after_request
149
  def cleanup(response):
150
+ try:
151
+ for file in os.listdir('downloads'):
152
+ filepath = os.path.join('downloads', file)
153
  if os.path.isfile(filepath):
154
  os.remove(filepath)
155
+ except:
156
+ pass
157
  return response
158
 
159
  if __name__ == "__main__":