Skip to content

Commit e26eacd

Browse files
committed
v0.1711 - cookie handling functionality & config options
1 parent 08a0ca7 commit e26eacd

File tree

4 files changed

+210
-17
lines changed

4 files changed

+210
-17
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ After launching your bot successfully, you can interact with it via Telegram (se
196196
- `/language` - set the model's transcription language (`auto` = autodetect); if you know the language spoken in the audio, setting the transcription language manually with this command may improve both transcription speed and accuracy.
197197

198198
## Changes
199+
- v0.1711 - **🍪 Cookie handling is here!**
200+
- see `config.ini` for `yt-dlp` options to set up your cookies
201+
- this will make it easier to enable seamless operation with no interruptions with some services
199202
- v0.1710 - rewrite for chunking logic when sending as messages & re-encoding tool
200203
- better step-by-step logging, better error catching, better fitting into TG message limits with fallbacks
201204
- again; please refer to i.e. [Issue #7](https://github.com/FlyingFathead/whisper-transcriber-telegram-bot/issues/7) (and open up a new issue if necessary) if the problem persists

config/config.ini

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,34 @@ cooldown_seconds = 10
6868
max_requests_per_minute = 5
6969

7070
[YTDLPSettings]
71-
# use your own `cookies.txt` (true/false)
71+
# use your own `cookies.txt` file (true/false)
7272
# this is sometimes required for sites that require login
7373
# or, in some cases, with sites like YouTube that don't like downloaders.
74-
use_cookies = False
74+
# (will be skipped if the file isn't found)
75+
use_cookies_file = false
7576
cookies_file = config/cookies.txt
76-
77+
# use cookies from your browser (to bypass yt-dlp woes)
78+
use_browser_cookies = true
79+
# your browser type
80+
browser_type = firefox
81+
# your browser cookies profile location, leave blank to use default location
82+
# (can be set either as a /path/to/profile.default or env var to read the location from)
83+
browser_cookies_profile = $TRANSCRIBERBOT_BROWSER_COOKIE_LOCATION
84+
# options for cache control
85+
# Use no cache dir (true/false); might work better in some use scenarios
86+
no_cache_dir = True
87+
# cache dir location, leave empty for default
88+
custom_cache_dir =
7789
# some media sites don't always work well with yt-dlp's audio download feature
7890
# for compatibility, it's recommended to enable the flag below (true)
7991
download_original_video_for_domains_active = true
80-
8192
# list your sites below to download original videos from, comma separated.
8293
# example:
8394
# download_original_video_domains = site1.com, site2.com, site3.com
8495
# these are the sites we use to download original videos from
8596
# (i.e. rumble.com has been a site that's been widely reported as having broken downloads;
8697
# hence the video-only download method for that site and others alike.)
8798
download_original_video_domains = rumble.com
88-
8999
# use worst video quality (true/false)
90100
# this is usually recommended, because we will only need the _audio_ for transcription.
91101
# adding a high-quality video will cause massive file size increases.

src/main.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# openai-whisper transcriber-bot for Telegram
44

55
# version of this program
6-
version_number = "0.1710"
6+
version_number = "0.1711"
77

88
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99
# https://github.com/FlyingFathead/whisper-transcriber-telegram-bot/
@@ -30,7 +30,7 @@
3030
# Adjust import paths based on new structure
3131
from transcription_handler import process_url_message, set_user_model, get_whisper_model, transcribe_audio, get_best_gpu, get_audio_duration, estimate_transcription_time, format_duration, get_whisper_language, set_user_language
3232
from utils.bot_token import get_bot_token
33-
from utils.utils import print_startup_message, safe_split_message
33+
from utils.utils import print_startup_message, safe_split_message, hz_line
3434
from config_loader import ConfigLoader # Import ConfigLoader
3535

3636
# Configure basic logging
@@ -53,6 +53,35 @@
5353
# Initialize the lock outside of your function to ensure it's shared across all invocations.
5454
queue_lock = asyncio.Lock()
5555

56+
# log cookies
57+
def log_cookies_config():
58+
"""Log the status of the cookie-related settings at startup."""
59+
config = ConfigLoader.get_config()
60+
61+
use_cookies_file = config.getboolean('YTDLPSettings', 'use_cookies_file', fallback=False)
62+
cookies_file = config.get('YTDLPSettings', 'cookies_file', fallback='config/cookies.txt')
63+
64+
use_browser_cookies = config.getboolean('YTDLPSettings', 'use_browser_cookies', fallback=False)
65+
browser_type = config.get('YTDLPSettings', 'browser_type', fallback='firefox')
66+
raw_browser_cookies_profile = config.get('YTDLPSettings', 'browser_cookies_profile', fallback='')
67+
68+
# Expand environment variable if needed
69+
expanded_profile = raw_browser_cookies_profile
70+
if use_browser_cookies and raw_browser_cookies_profile.startswith('$'):
71+
env_var_name = raw_browser_cookies_profile[1:]
72+
env_val = os.getenv(env_var_name, '')
73+
if env_val:
74+
expanded_profile = env_val
75+
76+
# hz_line()
77+
logger.info("--- yt-dlp cookie settings at startup ---")
78+
logger.info(f"use_cookies_file = {use_cookies_file}")
79+
logger.info(f"cookies_file = {cookies_file}")
80+
logger.info(f"use_browser_cookies = {use_browser_cookies}")
81+
logger.info(f"browser_type = {browser_type}")
82+
logger.info(f"browser_cookies_profile = {raw_browser_cookies_profile} [expanded -> {expanded_profile}]")
83+
hz_line()
84+
5685
class AllowedFileFilter(filters.MessageFilter):
5786
def __init__(self, allowed_formats):
5887
super().__init__()
@@ -835,6 +864,9 @@ async def ping_owners_on_startup(app: Application):
835864
if __name__ == '__main__':
836865
print_startup_message(version_number) # Print startup message
837866

867+
# Log ytdlp cookie info right away
868+
log_cookies_config()
869+
838870
# Read the update settings from config
839871
config = ConfigLoader.get_config()
840872
check_for_updates = config.getboolean('UpdateSettings', 'CheckForYTDLPUpdates', fallback=False)

src/transcription_handler.py

Lines changed: 158 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,65 @@ def split_message(message, max_length=3500):
178178
async def download_audio(url, audio_path):
179179
config = ConfigLoader.get_config()
180180
ytdlp_settings = ConfigLoader.get_ytdlp_domain_settings()
181-
use_cookies = config.getboolean('YTDLPSettings', 'use_cookies', fallback=False)
181+
182+
# --- Added verbose logging for cookie usage ---
183+
use_cookies_file = config.getboolean('YTDLPSettings', 'use_cookies_file', fallback=False)
182184
cookies_file = config.get('YTDLPSettings', 'cookies_file', fallback='config/cookies.txt')
185+
if use_cookies_file:
186+
logger.info("Cookie usage is enabled (use_cookies_file=True).")
187+
logger.info(f"Expected cookies file path: {cookies_file}")
188+
if os.path.exists(cookies_file):
189+
logger.info("Cookie file found and will be used for yt-dlp.")
190+
else:
191+
logger.warning(
192+
"Cookie file usage is enabled, but the specified cookies file "
193+
f"does not exist at: {cookies_file}. Please check config.ini."
194+
)
195+
else:
196+
logger.info("Cookie file usage is disabled from config (use_cookies_file=False).")
197+
198+
# read config options
199+
use_cookies_file = config.getboolean('YTDLPSettings', 'use_cookies_file', fallback=False)
200+
cookies_file = config.get('YTDLPSettings', 'cookies_file', fallback='config/cookies.txt')
201+
202+
use_browser_cookies = config.getboolean('YTDLPSettings', 'use_browser_cookies', fallback=False)
203+
browser_type = config.get('YTDLPSettings', 'browser_type', fallback='firefox')
204+
browser_cookies_profile = config.get('YTDLPSettings', 'browser_cookies_profile', fallback='')
205+
206+
no_cache_dir = config.getboolean('YTDLPSettings', 'no_cache_dir', fallback=False)
207+
custom_cache_dir = config.get('YTDLPSettings', 'custom_cache_dir', fallback='')
183208
use_worst_video_quality = config.getboolean('YTDLPSettings', 'use_worst_video_quality', fallback=True)
184-
209+
210+
# Log what was found
211+
logger.info(f"use_cookies_file={use_cookies_file}, cookies_file={cookies_file}")
212+
logger.info(f"use_browser_cookies={use_browser_cookies}, browser_type={browser_type}, browser_cookies_profile={browser_cookies_profile}")
213+
logger.info(f"no_cache_dir={no_cache_dir}, custom_cache_dir={custom_cache_dir}, use_worst_video_quality={use_worst_video_quality}")
214+
215+
# -- Mutually exclusive check (optional) --
216+
if use_cookies_file and use_browser_cookies:
217+
# Decide which to prefer, or raise an error
218+
logger.warning("Both 'use_cookies_file' and 'use_browser_cookies' are true! Defaulting to 'use_browser_cookies' and ignoring the cookies file.")
219+
# Or: raise Exception("Cannot use both cookie-file and browser-cookies at once.")
220+
221+
# Expand environment variable if browser_cookies_profile starts with '$'
222+
actual_browser_profile = browser_cookies_profile
223+
if use_browser_cookies and browser_cookies_profile.startswith('$'):
224+
env_var_name = browser_cookies_profile[1:]
225+
env_val = os.getenv(env_var_name, '')
226+
if env_val:
227+
logger.info(f"Resolved browser profile from env var {env_var_name}: {env_val}")
228+
actual_browser_profile = env_val
229+
else:
230+
logger.warning(f"Environment variable {env_var_name} not set or empty; browser cookies may fail.")
231+
# Optionally raise or fallback
232+
233+
# Check for existence of cookies_file if we’re using a file
234+
if use_cookies_file:
235+
if os.path.exists(cookies_file):
236+
logger.info(f"Cookies file found: {cookies_file}")
237+
else:
238+
logger.warning(f"Cookies file not found: {cookies_file}")
239+
185240
parsed_url = urlparse(url)
186241
domain = parsed_url.netloc.lower()
187242
if domain.startswith('www.'):
@@ -198,8 +253,26 @@ async def download_audio(url, audio_path):
198253
"--dump-json",
199254
url
200255
]
201-
if use_cookies and os.path.exists(cookies_file):
202-
command.extend(["--cookies", cookies_file])
256+
257+
# Apply cache settings based on config
258+
if no_cache_dir:
259+
logger.info("Disabling yt-dlp cache via config.ini (no_cache_dir=true).")
260+
command.insert(1, "--no-cache-dir") # Insert after 'yt-dlp'
261+
elif custom_cache_dir:
262+
logger.info(f"Using custom yt-dlp cache directory: {custom_cache_dir}")
263+
command.insert(1, f"--cache-dir={custom_cache_dir}")
264+
265+
# if use_cookies_file and os.path.exists(cookies_file):
266+
# command.extend(["--cookies", cookies_file])
267+
268+
# Add cookies (only if we do NOT also prefer the browser cookie approach)
269+
if use_browser_cookies and not use_cookies_file:
270+
# Add --cookies-from-browser
271+
command.extend(["--cookies-from-browser", f"{browser_type}:{actual_browser_profile}"])
272+
elif use_cookies_file:
273+
# Add --cookies <file>
274+
if os.path.exists(cookies_file):
275+
command.extend(["--cookies", cookies_file])
203276

204277
process = await asyncio.create_subprocess_exec(
205278
*command,
@@ -257,6 +330,7 @@ def get_format_sort_key(fmt):
257330
if not selected_format_id:
258331
raise Exception("Could not determine selected format ID.")
259332

333+
logger.info(f"yt-dlp command: {command}")
260334
logger.info(f"Selected format ID: {selected_format_id}")
261335

262336
# Step 3: Download video using the selected format
@@ -270,9 +344,26 @@ def get_format_sort_key(fmt):
270344
"--output", video_output_template,
271345
url
272346
]
273-
if use_cookies and os.path.exists(cookies_file):
274-
command.extend(["--cookies", cookies_file])
275347

348+
# if use_cookies_file and os.path.exists(cookies_file):
349+
# command.extend(["--cookies", cookies_file])
350+
351+
# apply the cache logic
352+
if no_cache_dir:
353+
logger.info("Disabling yt-dlp cache via config.ini (no_cache_dir=true).")
354+
command.insert(1, "--no-cache-dir")
355+
elif custom_cache_dir:
356+
logger.info(f"Using custom yt-dlp cache directory: {custom_cache_dir}")
357+
command.insert(1, f"--cache-dir={custom_cache_dir}")
358+
359+
# cookies again
360+
if use_browser_cookies and not use_cookies_file:
361+
command.extend(["--cookies-from-browser", f"{browser_type}:{actual_browser_profile}"])
362+
elif use_cookies_file:
363+
if os.path.exists(cookies_file):
364+
command.extend(["--cookies", cookies_file])
365+
366+
logger.info(f"Final yt-dlp command: {command}")
276367
logger.info("Downloading the selected quality video with audio...")
277368
else:
278369
# Download audio-only as mp3
@@ -283,8 +374,25 @@ def get_format_sort_key(fmt):
283374
"--output", audio_path,
284375
url
285376
]
286-
if use_cookies and os.path.exists(cookies_file):
377+
378+
# if use_cookies_file and os.path.exists(cookies_file):
379+
# command.extend(["--cookies", cookies_file])
380+
381+
# apply the cache logic
382+
if no_cache_dir:
383+
logger.info("Disabling yt-dlp cache via config.ini (no_cache_dir=true).")
384+
command.insert(1, "--no-cache-dir")
385+
elif custom_cache_dir:
386+
logger.info(f"Using custom yt-dlp cache directory: {custom_cache_dir}")
387+
command.insert(1, f"--cache-dir={custom_cache_dir}")
388+
389+
# cookies
390+
if use_browser_cookies and not use_cookies_file:
391+
command.extend(["--cookies-from-browser", f"{browser_type}:{actual_browser_profile}"])
392+
elif use_cookies_file and os.path.exists(cookies_file):
287393
command.extend(["--cookies", cookies_file])
394+
395+
logger.info(f"Final yt-dlp command: {command}")
288396
logger.info("Downloading audio-only...")
289397

