Skip to content

feat: use a separate cookies file only for age restricted media - #1624

Open
eunchanp wants to merge 4 commits into
meeb:test-contributionsfrom
eunchanp:feat/age-restricted-cookies
Open

eunchanp wants to merge 4 commits into
meeb:test-contributionsfrom
eunchanp:feat/age-restricted-cookies

Conversation

@eunchanp

@eunchanp eunchanp commented Oct 27, 2026 •

Copy link
Copy Markdown

Summary

Closes #1623.

Adds a separate cookies file that is only used after YouTube reports an age restriction. Regular requests keep using cookies.txt (or no cookies), so an account is only used when it is needed.

  • New setting AGE_RESTRICTED_COOKIES_FILE, defaulting to CONFIG_BASE_DIR / 'cookies-age-restricted.txt' (/config/tubesync/cookies-age-restricted.txt in the container).
  • When get_media_info() or download_media() fails with an age restriction message ("confirm your age", "age-restricted", "inappropriate for some users") and that file exists, the file is copied into a private temporary directory and the request is retried once with cookiefile pointing at the copy.
  • yt-dlp writes refreshed cookies back to the file it is given, so the configured file is never handed to it directly. The copy is removed after the retry.
  • Errors that are not age related keep the existing handling, including FormatUnavailableError and the members-only retry.

Usage

Drop the account's Netscape-format cookies into /config/tubesync/cookies-age-restricted.txt. On a retry the log shows:

[youtube-dl] retrying with age restricted cookies from: /config/tubesync/cookies-age-restricted.txt

The Using cookies wiki page does not mention this file yet. I'm happy to draft that addition if it helps.

Tests

New sync/tests/test_youtube.py (10 tests) mocks yt_dlp.YoutubeDL and covers, for both get_media_info() and download_media():

  • the retry uses a temporary copy, the original file is left unchanged, and the copy is removed
  • no retry when the file is not configured or the error is not age related
  • a failed retry raises YouTubeError
  • the regular cookies.txt is used first and replaced by the copy only for the retry

The full Django suite passes locally (77 tests, Python 3.14 on macOS), and ruff check is clean.

Unrelated finding

While writing the tests I noticed that get_media_info() raises TypeError when YOUTUBE_DL_CACHEDIR is not set, which is the default outside the container. YOUTUBE_DEFAULTS['cachedir'] is False, so Path(user_set('cachedir', opts, '/dev/shm')) becomes Path(False). The new tests set a cache directory the way the container settings do. I left the fix out of this PR to keep it focused.

When yt-dlp reports an age restriction, copy AGE_RESTRICTED_COOKIES_FILE
to a temporary file and retry once with it. An account is then only used
when needed, and the configured file is never written to by yt-dlp.

Closes meeb#1623
@tcely

tcely commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

Thanks! Please open an issue for the Path(False) bug so it gets fixed too.

If you commit your wiki changes under docs then I will move those into docs/wiki for you.

@tcely tcely left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this looks good.

Comment thread tubesync/sync/youtube.py
Comment thread tubesync/sync/youtube.py Outdated
Comment thread tubesync/tubesync/local_settings.py.container Outdated
@github-project-automation github-project-automation Bot moved this to In Progress in Status Oct 27, 2026
@tcely tcely added documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed labels Oct 27, 2026
Comment thread tubesync/sync/youtube.py
@tcely
tcely changed the base branch from main to test-contributions October 28, 2026 09:21
Comment thread tubesync/sync/youtube.py Outdated
Comment thread tubesync/sync/youtube.py Outdated
@github-project-automation github-project-automation Bot moved this from In Progress to Ready in Status Oct 28, 2026
@tcely

tcely commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

@eunchanp Did you want to make any further changes or update the wiki??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

Use a cookies file only for age restrictions

2 participants