Cache watch payload

This commit is contained in:
DevilXD committed 2022-01-22 19:42:01 +01:00
1 parent 75179dc69f
commit db5698843c
2 files changed
+14 -11

No files matched your search

+13 -10
View File
@@ -5,11 +5,12 @@ import json
import asyncio import asyncio
import logging import logging
from base64 import b64encode from base64 import b64encode
from functools import cached_property
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import Any, Optional, SupportsInt, TYPE_CHECKING from typing import Any, Optional, SupportsInt, TYPE_CHECKING
from utils import Game
from exceptions import MinerException from exceptions import MinerException
from utils import Game, invalidate_cache
from constants import JsonType, BASE_URL, GQL_OPERATIONS, ONLINE_DELAY, DROPS_ENABLED_TAG from constants import JsonType, BASE_URL, GQL_OPERATIONS, ONLINE_DELAY, DROPS_ENABLED_TAG
if TYPE_CHECKING: if TYPE_CHECKING:
@@ -93,7 +94,9 @@ class Channel:
self._stream = None self._stream = None
self._pending_stream_up = None self._pending_stream_up = None
self.priority = priority self.priority = priority
await self.get_stream() stream = await self.get_stream()
if stream is not None:
self._stream = stream
return self return self
def __repr__(self) -> str: def __repr__(self) -> str:
@@ -204,15 +207,15 @@ class Channel:
self.id = int(stream_data["id"]) self.id = int(stream_data["id"])
self.name = stream_data["displayName"] self.name = stream_data["displayName"]
if stream_data["stream"]: if stream_data["stream"]:
self._stream = Stream(self, stream_data) return Stream(self, stream_data)
else: return None
self._stream = None
return self._stream
async def check_online(self) -> bool: async def check_online(self) -> bool:
stream = await self.get_stream() stream = await self.get_stream()
if stream is None: if stream is None:
invalidate_cache(self, "_payload")
return False return False
self._stream = stream
return True return True
async def _online_delay(self): async def _online_delay(self):
@@ -250,6 +253,7 @@ class Channel:
self.display() self.display()
if self.online: if self.online:
self._stream = None self._stream = None
invalidate_cache(self, "_payload")
self.display() self.display()
self._twitch.on_offline(self) self._twitch.on_offline(self)
@@ -273,7 +277,8 @@ class Channel:
# so if we're not calling it, we need to do it ourselves # so if we're not calling it, we need to do it ourselves
self.display() self.display()
def _encode_payload(self): @cached_property
def _payload(self):
assert self._stream is not None assert self._stream is not None
payload = [ payload = [
{ {
@@ -299,7 +304,5 @@ class Channel:
if self._spade_url is None: if self._spade_url is None:
self._spade_url = await self.get_spade_url() self._spade_url = await self.get_spade_url()
logger.debug(f"Sending minute-watched to {self.name}") logger.debug(f"Sending minute-watched to {self.name}")
async with self._twitch._session.post( async with self._twitch._session.post(self._spade_url, data=self._payload) as response:
self._spade_url, data=self._encode_payload()
) as response:
return response.status == 204 return response.status == 204
+1 -1
View File
@@ -249,7 +249,7 @@ class Twitch:
else: else:
no_acl = True no_acl = True
# set them online if possible # 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 no_acl:
# if there's at least one game without an ACL, # if there's at least one game without an ACL,
# get a list of all live channels with drops enabled # get a list of all live channels with drops enabled