Spaces:
Running
Running
Delanoe Pirard
commited on
Commit
·
cc3d383
1
Parent(s):
e1b7852
clean 2
Browse files- agents/video_analyzer_agent.py +37 -3
- get_cookie.py +23 -0
- yt_cookie.txt +0 -16
agents/video_analyzer_agent.py
CHANGED
|
@@ -22,6 +22,37 @@ from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, No
|
|
| 22 |
logger = logging.getLogger(__name__)
|
| 23 |
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# ---------------------------------------------------------------------------
|
| 26 |
# Prompt loader
|
| 27 |
# ---------------------------------------------------------------------------
|
|
@@ -106,17 +137,20 @@ def download_video_and_analyze(video_url: str) -> str:
|
|
| 106 |
llm_model_name = os.getenv("VIDEO_ANALYZER_LLM_MODEL", "models/gemini-1.5-pro")
|
| 107 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
| 108 |
|
| 109 |
-
cookie_txt = "yt_cookie.txt"
|
| 110 |
-
|
| 111 |
ydl_opts = {
|
| 112 |
'format': 'best',
|
| 113 |
'outtmpl': os.path.join("downloaded_videos", 'temp_video.%(ext)s'),
|
| 114 |
'quiet': True,
|
| 115 |
'extract_flat': True,
|
| 116 |
'ignoreerrors': True,
|
| 117 |
-
"cookiefile": cookie_txt
|
| 118 |
}
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl_download:
|
| 121 |
ydl_download.download(video_url)
|
| 122 |
|
|
|
|
| 22 |
logger = logging.getLogger(__name__)
|
| 23 |
|
| 24 |
|
| 25 |
+
def env_to_cookies(env_content: str, output_file: str) -> None:
|
| 26 |
+
"""Convert environment variable content back to cookie file"""
|
| 27 |
+
try:
|
| 28 |
+
# Extract content from env format
|
| 29 |
+
if '="' not in env_content:
|
| 30 |
+
raise ValueError("Invalid env content format")
|
| 31 |
+
|
| 32 |
+
content = env_content.split('="', 1)[1].strip('"')
|
| 33 |
+
|
| 34 |
+
# Replace escaped newlines with actual newlines
|
| 35 |
+
cookie_content = content.replace('\\n', '\n')
|
| 36 |
+
|
| 37 |
+
# Write to cookie file
|
| 38 |
+
with open(output_file, 'w') as f:
|
| 39 |
+
f.write(cookie_content)
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
raise ValueError(f"Error converting to cookie file: {str(e)}")
|
| 43 |
+
|
| 44 |
+
def env_to_cookies_from_env(output_file: str) -> None:
|
| 45 |
+
"""Convert environment variable from .env file to cookie file"""
|
| 46 |
+
try:
|
| 47 |
+
env_content = os.getenv('YT_COOKIE', "")
|
| 48 |
+
# print(f"Printing env content: \n{env_content}")
|
| 49 |
+
if not env_content:
|
| 50 |
+
raise ValueError("YT_COOKIE not found in .env file")
|
| 51 |
+
|
| 52 |
+
env_to_cookies(f'YT_COOKIE="{env_content}"', output_file)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
raise ValueError(f"Error converting to cookie file: {str(e)}")
|
| 55 |
+
|
| 56 |
# ---------------------------------------------------------------------------
|
| 57 |
# Prompt loader
|
| 58 |
# ---------------------------------------------------------------------------
|
|
|
|
| 137 |
llm_model_name = os.getenv("VIDEO_ANALYZER_LLM_MODEL", "models/gemini-1.5-pro")
|
| 138 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
| 139 |
|
|
|
|
|
|
|
| 140 |
ydl_opts = {
|
| 141 |
'format': 'best',
|
| 142 |
'outtmpl': os.path.join("downloaded_videos", 'temp_video.%(ext)s'),
|
| 143 |
'quiet': True,
|
| 144 |
'extract_flat': True,
|
| 145 |
'ignoreerrors': True,
|
|
|
|
| 146 |
}
|
| 147 |
|
| 148 |
+
cookiefile = "cookies.txt"
|
| 149 |
+
env_to_cookies_from_env(cookiefile)
|
| 150 |
+
|
| 151 |
+
# Add cookies
|
| 152 |
+
ydl_opts["cookiefile"] = cookiefile # create_temp_cookie_file()
|
| 153 |
+
|
| 154 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl_download:
|
| 155 |
ydl_download.download(video_url)
|
| 156 |
|
get_cookie.py
CHANGED
|
@@ -34,3 +34,26 @@ def export_youtube_cookies_netscape(domain: str = "youtube.com") -> str:
|
|
| 34 |
|
| 35 |
tmp.flush()
|
| 36 |
return tmp.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
tmp.flush()
|
| 36 |
return tmp.name
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def cookies_to_env(cookie_file_path: str) -> str:
|
| 40 |
+
"""Convert cookie file content to environment variable format"""
|
| 41 |
+
try:
|
| 42 |
+
with open(cookie_file_path, 'r') as f:
|
| 43 |
+
lines = f.readlines()
|
| 44 |
+
|
| 45 |
+
# Keep header comments
|
| 46 |
+
header = [line.strip() for line in lines if line.startswith('#')]
|
| 47 |
+
# Get cookie content (non-comment lines)
|
| 48 |
+
cookies = [line.strip() for line in lines if line.strip() and not line.startswith('#')]
|
| 49 |
+
|
| 50 |
+
# Join with escaped newlines
|
| 51 |
+
content = '\\n'.join(header + [''] + cookies) # Empty line after headers
|
| 52 |
+
|
| 53 |
+
# Create env file content
|
| 54 |
+
return f'FIREFOX_COOKIES="{content}"'
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
raise ValueError(f"Error converting cookie file: {str(e)}")
|
| 58 |
+
|
| 59 |
+
print(cookies_to_env(export_youtube_cookies_netscape("youtube.com")))
|
yt_cookie.txt
DELETED
|
@@ -1,16 +0,0 @@
|
|
| 1 |
-
# Netscape HTTP Cookie File
|
| 2 |
-
# This file is generated by yt-dlp. Do not edit.
|
| 3 |
-
|
| 4 |
-
.youtube.com TRUE / TRUE 1761617545 __Secure-ROLLOUT_TOKEN CK2CtoGE9_qCKBDVmd6PqaqLAxjs7cKEmYGNAw%3D%3D
|
| 5 |
-
.youtube.com TRUE / TRUE 1761622693 VISITOR_INFO1_LIVE XJWWeg-61Jo
|
| 6 |
-
.youtube.com TRUE / TRUE 1780502573 __Secure-3PAPISID Es8lNi2tKH_w6xhk/A5yqk-H0g0K329X1o
|
| 7 |
-
.youtube.com TRUE / TRUE 1780502573 __Secure-3PSID g.a000wQiyYVADhJ0IDxSDoFRi6cUtMUs77vKiv67cKlhfBRyt8TWh6wP59ja7Ccu2lEPStG0E9gACgYKAT0SARASFQHGX2MirUvQtL-jYTLLnMocXq-eVxoVAUF8yKq2SkbS4FUiTUqswnomlXXS0076
|
| 8 |
-
.youtube.com TRUE / TRUE 1761622693 VISITOR_PRIVACY_METADATA CgJCRRIhEh0SGwsMDg8QERITFBUWFxgZGhscHR4fICEiIyQlJiBt
|
| 9 |
-
.youtube.com TRUE / FALSE 0 PREF f4=4000000&tz=UTC&f7=100&f6=40000400&f5=30000&hl=en
|
| 10 |
-
.youtube.com TRUE / TRUE 1777602578 __Secure-1PSIDTS sidts-CjIBjplskKsM_03lNIkS3RrNBUeXvyspivYPoOliq4TT_dcTTR60_KSnwv-tWi7MPFIArRAA
|
| 11 |
-
.youtube.com TRUE / TRUE 1777602578 __Secure-3PSIDTS sidts-CjIBjplskKsM_03lNIkS3RrNBUeXvyspivYPoOliq4TT_dcTTR60_KSnwv-tWi7MPFIArRAA
|
| 12 |
-
.youtube.com TRUE / TRUE 1777602578 __Secure-3PSIDCC AKEyXzWAno5sygvBmiCVOYca7OcomSey2MkLYLei77boI7N8znStQNIMPKLzIesnRhDOs_tFdC4T
|
| 13 |
-
.youtube.com TRUE / TRUE 0 SOCS CAI
|
| 14 |
-
.youtube.com TRUE / TRUE 1746072493 GPS 1
|
| 15 |
-
.youtube.com TRUE / TRUE 1809142693 __Secure-YT_TVFAS t=483201&s=2
|
| 16 |
-
.youtube.com TRUE / TRUE 1761622693 DEVICE_INFO ChxOelE1T1RNeE5qVXlOREF5TVRVeE1ERTJOdz09EKXZy8AGGKXZy8AG
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|