Update app.py
Browse files
app.py
CHANGED
@@ -17,38 +17,51 @@ class MediaDownloader:
|
|
17 |
def __init__(self):
|
18 |
self.temp_dir = tempfile.mkdtemp()
|
19 |
self.create_directories()
|
|
|
20 |
|
21 |
-
def
|
22 |
-
self.
|
23 |
-
self.video_path = os.path.join(self.temp_dir, "videos")
|
24 |
-
os.makedirs(self.image_path, exist_ok=True)
|
25 |
-
os.makedirs(self.video_path, exist_ok=True)
|
26 |
-
|
27 |
-
def cleanup(self):
|
28 |
-
shutil.rmtree(self.temp_dir)
|
29 |
-
|
30 |
-
def download_video(self, url):
|
31 |
-
output_template = os.path.join(self.video_path, '%(title)s.%(ext)s')
|
32 |
-
ydl_opts = {
|
33 |
'format': 'best',
|
34 |
-
'outtmpl': output_template,
|
35 |
'ignoreerrors': True,
|
36 |
'no_warnings': True,
|
37 |
'quiet': True,
|
38 |
'extract_flat': False,
|
39 |
'http_headers': {
|
40 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
|
|
|
|
|
|
41 |
}
|
42 |
}
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
try:
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
info = ydl.extract_info(url, download=True)
|
47 |
if info:
|
48 |
filename = ydl.prepare_filename(info)
|
49 |
return filename if os.path.exists(filename) else None
|
50 |
return None
|
51 |
-
except:
|
|
|
52 |
return None
|
53 |
|
54 |
def download_images(self, url):
|
@@ -72,7 +85,7 @@ class MediaDownloader:
|
|
72 |
try:
|
73 |
response = requests.get(img_url, headers={
|
74 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
75 |
-
})
|
76 |
response.raise_for_status()
|
77 |
|
78 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
@@ -97,6 +110,10 @@ class MediaDownloader:
|
|
97 |
return None
|
98 |
|
99 |
def download_media(self, url):
|
|
|
|
|
|
|
|
|
100 |
video_path = self.download_video(url)
|
101 |
if video_path:
|
102 |
return [video_path]
|
|
|
17 |
def __init__(self):
|
18 |
self.temp_dir = tempfile.mkdtemp()
|
19 |
self.create_directories()
|
20 |
+
self.setup_yt_dlp()
|
21 |
|
22 |
+
def setup_yt_dlp(self):
|
23 |
+
self.ydl_opts = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
'format': 'best',
|
|
|
25 |
'ignoreerrors': True,
|
26 |
'no_warnings': True,
|
27 |
'quiet': True,
|
28 |
'extract_flat': False,
|
29 |
'http_headers': {
|
30 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
31 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
32 |
+
'Accept-Language': 'en-us,en;q=0.5',
|
33 |
+
'Sec-Fetch-Mode': 'navigate'
|
34 |
}
|
35 |
}
|
36 |
|
37 |
+
def create_directories(self):
|
38 |
+
self.image_path = os.path.join(self.temp_dir, "images")
|
39 |
+
self.video_path = os.path.join(self.temp_dir, "videos")
|
40 |
+
os.makedirs(self.image_path, exist_ok=True)
|
41 |
+
os.makedirs(self.video_path, exist_ok=True)
|
42 |
+
|
43 |
+
def cleanup(self):
|
44 |
try:
|
45 |
+
shutil.rmtree(self.temp_dir)
|
46 |
+
except Exception as e:
|
47 |
+
print(f"Cleanup error: {str(e)}")
|
48 |
+
|
49 |
+
def download_video(self, url):
|
50 |
+
self.ydl_opts['outtmpl'] = os.path.join(self.video_path, '%(title)s.%(ext)s')
|
51 |
+
try:
|
52 |
+
with yt_dlp.YoutubeDL(self.ydl_opts) as ydl:
|
53 |
+
try:
|
54 |
+
ydl.update() # Update yt-dlp before downloading
|
55 |
+
except Exception as e:
|
56 |
+
print(f"Update error: {str(e)}")
|
57 |
+
|
58 |
info = ydl.extract_info(url, download=True)
|
59 |
if info:
|
60 |
filename = ydl.prepare_filename(info)
|
61 |
return filename if os.path.exists(filename) else None
|
62 |
return None
|
63 |
+
except Exception as e:
|
64 |
+
print(f"Video download error: {str(e)}")
|
65 |
return None
|
66 |
|
67 |
def download_images(self, url):
|
|
|
85 |
try:
|
86 |
response = requests.get(img_url, headers={
|
87 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
88 |
+
}, timeout=10)
|
89 |
response.raise_for_status()
|
90 |
|
91 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
|
|
110 |
return None
|
111 |
|
112 |
def download_media(self, url):
|
113 |
+
if 'tiktok.com' in url:
|
114 |
+
# Special handling for TikTok
|
115 |
+
self.ydl_opts['format'] = 'bestvideo+bestaudio/best'
|
116 |
+
|
117 |
video_path = self.download_video(url)
|
118 |
if video_path:
|
119 |
return [video_path]
|