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

singleton channels

üst 3cd0460c
...@@ -25,3 +25,4 @@ repos: ...@@ -25,3 +25,4 @@ repos:
rev: master rev: master
hooks: hooks:
- id: mypy - id: mypy
stages: [manual]
...@@ -7,7 +7,7 @@ from typing import Optional, Sequence ...@@ -7,7 +7,7 @@ from typing import Optional, Sequence
from chatat.twitch import Auth, Channel, Message from chatat.twitch import Auth, Channel, Message
class TwitchChatProtocol(asyncio.Protocol): class TwitchProtocol(asyncio.Protocol):
def __init__( def __init__(
self, auth: Auth, channels: Sequence[Channel], on_con_lost: asyncio.Future self, auth: Auth, channels: Sequence[Channel], on_con_lost: asyncio.Future
) -> None: ) -> None:
...@@ -27,6 +27,12 @@ class TwitchChatProtocol(asyncio.Protocol): ...@@ -27,6 +27,12 @@ class TwitchChatProtocol(asyncio.Protocol):
handler.setFormatter(formatter) handler.setFormatter(formatter)
self.logger.addHandler(handler) self.logger.addHandler(handler)
class TwitchHelixProtocol(TwitchProtocol):
pass
class TwitchChatProtocol(TwitchProtocol):
def _send(self, cmd: str, value: str) -> None: def _send(self, cmd: str, value: str) -> None:
request = f"{cmd.upper()} {value}\r\n" request = f"{cmd.upper()} {value}\r\n"
self.transport.write(request.encode("utf-8")) self.transport.write(request.encode("utf-8"))
......
...@@ -5,7 +5,7 @@ import os ...@@ -5,7 +5,7 @@ import os
import re import re
from dataclasses import dataclass from dataclasses import dataclass
from enum import Enum, auto 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) (.*?) #(.*?) :(.*?)$") PATTERN = re.compile(r"^:(.*?)!(.*?@.*?\.tmi\.twitch\.tv) (.*?) #(.*?) :(.*?)$")
...@@ -17,18 +17,30 @@ class Actions(Enum): ...@@ -17,18 +17,30 @@ class Actions(Enum):
@dataclass @dataclass
class Auth: class Auth:
username: str username: str
password: str
oauthtok: str oauthtok: str
client_id: str
client_secret: str
@classmethod @classmethod
def from_json(cls, file: os.PathLike): def from_json(cls, file: os.PathLike):
with open(os.fspath(file)) as f: with open(os.fspath(file)) as f:
content = json.loads(f.read()) 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 @dataclass
class Channel: class Channel(SingleableChannel):
name: str name: str
def __str__(self): 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