/
twitch_userscript.py
138 lines (98 loc) · 4.3 KB
/
twitch_userscript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from asyncio import FIRST_COMPLETED, CancelledError, create_task, sleep, wait
from collections import defaultdict
from typing import Callable
import orjson
from fastapi import APIRouter, WebSocket
from clip_state import ClipState
from config import FRIENDS, MAX_USERSCRIPT_SLOTS, TTV_CHANNEL
from events import (TYPE_CLIP_STATE, TYPE_USER_SCORE, TYPE_USER_VOTE_STATE,
Subscription)
from user_vote_state import UserVoteState
from utils import normalize_username
from ws_route import WSLoopTaskRoute
router = APIRouter(prefix='/userscript')
class Counter:
_counter: int = 0
def inc(self) -> bool:
if self._counter < MAX_USERSCRIPT_SLOTS:
self._counter += 1
return True
return False
def dec(self) -> None:
self._counter -= 1
user_slots: dict[str, Counter] = defaultdict(Counter)
addr_slots: dict[str, Counter] = defaultdict(Counter)
@router.websocket('/ws')
class WS(WSLoopTaskRoute):
username: str = ''
channel: str = ''
func_on_disconnect: list[Callable]
def __init__(self, ws: WebSocket):
super().__init__(ws)
self.func_on_disconnect = []
@property
def identifier(self) -> str:
return f'{super().identifier} @ {self.channel} : {self.username}'
async def on_connect(self) -> None:
addr = self.ws.client.host
if not addr_slots[addr].inc():
raise Exception(f'Too many connections (host={addr})')
self.func_on_disconnect.append(lambda: addr_slots[addr].dec())
async def on_receive(self, data: dict) -> None:
assert self.task is None, 'Unexpected receive'
assert 'username' in data, 'Missing username'
# TODO: in later release
# assert 'channel' in data, 'Missing channel'
username = normalize_username(data['username'])
# Twitch usernames must be between 4 and 25 characters
assert 4 <= len(username) <= 25, f'Invalid username: {username}'
channel = data.get('channel', TTV_CHANNEL)
# Channel must be valid
assert any(f.channel == channel for f in FRIENDS), f'Invalid channel: {channel}'
self.username = username
self.channel = channel
if not user_slots[self.username].inc():
raise Exception(f'Too many connections (user={self.username})')
self.func_on_disconnect.append(lambda: user_slots[self.username].dec())
await self.start()
async def on_disconnect(self) -> None:
await super().on_disconnect()
for func in self.func_on_disconnect:
func()
async def loop(self) -> None:
await self.ws.send_text('ok')
with \
Subscription(TYPE_CLIP_STATE(self.channel)) as s_clip, \
Subscription(TYPE_USER_VOTE_STATE(self.channel, self.username)) as s_vote, \
Subscription(TYPE_USER_SCORE(self.channel, self.username)) as s_score:
clip: ClipState | None = None
vote: UserVoteState | None = None
score: int | None = None
s_clip_wait = create_task(s_clip.wait())
s_vote_wait = create_task(s_vote.wait())
s_score_wait = create_task(s_score.wait())
while self.connected:
try:
done, _ = await wait((s_clip_wait, s_vote_wait, s_score_wait), return_when=FIRST_COMPLETED)
except CancelledError as e:
s_clip_wait.cancel()
s_vote_wait.cancel()
s_score_wait.cancel()
raise e
if s_clip_wait in done:
clip = s_clip_wait.result()
s_clip_wait = create_task(s_clip.wait())
if s_vote_wait in done:
vote = s_vote_wait.result()
s_vote_wait = create_task(s_vote.wait())
if s_score_wait in done:
score = s_score_wait.result()
s_score_wait = create_task(s_score.wait())
if clip is not None:
json = orjson.dumps({
'clip': vars(clip) if clip else None,
'vote': vars(vote) if vote else None,
'score': score
})
await self.ws.send_bytes(json)
await sleep(0.2)