Kalp97 commited on
Commit
89f48e2
·
verified ·
1 Parent(s): 1b24485

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -5
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  import whisper
3
  import yt_dlp
 
 
4
 
5
  models = {}
6
 
@@ -51,19 +53,34 @@ def download_from_url(url):
51
  'preferredcodec': 'mp3',
52
  'preferredquality': '192',
53
  }],
54
- # Rotate user agents to avoid blocks
55
  'http_headers': {
56
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
 
 
 
 
 
 
57
  },
58
- 'cookiefile': None,
59
  'socket_timeout': 30,
 
 
 
60
  }
61
  try:
62
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
63
  info = ydl.extract_info(url, download=True)
64
  title = info.get('title', 'video')
65
  except Exception as e:
66
- raise Exception(f"Could not download from URL: {str(e)}")
 
 
 
 
 
 
 
 
67
 
68
  # Find the downloaded mp3
69
  for f in os.listdir(tmp_dir):
@@ -161,8 +178,17 @@ def download_video_only(url):
161
  'no_warnings': True,
162
  'merge_output_format': 'mp4',
163
  'http_headers': {
164
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
 
 
 
165
  },
 
 
 
 
 
 
166
  }
167
  try:
168
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
 
1
  import gradio as gr
2
  import whisper
3
  import yt_dlp
4
+ import os
5
+ import tempfile
6
 
7
  models = {}
8
 
 
53
  'preferredcodec': 'mp3',
54
  'preferredquality': '192',
55
  }],
 
56
  'http_headers': {
57
+ 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1',
58
+ 'Accept-Language': 'en-US,en;q=0.9',
59
+ 'Accept': '*/*',
60
+ 'Referer': 'https://www.instagram.com/',
61
+ },
62
+ 'extractor_args': {
63
+ 'instagram': {'api_version': 'v1'},
64
  },
 
65
  'socket_timeout': 30,
66
+ 'retries': 3,
67
+ 'ignoreerrors': False,
68
+ 'geo_bypass': True,
69
  }
70
  try:
71
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
72
  info = ydl.extract_info(url, download=True)
73
  title = info.get('title', 'video')
74
  except Exception as e:
75
+ err = str(e)
76
+ if 'instagram' in err.lower() or '401' in err or '403' in err:
77
+ raise Exception("Instagram blocked this request. Try again in a few seconds, or make sure the post is public.")
78
+ elif 'private' in err.lower():
79
+ raise Exception("This account is private. Only public posts can be downloaded.")
80
+ elif 'not found' in err.lower() or '404' in err:
81
+ raise Exception("Video not found. Check the URL and make sure the post still exists.")
82
+ else:
83
+ raise Exception(f"Download failed: {err}")
84
 
85
  # Find the downloaded mp3
86
  for f in os.listdir(tmp_dir):
 
178
  'no_warnings': True,
179
  'merge_output_format': 'mp4',
180
  'http_headers': {
181
+ 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1',
182
+ 'Accept-Language': 'en-US,en;q=0.9',
183
+ 'Accept': '*/*',
184
+ 'Referer': 'https://www.instagram.com/',
185
  },
186
+ 'extractor_args': {
187
+ 'instagram': {'api_version': 'v1'},
188
+ },
189
+ 'socket_timeout': 30,
190
+ 'retries': 3,
191
+ 'geo_bypass': True,
192
  }
193
  try:
194
  with yt_dlp.YoutubeDL(ydl_opts) as ydl: