bohning / usdb_syncer

MIT License
58 stars 9 forks source link

Cookies seem to be not properly passed to yt-dlp #248

Open bohning opened 2 months ago

bohning commented 2 months ago
if path := browser.cookie_path():
        options["cookies"] = (f"{browser.value}:{path}",)

doesn't seem to do the trick, age restricted content still fails even though I'm logged into YT in a browser (test with e.g. https://usdb.animux.de/?link=detail&id=9743)

bohning commented 2 months ago

While the command line parameter seems to be --cookies, the parameter to pass cookies to the yt-dlp Python module seems to be cookiefile.

The cookiefile needs to be a plain text cookie file in "Netscape" format according to this FAQ.

I tried to convert the cookiejar from browser_cookie3 to a temporary cookie file in Netscape format with the following code snippet

from http.cookiejar import CookieJar
from pathlib import Path
import tempfile

def save_cookies_to_file(cookiejar: CookieJar) -> str:
    """
    Save cookies to a temporary file in Netscape format.

    Args:
        cookiejar: The cookie jar object.

    Returns:
        str: Path to the temporary cookie file.
    """
    temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
    with open(temp_file.name, 'w') as f:
        f.write("# Netscape HTTP Cookie File\n")
        for cookie in cookiejar:
            f.write(f"{cookie.domain}\t"
                    f"{'TRUE' if cookie.domain.startswith('.') else 'FALSE'}\t"
                    f"{cookie.path}\t"
                    f"{'TRUE' if cookie.secure else 'FALSE'}\t"
                    f"{cookie.expires if cookie.expires else '0'}\t"
                    f"{cookie.name}\t"
                    f"{cookie.value}\n")
    return temp_file.name

and the resulting file looks like specified in the FAQ above (including LF line endings on Mac). But when I pass this cookiefile to yt-dlp via

def _ytdl_options(format_: str, browser: Browser, target_stem: Path) -> YtdlOptions:
    options: YtdlOptions = {
        "format": format_,
        "outtmpl": f"{target_stem}.%(ext)s",
        "keepvideo": False,
        "verbose": False,
        # suppresses download of playlists, channels and search results
        "playlistend": 0,
        "overwrites": True,
    }
    if cookiejar := browser.cookies():
        options["cookiefile"] = save_cookies_to_file(cookiejar)
    return options

I still get ERROR: [youtube] MS91knuzoOA: Sign in to confirm your age. This video may be inappropriate for some users.

bohning commented 2 months ago

I do not understand why #9743 doesn't work, but #23589 does.