coollsd commited on
Commit
1168724
·
verified ·
1 Parent(s): 8a25d70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -45
app.py CHANGED
@@ -30,38 +30,14 @@ class MediaDownloader:
30
  except Exception as e:
31
  print(f"Cleanup error: {str(e)}")
32
 
33
- def download_tiktok(self, url):
34
- try:
35
- api_url = "https://ssstik.io/abc?url=" + url
36
- headers = {
37
- "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"
38
- }
39
- response = requests.get(api_url, headers=headers)
40
- if response.status_code == 200:
41
- video_url = response.json().get("video_url")
42
- if video_url:
43
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
44
- output_path = os.path.join(self.video_path, f"tiktok_{timestamp}.mp4")
45
- video_response = requests.get(video_url, headers=headers)
46
- with open(output_path, "wb") as f:
47
- f.write(video_response.content)
48
- return output_path
49
- return None
50
- except Exception as e:
51
- print(f"TikTok download error: {str(e)}")
52
- return None
53
-
54
  def download_video(self, url):
55
- if 'tiktok.com' in url:
56
- return self.download_tiktok(url)
57
-
58
  output_template = os.path.join(self.video_path, '%(title)s.%(ext)s')
59
  ydl_opts = {
60
- 'format': 'bestvideo+bestaudio/best',
61
  'outtmpl': output_template,
62
  'ignoreerrors': True,
63
  'no_warnings': True,
64
- 'quiet': False,
65
  'extract_flat': False,
66
  'socket_timeout': 30,
67
  'http_headers': {
@@ -74,7 +50,16 @@ class MediaDownloader:
74
  }
75
  }
76
 
77
- if 'instagram.com' in url:
 
 
 
 
 
 
 
 
 
78
  ydl_opts.update({
79
  'format': 'best',
80
  'extract_flat': True,
@@ -155,28 +140,21 @@ class MediaDownloader:
155
  if not url.startswith(('http://', 'https://')):
156
  url = 'https://' + url
157
 
158
- if 'instagram.com' in url:
159
- video_path = self.download_video(url)
160
- if video_path:
161
- return [video_path]
162
- image_paths = self.download_images(url)
163
- if image_paths:
164
- return image_paths
165
- else:
166
- video_path = self.download_video(url)
167
- if video_path:
168
- return [video_path]
169
- image_paths = self.download_images(url)
170
- if image_paths:
171
- return image_paths
172
 
173
  return None
174
 
175
  @app.route('/')
176
  def home():
177
  return """
178
- <h1>download</h1>
179
- <p>Use: /download?url=url</p>
180
  """
181
 
182
  @app.route('/download')
@@ -184,7 +162,7 @@ def download():
184
  try:
185
  url = request.args.get('url')
186
  if not url:
187
- return "No URL", 400
188
 
189
  downloader = MediaDownloader()
190
 
@@ -220,7 +198,7 @@ def download():
220
  return response
221
  else:
222
  downloader.cleanup()
223
- return "Failed to download", 400
224
 
225
  except Exception as e:
226
  downloader.cleanup()
 
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')
35
  ydl_opts = {
36
+ 'format': 'best',
37
  'outtmpl': output_template,
38
  'ignoreerrors': True,
39
  'no_warnings': True,
40
+ 'quiet': True,
41
  'extract_flat': False,
42
  'socket_timeout': 30,
43
  'http_headers': {
 
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,
 
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]
146
+
147
+ image_paths = self.download_images(url)
148
+ if image_paths:
149
+ return image_paths
 
 
 
 
 
 
 
150
 
151
  return None
152
 
153
  @app.route('/')
154
  def home():
155
  return """
156
+ <h1>Social Media Downloader API</h1>
157
+ <p>Use: /download?url=YOUR_URL_HERE</p>
158
  """
159
 
160
  @app.route('/download')
 
162
  try:
163
  url = request.args.get('url')
164
  if not url:
165
+ return "No URL provided", 400
166
 
167
  downloader = MediaDownloader()
168
 
 
198
  return response
199
  else:
200
  downloader.cleanup()
201
+ return "Failed to download media", 400
202
 
203
  except Exception as e:
204
  downloader.cleanup()