290398
# Start the subprocess
@@ -821,9 +929,49 @@ def format_duration(duration):
821929

822930
# Fetch details for videos
823931
async def fetch_video_details(url, max_retries=3, base_delay=5, command_timeout=30):
824-
command = ["yt-dlp", "--user-agent",
825-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
826-
"--dump-json", url]
932+
# command = ["yt-dlp", "--user-agent",
933+
# "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
934+
# "--dump-json", url]
935+
936+
config = ConfigLoader.get_config()
937+
938+
# read your YTDLPSettings
939+
use_cookies_file = config.getboolean('YTDLPSettings', 'use_cookies_file', fallback=False)
940+
cookies_file = config.get('YTDLPSettings', 'cookies_file', fallback='config/cookies.txt')
941+
use_browser_cookies = config.getboolean('YTDLPSettings', 'use_browser_cookies', fallback=False)
942+
browser_type = config.get('YTDLPSettings', 'browser_type', fallback='firefox')
943+
browser_cookies_profile = config.get('YTDLPSettings', 'browser_cookies_profile', fallback='')
944+
945+
# expand env var if needed
946+
actual_browser_profile = browser_cookies_profile
947+
if use_browser_cookies and browser_cookies_profile.startswith('$'):
948+
env_var_name = browser_cookies_profile[1:]
949+
env_val = os.getenv(env_var_name, '')
950+
if env_val:
951+
actual_browser_profile = env_val
952+
953+
command = [
954+
"yt-dlp",
955+
"--user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36...",
956+
"--dump-json",
957+
url
958+
]
959+
960+
# if you prefer browser cookies over file
961+
if use_browser_cookies and not use_cookies_file:
962+
command.extend(["--cookies-from-browser", f"{browser_type}:{actual_browser_profile}"])
963+
elif use_cookies_file and os.path.exists(cookies_file):
964+
command.extend(["--cookies", cookies_file])
965+
966+
# optional: handle no_cache_dir / custom_cache_dir too if you want
967+
no_cache_dir = config.getboolean('YTDLPSettings', 'no_cache_dir', fallback=False)
968+
custom_cache_dir = config.get('YTDLPSettings', 'custom_cache_dir', fallback='')
969+
if no_cache_dir:
970+
command.insert(1, "--no-cache-dir")
971+
elif custom_cache_dir:
972+
command.insert(1, f"--cache-dir={custom_cache_dir}")
973+
974+
logger.info(f"fetch_video_details command: {command}")
827975

828976
last_stderr_output = ""
829977

0 commit comments

Comments
 (0)