Update app.py
Browse files
app.py
CHANGED
@@ -20,7 +20,7 @@ class MediaDownloader:
|
|
20 |
|
21 |
def create_directories(self):
|
22 |
self.image_path = os.path.join(self.temp_dir, "images")
|
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 |
|
@@ -30,7 +30,31 @@ class MediaDownloader:
|
|
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': 'bestvideo+bestaudio/best',
|
@@ -46,34 +70,11 @@ class MediaDownloader:
|
|
46 |
'Accept-Language': 'en-US,en;q=0.5',
|
47 |
'Accept-Encoding': 'gzip, deflate, br',
|
48 |
'Connection': 'keep-alive',
|
49 |
-
'Upgrade-Insecure-Requests': '1'
|
50 |
-
'Sec-Fetch-Dest': 'document',
|
51 |
-
'Sec-Fetch-Mode': 'navigate',
|
52 |
-
'Sec-Fetch-Site': 'none',
|
53 |
-
'Sec-Fetch-User': '?1'
|
54 |
}
|
55 |
}
|
56 |
|
57 |
-
if '
|
58 |
-
ydl_opts.update({
|
59 |
-
'format': 'best',
|
60 |
-
'force_generic_extractor': False,
|
61 |
-
'extract_flat': False,
|
62 |
-
'quiet': False,
|
63 |
-
'no_warnings': False,
|
64 |
-
'socket_timeout': 60,
|
65 |
-
'retries': 10,
|
66 |
-
'cookiefile': None,
|
67 |
-
'extractor_args': {
|
68 |
-
'tiktok': {
|
69 |
-
'embed_api': True,
|
70 |
-
'api_hostname': 'api16-normal-c-useast1a.tiktokv.com',
|
71 |
-
'download_api': True,
|
72 |
-
'allow_redirects': True
|
73 |
-
}
|
74 |
-
}
|
75 |
-
})
|
76 |
-
elif 'instagram.com' in url:
|
77 |
ydl_opts.update({
|
78 |
'format': 'best',
|
79 |
'extract_flat': True,
|
@@ -116,10 +117,7 @@ class MediaDownloader:
|
|
116 |
'Accept': 'image/webp,*/*',
|
117 |
'Accept-Language': 'en-US,en;q=0.5',
|
118 |
'Accept-Encoding': 'gzip, deflate, br',
|
119 |
-
'Connection': 'keep-alive'
|
120 |
-
'Sec-Fetch-Dest': 'image',
|
121 |
-
'Sec-Fetch-Mode': 'no-cors',
|
122 |
-
'Sec-Fetch-Site': 'cross-site'
|
123 |
}
|
124 |
|
125 |
for img_url in j.urls:
|
|
|
20 |
|
21 |
def create_directories(self):
|
22 |
self.image_path = os.path.join(self.temp_dir, "images")
|
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 |
|
|
|
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',
|
|
|
70 |
'Accept-Language': 'en-US,en;q=0.5',
|
71 |
'Accept-Encoding': 'gzip, deflate, br',
|
72 |
'Connection': 'keep-alive',
|
73 |
+
'Upgrade-Insecure-Requests': '1'
|
|
|
|
|
|
|
|
|
74 |
}
|
75 |
}
|
76 |
|
77 |
+
if 'instagram.com' in url:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
ydl_opts.update({
|
79 |
'format': 'best',
|
80 |
'extract_flat': True,
|
|
|
117 |
'Accept': 'image/webp,*/*',
|
118 |
'Accept-Language': 'en-US,en;q=0.5',
|
119 |
'Accept-Encoding': 'gzip, deflate, br',
|
120 |
+
'Connection': 'keep-alive'
|
|
|
|
|
|
|
121 |
}
|
122 |
|
123 |
for img_url in j.urls:
|