From db5698843c458cf29885f3c49b0f2a46e4e7d634 Mon Sep 17 00:00:00 2001 From: DevilXD Date: Sat, 22 Jan 2022 19:42:01 +0100 Subject: [PATCH] Cache watch payload --- channel.py | 23 +++++++++++++---------- twitch.py | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/channel.py b/channel.py index dadb0bc..cafb318 100644 --- a/channel.py +++ b/channel.py @@ -5,11 +5,12 @@ import json import asyncio import logging from base64 import b64encode +from functools import cached_property from datetime import datetime, timezone from typing import Any, Optional, SupportsInt, TYPE_CHECKING -from utils import Game from exceptions import MinerException +from utils import Game, invalidate_cache from constants import JsonType, BASE_URL, GQL_OPERATIONS, ONLINE_DELAY, DROPS_ENABLED_TAG if TYPE_CHECKING: @@ -93,7 +94,9 @@ class Channel: self._stream = None self._pending_stream_up = None self.priority = priority - await self.get_stream() + stream = await self.get_stream() + if stream is not None: + self._stream = stream return self def __repr__(self) -> str: @@ -204,15 +207,15 @@ class Channel: self.id = int(stream_data["id"]) self.name = stream_data["displayName"] if stream_data["stream"]: - self._stream = Stream(self, stream_data) - else: - self._stream = None - return self._stream + return Stream(self, stream_data) + return None async def check_online(self) -> bool: stream = await self.get_stream() if stream is None: + invalidate_cache(self, "_payload") return False + self._stream = stream return True async def _online_delay(self): @@ -250,6 +253,7 @@ class Channel: self.display() if self.online: self._stream = None + invalidate_cache(self, "_payload") self.display() self._twitch.on_offline(self) @@ -273,7 +277,8 @@ class Channel: # so if we're not calling it, we need to do it ourselves self.display() - def _encode_payload(self): + @cached_property + def _payload(self): assert self._stream is not None payload = [ { @@ -299,7 +304,5 @@ 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}") - async with self._twitch._session.post( - self._spade_url, data=self._encode_payload() - ) as response: + async with self._twitch._session.post(self._spade_url, data=self._payload) as response: return response.status == 204 diff --git a/twitch.py b/twitch.py index a64e84f..e78080e 100644 --- a/twitch.py +++ b/twitch.py @@ -249,7 +249,7 @@ class Twitch: else: no_acl = True # set them online if possible - await asyncio.gather(*(channel.get_stream() for channel in new_channels)) + await asyncio.gather(*(channel.check_online() for channel in new_channels)) if no_acl: # if there's at least one game without an ACL, # get a list of all live channels with drops enabled