Redo stream init

This commit is contained in:
DevilXD committed 2022-02-11 18:27:08 +01:00
1 parent 4d2a884fbe
commit 904ccecc2a
1 file changed
+39 -24
+39 -24
View File
@@ -6,8 +6,7 @@ import asyncio
import logging import logging
from base64 import b64encode from base64 import b64encode
from functools import cached_property from functools import cached_property
from datetime import datetime, timezone from typing import Any, Optional, List, SupportsInt, TYPE_CHECKING
from typing import Any, Optional, SupportsInt, TYPE_CHECKING
from utils import Game, invalidate_cache from utils import Game, invalidate_cache
from exceptions import MinerException, RequestException from exceptions import MinerException, RequestException
@@ -22,30 +21,46 @@ logger = logging.getLogger("TwitchDrops")
class Stream: class Stream:
def __init__(self, channel: Channel, data: JsonType): def __init__(
self._twitch: Twitch = channel._twitch self,
channel: Channel,
*,
id: SupportsInt,
game: Optional[JsonType],
viewers: int,
title: str,
tags: List[Any],
):
self.channel: Channel = channel self.channel: Channel = channel
stream = data["stream"] self.broadcast_id = int(id)
self.broadcast_id = int(stream["id"]) self.viewers: int = viewers
self.viewers: int = stream["viewersCount"] self.drops_enabled: bool = any(t["id"] == DROPS_ENABLED_TAG for t in tags)
self.drops_enabled: bool = any(tag["id"] == DROPS_ENABLED_TAG for tag in stream["tags"]) self.game: Optional[Game] = Game(game) if game else None
settings = data["broadcastSettings"] self.title: str = title
self.game: Optional[Game] = Game(settings["game"]) if settings["game"] else None
self.title: str = settings["title"]
self._timestamp = datetime.now(timezone.utc)
@classmethod @classmethod
def from_directory(cls, channel: Channel, data: JsonType): def from_get_stream(cls, channel: Channel, data: JsonType) -> Stream:
self = super().__new__(cls) stream = data["stream"]
self._twitch = channel._twitch settings = data["broadcastSettings"]
self.channel = channel return cls(
self.broadcast_id = int(data["id"]) channel,
self.viewers = data["viewersCount"] id=stream["id"],
self.drops_enabled = any(tag["id"] == DROPS_ENABLED_TAG for tag in data["tags"]) game=settings["game"],
self.game = Game(data["game"]) # has to be there since we searched with it viewers=stream["viewersCount"],
self.title = data["title"] title=settings["title"],
self._timestamp = datetime.now(timezone.utc) tags=stream["tags"],
return self )
@classmethod
def from_directory(cls, channel: Channel, data: JsonType) -> Stream:
return cls(
channel,
id=data["id"],
game=data["game"], # has to be there since we searched with it
viewers=data["viewersCount"],
title=data["title"],
tags=data["tags"],
)
class Channel: class Channel:
@@ -228,7 +243,7 @@ class Channel:
self._display_name = stream_data["displayName"] self._display_name = stream_data["displayName"]
if not stream_data["stream"]: if not stream_data["stream"]:
return None return None
return Stream(self, stream_data) return Stream.from_get_stream(self, stream_data)
async def check_online(self) -> bool: async def check_online(self) -> bool:
stream = await self.get_stream() stream = await self.get_stream()