From 28c0f8b75e4074807919822d5ecfde08b52cbce3 Mon Sep 17 00:00:00 2001 From: DevilXD Date: Thu, 22 Dec 2022 13:14:44 +0100 Subject: [PATCH] Streamline user agent and client ID access --- constants.py | 32 ++++++++++++++++++++++++++------ twitch.py | 14 +++++++------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/constants.py b/constants.py index ef87043..485b360 100644 --- a/constants.py +++ b/constants.py @@ -6,6 +6,7 @@ from pathlib import Path from copy import deepcopy from enum import Enum, auto from datetime import timedelta +from collections import namedtuple from typing import Any, Dict, Literal, NewType, TYPE_CHECKING from yarl import URL @@ -69,12 +70,6 @@ MAX_CHANNELS = MAX_TOPICS // TOPICS_PER_CHANNEL # Misc DEFAULT_LANG = "English" BASE_URL = URL("https://twitch.tv") -CLIENT_ID = "kd1unb4b3q4t58fwlpcbzcbnm76a8fp" -# CLIENT_ID = "uo6dggojyb8d6soh92zknwmi5ej1q2" -ANDROID_USER_AGENT = ( - "Dalvik/2.1.0 (Linux; U; Android 7.1.2; SM-G977N Build/LMY48Z) " - "tv.twitch.android.app/14.3.2/1403020" -) # Intervals and Delays PING_INTERVAL = timedelta(minutes=3) PING_TIMEOUT = timedelta(seconds=10) @@ -90,6 +85,31 @@ FILE_FORMATTER = logging.Formatter( datefmt="%Y-%m-%d %H:%M:%S", ) OUTPUT_FORMATTER = logging.Formatter("{levelname}: {message}", style='{', datefmt="%H:%M:%S") +ClientInfo = namedtuple("ClientInfo", ["CLIENT_ID", "USER_AGENT"]) + + +class ClientType: + WEB = ClientInfo( + "kimne78kx3ncx6brgo4mv6wki5h1ko", + ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" + ), + ) + ANDROID = ClientInfo( + "kd1unb4b3q4t58fwlpcbzcbnm76a8fp", + ( + "Dalvik/2.1.0 (Linux; U; Android 7.1.2; SM-G977N Build/LMY48Z) " + "tv.twitch.android.app/14.3.2/1403020" + ), + ) + SMARTBOX = ClientInfo( + "ue6666qo983tsx6so1t0vnawi233wa", + ( + "Mozilla/5.0 (Linux; Android 7.1; Smart Box C1) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" + ), + ) class State(Enum): diff --git a/twitch.py b/twitch.py index b4d9cea..5d45088 100644 --- a/twitch.py +++ b/twitch.py @@ -56,14 +56,13 @@ from utils import ( from constants import ( CALL, BASE_URL, - CLIENT_ID, COOKIES_PATH, GQL_OPERATIONS, MAX_CHANNELS, WATCH_INTERVAL, DROPS_ENABLED_TAG, - ANDROID_USER_AGENT, State, + ClientType, WebsocketTopic, ) @@ -87,6 +86,7 @@ class SkipExtraJsonDecoder(json.JSONDecoder): return obj +CLIENT_ID, USER_AGENT = ClientType.ANDROID SAFE_LOADS = lambda s: json.loads(s, cls=SkipExtraJsonDecoder) @@ -276,6 +276,9 @@ class _AuthState: token_kind: str = '' use_chrome: bool = False payload: JsonType = { + # username and password are added later + # "username": str, + # "password": str, "client_id": CLIENT_ID, # client ID to-be associated with the access token "undelete_user": False, # purpose unknown "remember_me": True, # persist the session via the cookie @@ -310,7 +313,7 @@ class _AuthState: "Client-Id": CLIENT_ID, "Content-Type": "application/json; charset=UTF-8", "Host": "passport.twitch.tv", - "User-Agent": ANDROID_USER_AGENT, + "User-Agent": USER_AGENT, "X-Device-Id": self.device_id, # "X-Device-Id": ''.join(random.choices('0123456789abcdef', k=32)), } @@ -569,10 +572,7 @@ class Twitch: chrome_agent = random.choice(agents[7:11]) except Exception: # looks like we can't rely on 3rd parties too much - chrome_agent = ( - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " - "(KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36" - ) + chrome_agent = ClientType.WEB.USER_AGENT # load in cookies cookie_jar = aiohttp.CookieJar() if COOKIES_PATH.exists():