De-async initialize and URLs cleanup
This commit is contained in:
1 parent
6561f5bcd9
commit
9ad0979d0e
2 files changed
+14
-10
No files matched your search
@@ -22,7 +22,6 @@ MAX_WEBSOCKETS = 8
|
|||||||
WS_TOPICS_LIMIT = 50
|
WS_TOPICS_LIMIT = 50
|
||||||
# URLs
|
# URLs
|
||||||
BASE_URL = "https://twitch.tv"
|
BASE_URL = "https://twitch.tv"
|
||||||
AUTH_URL = "https://passport.twitch.tv"
|
|
||||||
WEBSOCKET_URL = "wss://pubsub-edge.twitch.tv/v1"
|
WEBSOCKET_URL = "wss://pubsub-edge.twitch.tv/v1"
|
||||||
GQL_URL = "https://gql.twitch.tv/gql"
|
GQL_URL = "https://gql.twitch.tv/gql"
|
||||||
# Misc for Twitch
|
# Misc for Twitch
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ from utils import task_wrapper, timestamp, AwaitableValue, OrderedSet
|
|||||||
from exceptions import RequestException, LoginException, CaptchaRequired
|
from exceptions import RequestException, LoginException, CaptchaRequired
|
||||||
from constants import (
|
from constants import (
|
||||||
GQL_URL,
|
GQL_URL,
|
||||||
AUTH_URL,
|
BASE_URL,
|
||||||
CLIENT_ID,
|
CLIENT_ID,
|
||||||
USER_AGENT,
|
USER_AGENT,
|
||||||
COOKIES_PATH,
|
COOKIES_PATH,
|
||||||
@@ -85,7 +85,7 @@ class Twitch:
|
|||||||
# Maintenance task
|
# Maintenance task
|
||||||
self._mnt_task: asyncio.Task[None] | None = None
|
self._mnt_task: asyncio.Task[None] | None = None
|
||||||
|
|
||||||
async def initialize(self) -> None:
|
def initialize(self) -> None:
|
||||||
cookie_jar = aiohttp.CookieJar()
|
cookie_jar = aiohttp.CookieJar()
|
||||||
if os.path.isfile(COOKIES_PATH):
|
if os.path.isfile(COOKIES_PATH):
|
||||||
cookie_jar.load(COOKIES_PATH)
|
cookie_jar.load(COOKIES_PATH)
|
||||||
@@ -682,7 +682,9 @@ class Twitch:
|
|||||||
payload.pop("authy_token", None)
|
payload.pop("authy_token", None)
|
||||||
payload.pop("twitchguard_code", None)
|
payload.pop("twitchguard_code", None)
|
||||||
for attempt in range(2):
|
for attempt in range(2):
|
||||||
async with self.request("POST", f"{AUTH_URL}/login", json=payload) as response:
|
async with self.request(
|
||||||
|
"POST", "https://passport.twitch.tv/login", json=payload
|
||||||
|
) as response:
|
||||||
login_response: JsonType = await response.json()
|
login_response: JsonType = await response.json()
|
||||||
|
|
||||||
# Feed this back in to avoid running into CAPTCHA if possible
|
# Feed this back in to avoid running into CAPTCHA if possible
|
||||||
@@ -751,12 +753,15 @@ class Twitch:
|
|||||||
login_form: LoginForm = self.gui.login
|
login_form: LoginForm = self.gui.login
|
||||||
logger.debug("Checking login")
|
logger.debug("Checking login")
|
||||||
login_form.update("Logging in...", None)
|
login_form.update("Logging in...", None)
|
||||||
|
# NOTE: We need this here because of the jar being accessed
|
||||||
if self._session is None:
|
if self._session is None:
|
||||||
await self.initialize()
|
self.initialize()
|
||||||
assert self._session is not None
|
assert self._session is not None
|
||||||
|
url = URL(BASE_URL)
|
||||||
|
assert url.host is not None
|
||||||
jar = cast(aiohttp.CookieJar, self._session.cookie_jar)
|
jar = cast(aiohttp.CookieJar, self._session.cookie_jar)
|
||||||
for attempt in range(2):
|
for attempt in range(2):
|
||||||
cookie = jar.filter_cookies("https://twitch.tv") # type: ignore
|
cookie = jar.filter_cookies(url)
|
||||||
if not cookie:
|
if not cookie:
|
||||||
# no cookie - login
|
# no cookie - login
|
||||||
await self._login()
|
await self._login()
|
||||||
@@ -776,7 +781,7 @@ class Twitch:
|
|||||||
if status == 401:
|
if status == 401:
|
||||||
# the access token we have is invalid - clear the cookie and reauth
|
# the access token we have is invalid - clear the cookie and reauth
|
||||||
logger.debug("Restored session is invalid")
|
logger.debug("Restored session is invalid")
|
||||||
jar.clear_domain("twitch.tv")
|
jar.clear_domain(url.host)
|
||||||
continue
|
continue
|
||||||
elif status == 200:
|
elif status == 200:
|
||||||
validate_response = await response.json()
|
validate_response = await response.json()
|
||||||
@@ -789,16 +794,16 @@ class Twitch:
|
|||||||
logger.debug(f"Login successful, user ID: {self._user_id}")
|
logger.debug(f"Login successful, user ID: {self._user_id}")
|
||||||
login_form.update("Logged in", self._user_id)
|
login_form.update("Logged in", self._user_id)
|
||||||
# update our cookie and save it
|
# update our cookie and save it
|
||||||
jar.update_cookies(cookie, URL("https://twitch.tv"))
|
jar.update_cookies(cookie, url)
|
||||||
jar.save(COOKIES_PATH)
|
jar.save(COOKIES_PATH)
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def request(
|
async def request(
|
||||||
self, method: str, url: str, *, attempts: int = 5, **kwargs
|
self, method: str, url: str, *, attempts: int = 5, **kwargs
|
||||||
) -> abc.AsyncIterator[aiohttp.ClientResponse]:
|
) -> abc.AsyncIterator[aiohttp.ClientResponse]:
|
||||||
if self._session is None:
|
|
||||||
await self.initialize()
|
|
||||||
session = self._session
|
session = self._session
|
||||||
|
if session is None:
|
||||||
|
self.initialize()
|
||||||
assert session is not None
|
assert session is not None
|
||||||
method = method.upper()
|
method = method.upper()
|
||||||
cause: Exception | None = None
|
cause: Exception | None = None
|
||||||
|
|||||||
Reference in new issue
Block a user