From a91644d6de482e08913f3dc7b631b95460739b5e Mon Sep 17 00:00:00 2001 From: DevilXD Date: Wed, 12 Jan 2022 12:11:04 +0100 Subject: [PATCH] Latest user agent, cleanup comments and docstrings; ONLINE_DELAY: 60s -> 120s --- channel.py | 36 +++++++++++++++++++++++++----------- constants.py | 4 ++-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/channel.py b/channel.py index 927cc98..4189e98 100644 --- a/channel.py +++ b/channel.py @@ -4,7 +4,6 @@ import re import json import asyncio import logging -from time import time from base64 import b64encode from datetime import datetime, timezone from typing import Any, Optional, TYPE_CHECKING @@ -97,7 +96,9 @@ class Channel: @property def iid(self) -> str: - # this is responsible for the ID/Key of the columns inside channel list + """ + Returns a string to be used as ID/key of the columns inside channel list. + """ return str(self.id) @property @@ -193,6 +194,10 @@ class Channel: return True async def _online_delay(self): + """ + The 'stream-up' event is sent before the stream actually goes online, + so just wait a bit and check if it's actually online by then. + """ self.display() await asyncio.sleep(ONLINE_DELAY.total_seconds()) await self.check_online() @@ -200,24 +205,34 @@ class Channel: self.display() def set_online(self): + """ + Sets the channel status to OFFLINE. + + This is called externally, if we receive an event about this happening. + """ if self.online or self.pending_online: # we're already online, or about to be return - # stream-up is sent before the stream actually goes online, so just wait a bit - # and check if it's actually online by then self._pending_stream_up = asyncio.create_task(self._online_delay()) # display is called from within the task def set_offline(self): - # to be called externally, if we receive an event about this happening + """ + Sets the channel status to OFFLINE. + + This is called externally, if we receive an event about this happening. + """ if self._pending_stream_up is not None: self._pending_stream_up.cancel() self._pending_stream_up = None - self._stream = None - self.display() + if self.online: + self._stream = None + self.display() async def claim_bonus(self): - # this also fills out the 'points' attribute + """ + This claims bonus points if they're available, and fills out the 'points' attribute. + """ response = await self._twitch.gql_request( GQL_OPERATIONS["ChannelPointsContext"].with_variables({"channelLogin": self.name}) ) @@ -230,8 +245,8 @@ class Channel: await self._twitch.claim_points(channel_data["id"], claim_available["id"]) logger.info("Claimed bonus points") else: - # calling claim_points is going to refresh the display, so if we're not calling it, - # we need to do it ourselves + # calling 'claim_points' is going to refresh the display via the websocket payload, + # so if we're not calling it, we need to do it ourselves self.display() def _encode_payload(self): @@ -260,7 +275,6 @@ class Channel: if self._spade_url is None: self._spade_url = await self.get_spade_url() logger.debug(f"Sending minute-watched to {self.name}") - self._twitch._last_watch = time() async with self._twitch._session.post( self._spade_url, data=self._encode_payload() ) as response: diff --git a/constants.py b/constants.py index 45ce13c..f5e94ac 100644 --- a/constants.py +++ b/constants.py @@ -21,7 +21,7 @@ GQL_URL = "https://gql.twitch.tv/gql" CLIENT_ID = "kimne78kx3ncx6brgo4mv6wki5h1ko" USER_AGENT = ( "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " - "Chrome/96.0.4664.45 Safari/537.36" + "Chrome/97.0.4692.71 Safari/537.36" ) # Paths LOG_PATH = "log.txt" @@ -30,7 +30,7 @@ SETTINGS_PATH = "settings.json" # Intervals and Delays PING_INTERVAL = timedelta(minutes=3) PING_TIMEOUT = timedelta(seconds=10) -ONLINE_DELAY = timedelta(seconds=60) +ONLINE_DELAY = timedelta(seconds=120) WATCH_INTERVAL = timedelta(seconds=58.8) # Tags DROPS_ENABLED_TAG = "c2542d6d-cd10-4532-919b-3d19f30a768b"