automatically obtain po token

This commit is contained in:
insert 2025-01-19 19:18:41 -05:00
parent 4fd92f7834
commit db27864f03
Signed by: insert
GPG key ID: A70775C389ACF105
2 changed files with 32 additions and 3 deletions

View file

@ -1,4 +1,10 @@
import obsws_python import obsws_python
from selenium.webdriver.firefox.options import Options
from seleniumwire import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from netscape_cookies import to_netscape_string
import json
import yt_dlp import yt_dlp
from time import sleep from time import sleep
import time import time
@ -37,6 +43,26 @@ full_dl_dir = Path(download_dir.name)
vid_details = {"title": "", "channel": ""} vid_details = {"title": "", "channel": ""}
con = sqlite3.connect(os.getenv("QUEUE_PATH","queue.db"), check_same_thread=False) con = sqlite3.connect(os.getenv("QUEUE_PATH","queue.db"), check_same_thread=False)
cur = con.cursor() cur = con.cursor()
options = Options()
options.add_argument("--headless")
options.profile = webdriver.FirefoxProfile(os.getenv("PROFILE_PATH"))
driver = webdriver.Firefox(options=options)
print("Getting potoken from firefox...")
driver.get("https://www.youtube.com/embed/aqz-KE-bpKQ")
elem = WebDriverWait(driver, 8, 0.2, None).until(lambda x: x.find_element(By.CLASS_NAME, "html5-video-player"))
sleep(1)
elem.click()
sleep(2)
for r in driver.requests:
if "https://www.youtube.com/youtubei/v1/player" in r.url:
potoken = json.loads(r.body)['serviceIntegrityDimensions']["poToken"]
break
cookiesstr = to_netscape_string(driver.get_cookies())
with open("cookies.txt", 'w') as f:
f.write(f'# Netscape HTTP Cookie File\n# https://curl.haxx.se/rfc/cookie_spec.html\n# This is a generated file! Do not edit.\n\n{cookiesstr}')
driver.close()
print(f"Success! Potoken is {potoken}")
def countuser(usrid): def countuser(usrid):
res = cur.execute(f"SELECT ROWID FROM queue WHERE " + ("hasplayed = false AND " if not (os.getenv("PERMANENT_MAX_QUEUE","FALSE") == "TRUE") else "") + f"user = {usrid}") res = cur.execute(f"SELECT ROWID FROM queue WHERE " + ("hasplayed = false AND " if not (os.getenv("PERMANENT_MAX_QUEUE","FALSE") == "TRUE") else "") + f"user = {usrid}")
@ -81,9 +107,9 @@ ydl_opts = {
'match_filter': video_check, 'match_filter': video_check,
'hls_prefer_native': True, 'hls_prefer_native': True,
'extract_flat': 'discard_in_playlist', 'extract_flat': 'discard_in_playlist',
'cookiefile': f'{os.getenv("COOKIES_FILE")}', 'cookiefile': f'cookies.txt',
'extractor_args': {'youtube': {'player_client': ['web', 'default'], 'extractor_args': {'youtube': {'player_client': ['web', 'default'],
'po_token': [f'web+{os.getenv("PO_TOKEN")}']}}, 'po_token': [f'web+{potoken}']}},
'format': 'bestvideo[height<=1080][vcodec!*=av01][ext=mp4]+bestaudio[abr<=256][ext=m4a]/best[ext=mp4]/best', 'format': 'bestvideo[height<=1080][vcodec!*=av01][ext=mp4]+bestaudio[abr<=256][ext=m4a]/best[ext=mp4]/best',
'fragment_retries': 10, 'fragment_retries': 10,
'noplaylist': True, 'noplaylist': True,

View file

@ -1,2 +1,5 @@
disnake disnake
obsws_python obsws_python
netscape-cookies
blinker==1.7.0
selenium-wire