Kaydet (Commit) 871d8966 authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

singleton channels

üst 3cd0460c
......@@ -25,3 +25,4 @@ repos:
rev: master
hooks:
- id: mypy
stages: [manual]
......@@ -7,7 +7,7 @@ from typing import Optional, Sequence
from chatat.twitch import Auth, Channel, Message
class TwitchChatProtocol(asyncio.Protocol):
class TwitchProtocol(asyncio.Protocol):
def __init__(
self, auth: Auth, channels: Sequence[Channel], on_con_lost: asyncio.Future
) -> None:
......@@ -27,6 +27,12 @@ class TwitchChatProtocol(asyncio.Protocol):
handler.setFormatter(formatter)
self.logger.addHandler(handler)
class TwitchHelixProtocol(TwitchProtocol):
pass
class TwitchChatProtocol(TwitchProtocol):
def _send(self, cmd: str, value: str) -> None:
request = f"{cmd.upper()} {value}\r\n"
self.transport.write(request.encode("utf-8"))
......
......@@ -5,7 +5,7 @@ import os
import re
from dataclasses import dataclass
from enum import Enum, auto
from typing import Optional, Union
from typing import Any, Dict, Optional, Tuple, Union
PATTERN = re.compile(r"^:(.*?)!(.*?@.*?\.tmi\.twitch\.tv) (.*?) #(.*?) :(.*?)$")
......@@ -17,18 +17,30 @@ class Actions(Enum):
@dataclass
class Auth:
username: str
password: str
oauthtok: str
client_id: str
client_secret: str
@classmethod
def from_json(cls, file: os.PathLike):
with open(os.fspath(file)) as f:
content = json.loads(f.read())
return cls(**content)
return cls(**content["chat"], **content["helix"]) # type: ignore
class SingleableChannel:
def __init_subclass__(cls: SingleableChannel) -> None:
cls._singletons: Dict[Tuple[Any, ...], SingleableChannel] = {}
def __new__(cls: SingleableChannel, *args) -> SingleableChannel:
if not cls._singletons.get(args):
cls._singletons[args] = super().__new__(cls)
return cls._singletons[args]
@dataclass
class Channel:
class Channel(SingleableChannel):
name: str
def __str__(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment