7x11x13 / youtube-up

Upload videos to YouTube using the internal YouTube API. Does not require an API key.
https://7x11x13.xyz/youtube-up
14 stars 5 forks source link

invalid Netscape format cookies file %r: %r" % http.cookiejar.LoadError: invalid Netscape format cookies file #6

Closed bugbounted closed 3 months ago

bugbounted commented 3 months ago

Hi 7x11x13, for automate get cookie file from my account to don't say me cookie expired in your program , I'm trying to code auto cookie fetcher using playwright , but getting error below all the time on youtube-up :

C:\Users\Bugbounted\Desktop\New folder\New folder (3)>python test.py
C:\Users\Bugbounted\AppData\Local\Programs\Python\Python311\Lib\http\cookiejar.py:2083: UserWarning: http.cookiejar bug!
Traceback (most recent call last):
  File "C:\Users\Bugbounted\AppData\Local\Programs\Python\Python311\Lib\http\cookiejar.py", line 2056, in _really_load
    assert domain_specified == initial_dot
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

  _warn_unhandled_exception()
Traceback (most recent call last):
  File "C:\Users\Bugbounted\AppData\Local\Programs\Python\Python311\Lib\http\cookiejar.py", line 2056, in _really_load
    assert domain_specified == initial_dot
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Bugbounted\Desktop\New folder\New folder (3)\test.py", line 3, in <module>
    uploader = YTUploaderSession.from_cookies_txt("cookies/cookies.txt")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bugbounted\AppData\Local\Programs\Python\Python311\Lib\site-packages\youtube_up\uploader.py", line 125, in from_cookies_txt
    return cls(cj, webdriver_path, selenium_timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bugbounted\AppData\Local\Programs\Python\Python311\Lib\site-packages\youtube_up\uploader.py", line 97, in __init__
    self._cookies.load(ignore_discard=True, ignore_expires=True)
  File "C:\Users\Bugbounted\AppData\Local\Programs\Python\Python311\Lib\http\cookiejar.py", line 1802, in load
    self._really_load(f, filename, ignore_discard, ignore_expires)
  File "C:\Users\Bugbounted\AppData\Local\Programs\Python\Python311\Lib\http\cookiejar.py", line 2084, in _really_load
    raise LoadError("invalid Netscape format cookies file %r: %r" %
http.cookiejar.LoadError: invalid Netscape format cookies file 'cookies/cookies.txt': '.youtube.com\tFALSE\t/\tTRUE\t1752940624\tPREF\tf6=40000000&tz=Europe.London'

my playwright botright cookie exporter is :

import asyncio
import botright

async def main():
    botright_client = await botright.Botright()
    browser = await botright_client.new_browser()
    page = await browser.new_page()

    # Navigate to YouTube
    await page.goto("https://www.youtube.com")

    # Click on the sign-in button
    sign_in_button = await page.query_selector('.signin .yt-spec-touch-feedback-shape__fill')
    if sign_in_button:
        await sign_in_button.click()
    else:
        print("Sign-in button not found!")
        await browser.close()
        await botright_client.close()
        return

    # Wait for the email input field on the redirected page to load
    await page.wait_for_selector('#identifierId', timeout=10000)

    # Click on the email input field
    await page.click('#identifierId')

    # Fill the email input field with "test@gmail.com"
    await page.fill('#identifierId', "")

    # Click the "Next" button
    await page.click('text=Next')

    # Wait for the password input field to appear
    await page.wait_for_selector('input[type="password"]', timeout=10000)

    # Fill the password input field with "your_pass"
    await page.fill('[name="Passwd"]', "")

    # Click the "Next" button
    await page.click('text=Next')

    # Wait for 10 seconds
    await page.wait_for_timeout(25000)

    # Get all cookies
    cookies = await page.context.cookies()

    # Filter cookies for domain '.youtube.com'
    youtube_cookies = [cookie for cookie in cookies if '.youtube.com' in cookie['domain']]

    # Write cookies to a Netscape HTTP Cookie File
    with open("cookies.txt", "w") as f:
        f.write("# Netscape HTTP Cookie File\n")
        f.write("# http://curl.haxx.se/rfc/cookie_spec.html\n")
        f.write("# This is a generated file!  Do not edit.\n\n")
        for cookie in youtube_cookies:
            f.write(f"{cookie['domain']}\t")
            f.write(f"{'TRUE' if cookie['httpOnly'] else 'FALSE'}\t")
            f.write(f"{cookie['path']}\t")
            f.write(f"{'TRUE' if cookie['secure'] else 'FALSE'}\t")
            # Convert the expiration time to an integer if available, otherwise set it to 0
            expires = int(cookie['expires']) if 'expires' in cookie and cookie['expires'] else 0
            f.write(f"{expires}\t")
            f.write(f"{cookie['name']}\t")
            f.write(f"{cookie['value']}\n")

    print("Cookies saved successfully!")

    # Close the browser and botright client
    await browser.close()
    await botright_client.close()

if __name__ == "__main__":
    asyncio.run(main())

please if it's possible make new branch for your code to give only gmail & password & auto export cookie values all the time before every upload waiting for your reponse regards

7x11x13 commented 3 months ago

It looks like the second value you write for each line is cookie['httpOnly'], but I think it should be something else. I think it should be TRUE if the domain starts with a . and FALSE otherwise

7x11x13 commented 3 months ago

Closing this because it's not an issue with youtube-up