prasanth.thangavel
commited on
Commit
·
521848e
1
Parent(s):
c4a7c36
Add firefox cookies to handle bot issue
Browse files- .gitignore +4 -1
- app.py +92 -9
- firefox-cookies.txt +0 -17
.gitignore
CHANGED
@@ -12,4 +12,7 @@ __pycache__/
|
|
12 |
|
13 |
# VS Code
|
14 |
.vscode/
|
15 |
-
*.code-workspace
|
|
|
|
|
|
|
|
12 |
|
13 |
# VS Code
|
14 |
.vscode/
|
15 |
+
*.code-workspace.env
|
16 |
+
cookies.firefox-private.txt
|
17 |
+
.env
|
18 |
+
firefox-cookies.txt
|
app.py
CHANGED
@@ -1,20 +1,94 @@
|
|
1 |
import gradio as gr
|
2 |
from yt_dlp import YoutubeDL
|
3 |
import os
|
|
|
4 |
from pathlib import Path
|
5 |
import tempfile
|
6 |
import shutil
|
7 |
from pathlib import Path
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def get_cookies():
|
10 |
-
"""Get
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
14 |
|
15 |
-
def
|
16 |
-
"""
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def download_for_browser(url, mode='audio', quality='high'):
|
20 |
if not url:
|
@@ -22,7 +96,15 @@ def download_for_browser(url, mode='audio', quality='high'):
|
|
22 |
|
23 |
# Create temporary directory that persists until file is served
|
24 |
temp_dir = Path(tempfile.mkdtemp())
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
try:
|
28 |
# Configure download options
|
@@ -53,7 +135,7 @@ def download_for_browser(url, mode='audio', quality='high'):
|
|
53 |
})
|
54 |
|
55 |
# Add cookies
|
56 |
-
opts["cookiefile"] =
|
57 |
|
58 |
# Download file
|
59 |
with YoutubeDL(opts) as ydl:
|
@@ -77,6 +159,7 @@ def download_for_browser(url, mode='audio', quality='high'):
|
|
77 |
return None, f"Error during download: {str(e)}"
|
78 |
|
79 |
def create_browser_ui():
|
|
|
80 |
with gr.Blocks(title="YouTube Downloader", theme=gr.themes.Soft()) as demo:
|
81 |
gr.Markdown("## YouTube Downloader")
|
82 |
|
|
|
1 |
import gradio as gr
|
2 |
from yt_dlp import YoutubeDL
|
3 |
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
from pathlib import Path
|
6 |
import tempfile
|
7 |
import shutil
|
8 |
from pathlib import Path
|
9 |
|
10 |
+
def cookies_to_env(cookie_file_path: str) -> str:
|
11 |
+
"""Convert cookie file content to environment variable format"""
|
12 |
+
try:
|
13 |
+
with open(cookie_file_path, 'r') as f:
|
14 |
+
lines = f.readlines()
|
15 |
+
|
16 |
+
# Keep header comments
|
17 |
+
header = [line.strip() for line in lines if line.startswith('#')]
|
18 |
+
# Get cookie content (non-comment lines)
|
19 |
+
cookies = [line.strip() for line in lines if line.strip() and not line.startswith('#')]
|
20 |
+
|
21 |
+
# Join with escaped newlines
|
22 |
+
content = '\\n'.join(header + [''] + cookies) # Empty line after headers
|
23 |
+
|
24 |
+
# Create env file content
|
25 |
+
return f'FIREFOX_COOKIES="{content}"'
|
26 |
+
|
27 |
+
except Exception as e:
|
28 |
+
raise ValueError(f"Error converting cookie file: {str(e)}")
|
29 |
+
|
30 |
+
def env_to_cookies(env_content: str, output_file: str) -> None:
|
31 |
+
"""Convert environment variable content back to cookie file"""
|
32 |
+
try:
|
33 |
+
# Extract content from env format
|
34 |
+
if '="' not in env_content:
|
35 |
+
raise ValueError("Invalid env content format")
|
36 |
+
|
37 |
+
content = env_content.split('="', 1)[1].strip('"')
|
38 |
+
|
39 |
+
# Replace escaped newlines with actual newlines
|
40 |
+
cookie_content = content.replace('\\n', '\n')
|
41 |
+
|
42 |
+
# Write to cookie file
|
43 |
+
with open(output_file, 'w') as f:
|
44 |
+
f.write(cookie_content)
|
45 |
+
|
46 |
+
except Exception as e:
|
47 |
+
raise ValueError(f"Error converting to cookie file: {str(e)}")
|
48 |
+
|
49 |
+
def save_to_env_file(env_content: str, env_file: str = '.env') -> None:
|
50 |
+
"""Save environment variable content to .env file"""
|
51 |
+
try:
|
52 |
+
with open(env_file, 'w') as f:
|
53 |
+
f.write(env_content)
|
54 |
+
#print(f"Successfully saved to {env_file}")
|
55 |
+
except Exception as e:
|
56 |
+
raise ValueError(f"Error saving to env file: {str(e)}")
|
57 |
+
|
58 |
+
def env_to_cookies_from_env(output_file: str) -> None:
|
59 |
+
"""Convert environment variable from .env file to cookie file"""
|
60 |
+
try:
|
61 |
+
load_dotenv() # Load from .env file
|
62 |
+
env_content = os.getenv('FIREFOX_COOKIES')
|
63 |
+
print(f"Printing env content: \n{env_content}")
|
64 |
+
if not env_content:
|
65 |
+
raise ValueError("FIREFOX_COOKIES not found in .env file")
|
66 |
+
|
67 |
+
env_to_cookies(f'FIREFOX_COOKIES="{env_content}"', output_file)
|
68 |
+
except Exception as e:
|
69 |
+
raise ValueError(f"Error converting to cookie file: {str(e)}")
|
70 |
+
|
71 |
def get_cookies():
|
72 |
+
"""Get cookies from environment variable"""
|
73 |
+
load_dotenv()
|
74 |
+
cookie_content = os.getenv('FIREFOX_COOKIES')
|
75 |
+
#print(cookie_content)
|
76 |
+
if not cookie_content:
|
77 |
+
raise ValueError("FIREFOX_COOKIES environment variable not set")
|
78 |
+
return cookie_content
|
79 |
|
80 |
+
def create_temp_cookie_file():
|
81 |
+
"""Create temporary cookie file from environment variable"""
|
82 |
+
temp_cookie = tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.txt')
|
83 |
+
try:
|
84 |
+
cookie_content = get_cookies()
|
85 |
+
# Replace escaped newlines with actual newlines
|
86 |
+
cookie_content = cookie_content.replace('\\n', '\n')
|
87 |
+
temp_cookie.write()
|
88 |
+
temp_cookie.flush()
|
89 |
+
return Path(temp_cookie.name)
|
90 |
+
finally:
|
91 |
+
temp_cookie.close()
|
92 |
|
93 |
def download_for_browser(url, mode='audio', quality='high'):
|
94 |
if not url:
|
|
|
96 |
|
97 |
# Create temporary directory that persists until file is served
|
98 |
temp_dir = Path(tempfile.mkdtemp())
|
99 |
+
|
100 |
+
# Convert cookie file to env and save locally
|
101 |
+
#cookie_file = "cookies.firefox-private.txt"
|
102 |
+
#env_content = cookies_to_env(cookie_file)
|
103 |
+
#save_to_env_file(env_content)
|
104 |
+
|
105 |
+
# Convert back to cookie file using .env
|
106 |
+
cookiefile = "firefox-cookies.txt"
|
107 |
+
env_to_cookies_from_env("firefox-cookies.txt")
|
108 |
|
109 |
try:
|
110 |
# Configure download options
|
|
|
135 |
})
|
136 |
|
137 |
# Add cookies
|
138 |
+
opts["cookiefile"] = "firefox-cookies.txt" #create_temp_cookie_file()
|
139 |
|
140 |
# Download file
|
141 |
with YoutubeDL(opts) as ydl:
|
|
|
159 |
return None, f"Error during download: {str(e)}"
|
160 |
|
161 |
def create_browser_ui():
|
162 |
+
|
163 |
with gr.Blocks(title="YouTube Downloader", theme=gr.themes.Soft()) as demo:
|
164 |
gr.Markdown("## YouTube Downloader")
|
165 |
|
firefox-cookies.txt
DELETED
@@ -1,17 +0,0 @@
|
|
1 |
-
# Netscape HTTP Cookie File
|
2 |
-
# https://curl.haxx.se/rfc/cookie_spec.html
|
3 |
-
# This is a generated file! Do not edit.
|
4 |
-
|
5 |
-
addons.mozilla.org FALSE / TRUE 0 taarId 96b327d5d30cc4b8647e9640bc6609ef5354d95d4811f0684a58d7e907078809
|
6 |
-
.youtube.com TRUE / TRUE 1735380447 GPS 1
|
7 |
-
.youtube.com TRUE / TRUE 0 YSC DnLywHKC13A
|
8 |
-
.youtube.com TRUE / TRUE 1750930669 VISITOR_INFO1_LIVE oZG0RSnTnU8
|
9 |
-
.youtube.com TRUE / TRUE 1750930669 VISITOR_PRIVACY_METADATA CgJTRxIEGgAgJA%3D%3D
|
10 |
-
.youtube.com TRUE / TRUE 1769939000 PREF f4=4000000&f6=40000000&tz=Asia.Singapore&f7=100
|
11 |
-
.mozilla.org TRUE / FALSE 1769938662 _ga_B9CY1C9VBC GS1.1.1735378656.1.0.1735378662.0.0.0
|
12 |
-
.mozilla.org TRUE / FALSE 1769938656 _ga GA1.2.1293160606.1735378657
|
13 |
-
.mozilla.org TRUE / FALSE 1735465056 _gid GA1.2.67885460.1735378657
|
14 |
-
.mozilla.org TRUE / FALSE 1735378716 _gat 1
|
15 |
-
.youtube.com TRUE / TRUE 1735379283 CONSISTENCY AKreu9spj6VrirZ8v5nz5RnBrJ3BLva9iWI3jstb-iNbdBTO55DeQc-MZphbM17lI4s4BoPpzfY8E0Qi_1C6_WynAgOGh4RTXtn1NhYIBIcYQ3wGoRhi7nbKegdkAdQcBu8imYtviFomgBBYduhqQKM
|
16 |
-
.youtube.com TRUE / FALSE 1735379001 ST-go2vz4 csn=99MsyrhwMPm7iUCn&itct=CAAQg2ciEwjXwpCplcqKAxUfp9gFHYqmMdYyDHJlbGF0ZWQtYXV0b0iwtb3O88meyXOaAQUIAxD4HQ%3D%3D&lact=298817&pbis=3&prefetch_reason=hc_pbis&vis=0&autonav=1&endpoint=%7B%22clickTrackingParams%22%3A%22CAAQg2ciEwjXwpCplcqKAxUfp9gFHYqmMdYyDHJlbGF0ZWQtYXV0b0iwtb3O88meyXOaAQUIAxD4HQ%3D%3D%22%2C%22commandMetadata%22%3A%7B%22webCommandMetadata%22%3A%7B%22url%22%3A%22%2Fwatch%3Fv%3D4ybcCJwdpfI%26pp%3DQAFIAQ%253D%253D%22%2C%22webPageType%22%3A%22WEB_PAGE_TYPE_WATCH%22%2C%22rootVe%22%3A3832%7D%7D%2C%22watchEndpoint%22%3A%7B%22videoId%22%3A%224ybcCJwdpfI%22%2C%22params%22%3A%22EAEYAdoBBAgBKgA%253D%22%2C%22playerParams%22%3A%22QAFIAQ%253D%253D%22%2C%22watchEndpointSupportedPrefetchConfig%22%3A%7B%22prefetchHintConfig%22%3A%7B%22prefetchPriority%22%3A0%2C%22countdownUiRelativeSecondsPrefetchCondition%22%3A-3%7D%7D%7D%7D
|
17 |
-
.youtube.com TRUE / FALSE 1735379004 ST-1g2y8kt autonav=1&playnext=0&itct=CLUBEOGSASITCNfCkKmVyooDFR-n2AUdiqYx1jIMcmVsYXRlZC1hdXRvSLC1vc7zyZ7Jc5oBBQgDEPgd&lact=301318&vis=0&csn=99MsyrhwMPm7iUCn&endpoint=%7B%22watchEndpoint%22%3A%7B%22videoId%22%3A%224ybcCJwdpfI%22%2C%22params%22%3A%22EAEYAdoBBAgBKgA%253D%22%2C%22playerParams%22%3A%22QAFIAQ%253D%253D%22%2C%22watchEndpointSupportedPrefetchConfig%22%3A%7B%22prefetchHintConfig%22%3A%7B%22prefetchPriority%22%3A0%2C%22countdownUiRelativeSecondsPrefetchCondition%22%3A-3%7D%7D%7D%2C%22commandMetadata%22%3A%7B%22webCommandMetadata%22%3A%7B%22url%22%3A%22%2Fwatch%3Fv%3D4ybcCJwdpfI%26pp%3DQAFIAQ%253D%253D%22%2C%22webPageType%22%3A%22WEB_PAGE_TYPE_WATCH%22%2C%22rootVe%22%3A3832%7D%7D%2C%22clickTrackingParams%22%3A%22CLUBEOGSASITCNfCkKmVyooDFR-n2AUdiqYx1jIMcmVsYXRlZC1hdXRvSLC1vc7zyZ7Jc5oBBQgDEPgd%22%7D
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|