-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tracker.py
717 lines (608 loc) · 25.8 KB
/
test_tracker.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
"""Twitch donothon clock based on reading chat"""
import asyncio
import json
import logging
from contextlib import asynccontextmanager
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Literal, Optional
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
import toml
import twitchAPI.oauth
from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect
from fastapi.encoders import jsonable_encoder
from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse
from twitchAPI.chat import Chat, ChatCommand, ChatMessage, ChatSub, EventData
from twitchAPI.chat.middleware import BaseCommandMiddleware
from twitchAPI.eventsub.websocket import EventSubWebsocket
from twitchAPI.helper import first
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.twitch import Twitch
from twitchAPI.type import AuthScope, ChatEvent, TwitchAPIException
from websockets import ConnectionClosedOK
from twitch_dono_clock.config import SETTINGS
from twitch_dono_clock.donos import (
BITS,
CSV_COLUMNS,
CSV_TYPES,
TIPS,
Donos,
add_tip_command,
)
from twitch_dono_clock.end import End, EndException
from twitch_dono_clock.pause import (
Pause,
PauseException,
add_time_command,
pause_command,
remove_time_command,
resume_command,
)
from twitch_dono_clock.spins import Spins, spin_done_command
# "chat:read chat:edit"
USER_SCOPE = [AuthScope.CHAT_READ, AuthScope.CHAT_EDIT]
log = logging.getLogger("test_tracker")
def config_logging(level: str = "INFO"):
stream_handler = logging.StreamHandler()
file_handler = logging.FileHandler(datetime.now().strftime("%Y.%m.%d-%H.%M.%S.log"))
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s\t%(levelname)s\t%(name)s\t%(message)s",
handlers=[stream_handler, file_handler],
)
stream_handler.setLevel(level=level)
chat_logger = logging.getLogger("twitchAPI.chat")
def oauth_filter(record):
if record.msg.startswith('> "PASS oauth:'):
return False
return True
chat_logger.addFilter(oauth_filter)
config_logging()
class OnlyMyChat(BaseCommandMiddleware):
async def can_execute(self, command: ChatCommand) -> bool:
"""Only allow execution of command is from this chat"""
if "source-room-id" in command._parsed["tags"]:
if command._parsed["tags"]["source-room-id"] != command._parsed["tags"]["room-id"]:
return False
return True
async def was_executed(self, command: ChatCommand):
pass # Nothing to track
# this will be called when the event READY is triggered, which will be on bot start
async def on_ready(_ready_event: EventData):
log.info(f"Bot is ready for work, should have already joined channel {SETTINGS.twitch.channel}")
# this will be called whenever a message in a channel was send by either the bot OR another user
async def on_message(msg: ChatMessage):
if "source-room-id" in msg._parsed["tags"]:
if msg._parsed["tags"]["source-room-id"] != msg._parsed["tags"]["room-id"]:
log.debug(f"Skipping shared-chat message from {msg._parsed['tags']['source-room-id']} saying {msg.text}")
return
log.debug(f"{msg.user.name=} {msg._parsed=}")
if msg.bits:
log.info(f"in {msg.room.name}, {msg.user.name} sent bits: {msg.bits}")
Donos().add_event(
ts=msg.sent_timestamp,
user=msg.user.display_name,
target=None,
type=BITS,
amount=int(msg.bits),
)
fancy_msg = msg._parsed["tags"].get("msg-id")
if fancy_msg and msg.user.name.lower() != msg.room.name.lower():
if fancy_msg == "gigantified-emote-message":
log.info(f"{msg.sent_timestamp} {msg.user.display_name} sent a Giant Emote {msg.text}")
Donos().add_event(
ts=msg.sent_timestamp,
user=msg.user.display_name,
target="giant-emote",
type=BITS,
amount=SETTINGS.bits.giant_emote_bits,
)
elif fancy_msg == "animated-message":
animation_type = msg._parsed["tags"].get("animation-id")
log.info(f"{msg.sent_timestamp} {msg.user.display_name} sent {animation_type} Animated Message: {msg.text}")
Donos().add_event(
ts=msg.sent_timestamp,
user=msg.user.display_name,
target="animated-msg",
type=BITS,
amount=SETTINGS.bits.animated_message_bits,
)
else:
log.info(f"{msg.sent_timestamp} {msg.user.display_name} sent an unknown {fancy_msg=} - {msg.text}")
for user, regex, dono_type, target in SETTINGS.compiled_re:
if msg.user.name.lower() == user.lower():
match = regex.match(msg.text)
if match:
via = f" via {target}" if target else ""
log.info(f"in {msg.room.name}, {match['user']} sent {dono_type}{via}: {match['amount']}")
if dono_type == BITS:
amount = int(match["amount"].replace(",", ""))
elif dono_type == TIPS:
amount = float(match["amount"].replace(",", ""))
else:
raise ValueError(f"Unknown target from msg parsing {dono_type}")
if not amount: # If we have a record with 0 bits / 0 tip we shouldn't record
log.debug(f"Skipping recording {amount} {dono_type}")
continue
Donos().add_event(
ts=msg.sent_timestamp,
user=match["user"],
target=target,
type=dono_type,
amount=amount,
)
# this will be called whenever someone subscribes to a channel
async def on_sub(sub: ChatSub):
log_msg = (
f"New subscription in {sub.room.name}:"
f"\tType: {sub.sub_plan}"
f'\tFrom: {sub._parsed["tags"]["display-name"]}'
f'\tTo: {sub._parsed["tags"].get("msg-param-recipient-user-name", sub._parsed["tags"]["display-name"])}'
)
if SETTINGS.subs.count_multimonth:
months = int(sub._parsed["tags"].get("msg-param-multimonth-duration", 0))
if not months and SETTINGS.subs.count_multimonth_gift:
months = int(sub._parsed["tags"].get("msg-param-gift-months", 0))
if not months:
months = 1
log_msg += f"\t Months: {months}"
else:
months = 1
log.info(log_msg)
log.debug(f"{sub._parsed=}")
Donos().add_event(
ts=sub._parsed["tags"]["tmi-sent-ts"],
user=sub._parsed["tags"]["display-name"],
target=sub._parsed["tags"].get("msg-param-recipient-display-name"),
type=Donos.sub_from_twitch_plan(sub.sub_plan),
amount=months,
)
async def on_raid(raid: dict):
raider = raid.get("tags", {}).get("msg-param-displayName", "Unknown")
raider_count = int(raid.get("tags", {}).get("msg-param-viewerCount", -1))
_raid_timestamp = raid.get("tags", {}).get("tmi-sent-ts")
log.info(f"RAID from {raider} with {raider_count} raiders just happened!")
log.debug(f"{raid}")
async def raised_command(cmd: ChatCommand):
fmt_dict = {
"user": cmd.user.name,
"cmd": "traised",
}
if not (cmd.user.mod or cmd.user.name.lower() in SETTINGS.twitch.admin_users):
log.warning(SETTINGS.fmt.cmd_blocked.format(**fmt_dict))
return
so_far_total_min = calc_time_so_far().total_seconds() / 60
fmt_dict = {
**fmt_dict,
"so_far_total_min": so_far_total_min,
"so_far_hrs": so_far_total_min // 60,
"so_far_min": so_far_total_min % 60,
"min_paid_for": Donos().calc_chat_minutes(),
"min_total": Donos().calc_total_minutes(),
"min_end_at": calc_end().total_seconds() / 60,
"end_ts": End().end_ts,
"end_min": End().end_min,
"total_value": Donos().calc_dollars(),
"countdown": calc_timer(),
"bits": Donos().bits,
"tips": Donos().tips,
"subs": Donos().subs,
"subs_t1": Donos().subs_t1,
"subs_t2": Donos().subs_t2,
"subs_t3": Donos().subs_t3,
"pause_min": Pause().minutes,
"pause_start": Pause().start or "Not Currently Paused",
}
response = SETTINGS.fmt.traised_success.format(**fmt_dict)
log.info(response)
await cmd.reply(response)
def calc_end() -> timedelta:
"""Find the timedelta to use for final calculations"""
if End().end_min:
return timedelta(minutes=End().end_min)
minutes = Donos().calc_total_minutes()
if SETTINGS.end.max_minutes:
minutes = min(minutes, SETTINGS.end.max_minutes)
return timedelta(minutes=minutes)
def calc_time_so_far() -> timedelta:
"""How much time has been counted down since the start"""
if End().is_ended():
cur_time = End().end_ts
elif Pause().is_paused():
cur_time: datetime = Pause().start
else:
cur_time = datetime.now(tz=timezone.utc)
time_so_far = cur_time - SETTINGS.start.time
corrected_tsf = time_so_far - timedelta(minutes=Pause().minutes)
return corrected_tsf
def calc_timer(handle_end: bool = True) -> str:
"""Generate the timer string from the difference between paid and run minutes"""
if handle_end:
End().handle_end(calc_time_so_far, calc_end, Donos().calc_total_minutes)
remaining = calc_end() - calc_time_so_far()
hours = int(remaining.total_seconds() / 60 / 60)
minutes = int(remaining.total_seconds() / 60) % 60
seconds = int(remaining.total_seconds()) % 60
time_str = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
if Pause().is_paused():
pause_format = SETTINGS.fmt.countdown_pause
return pause_format.format(clock=time_str)
else:
return time_str
async def channel_offline(_event):
now = datetime.now(tz=timezone.utc)
if Pause().is_paused():
log.info(f"Channel went offline at {now.isoformat()}, already was paused at {Pause().start.isoformat()}")
elif SETTINGS.twitch.pause_on_offline:
now = Pause().start_pause("channel went offline")
log.info(f"Channel went offline at {now.isoformat()}, pause started")
else:
log.info(f"Channel went offline at {now.isoformat()}, but pause not started, timer is still running")
async def channel_online(_event):
now = datetime.now(tz=timezone.utc)
if Pause().is_paused() and SETTINGS.twitch.unpause_on_online:
added_min = Pause().resume("channel went online")
log.info(
f"Pause resumed with an addition of {added_min:.02f} minutes"
f" for a total of {Pause().minutes:.02f} minutes"
)
elif Pause().is_paused():
delta = (now - Pause().start).total_seconds() / 60.0
log.info(
f"Channel went online at {now.isoformat()}, pause started at {Pause().start.isoformat()} or {delta} min ago"
)
else:
log.info(f"Channel went online at {now.isoformat()}, but time was not paused?!?")
async def store_user_token(user_auth_token, user_auth_refresh_token):
usr_token_file = Path(SETTINGS.twitch.user_token_file)
usr_token_file.write_text(toml.dumps({"token": user_auth_token, "refresh_token": user_auth_refresh_token}))
log.info(f"Wrote updated {usr_token_file}")
@asynccontextmanager
async def lifespan(app: FastAPI):
# set up twitch api instance and add user authentication with some scopes
twitch = await Twitch(SETTINGS.twitch.app_id, SETTINGS.twitch.app_secret.get_secret_value())
twitch.user_auth_refresh_callback = store_user_token
usr_token_file = Path(SETTINGS.twitch.user_token_file)
if usr_token_file.is_file():
user_auth = toml.load(usr_token_file)
token, refresh_token = user_auth["token"], user_auth.get("refresh_token")
if not refresh_token:
twitch.auto_refresh_auth = False
else:
auth = UserAuthenticator(twitch, USER_SCOPE, url=SETTINGS.twitch.auth_url)
class FakeWebBrowser:
@staticmethod
def get(*_args, **_kwargs):
class FakeBrowser:
@staticmethod
def open(url, *_args, **_kwargs):
log.info(f"Please open {url}")
return FakeBrowser()
# Patch the webbrowser import to print the url to load
twitchAPI.oauth.webbrowser = FakeWebBrowser()
token, refresh_token = await auth.authenticate()
await store_user_token(token, refresh_token)
try:
await twitch.set_user_authentication(token, USER_SCOPE, refresh_token)
except TwitchAPIException:
log.error(f"Invalid token, please remove {usr_token_file} try again")
raise
# Get id for twitch channel
channel = await first(twitch.get_users(logins=[SETTINGS.twitch.channel]))
# create eventsub websocket instance and start the client.
eventsub = None
if SETTINGS.twitch.eventsub:
eventsub = EventSubWebsocket(twitch, callback_loop=asyncio.get_running_loop())
eventsub.start()
await eventsub.listen_stream_offline(channel.id, channel_offline)
await eventsub.listen_stream_online(channel.id, channel_online)
# create chat instance
chat = await Chat(
twitch,
initial_channel=[SETTINGS.twitch.channel],
callback_loop=asyncio.get_running_loop(),
no_message_reset_time=6,
)
# listen to when the bot is done starting up and ready to join channels
chat.register_event(ChatEvent.READY, on_ready)
# listen to chat messages
chat.register_event(ChatEvent.MESSAGE, on_message)
# listen to channel subscriptions
chat.register_event(ChatEvent.SUB, on_sub)
# listen to channel raids
chat.register_event(ChatEvent.RAID, on_raid)
# you can directly register commands and their handlers
if SETTINGS.twitch.enable_cmds:
chat.register_command_middleware(OnlyMyChat())
chat.register_command("tpause", pause_command)
chat.register_command("tresume", resume_command)
chat.register_command("tadd", add_time_command)
chat.register_command("tremove", remove_time_command)
chat.register_command("traised", raised_command)
chat.register_command("taddtip", add_tip_command)
if Spins.enabled:
chat.register_command("tspin", spin_done_command)
# we are done with our setup, lets start this bot up!
chat.start()
yield # Run FastAPI stuff
if eventsub:
await eventsub.stop()
# now we can close the chat bot and the twitch api client
chat.stop()
await twitch.close()
log.info("Done shutting down TwitchAPI")
app = FastAPI(lifespan=lifespan)
class JSONResponse(JSONResponse):
def render(self, content) -> bytes:
return json.dumps(content, ensure_ascii=False, allow_nan=False, indent=4, separators=(",", ":")).encode("utf-8")
@app.get("/live_stats", response_class=JSONResponse)
async def get_live_stats():
full_stats = {"pause": Pause().to_dict(), "donos": Donos().to_dict(), "end": End().to_dict()}
return jsonable_encoder(full_stats)
@app.get("/live_stats/bits", response_class=PlainTextResponse)
async def get_live_stats_bits():
return f"{Donos().bits}"
@app.get("/live_stats/tips", response_class=PlainTextResponse)
async def get_live_stats_tips():
return f"${Donos().tips:.02f}"
@app.get("/live_stats/subs", response_class=PlainTextResponse)
async def get_live_stats_subs():
return str(Donos().subs)
@app.get("/live_stats/total_value", response_class=PlainTextResponse)
async def get_total_value():
return f"${Donos().calc_dollars():0.02f}"
if Spins.enabled:
@app.get("/live_stats/spins", response_class=PlainTextResponse)
async def get_total_value():
return f"{Spins().performed}/{Spins().calc_todo(Donos().calc_dollars()):0.1f}"
@app.get("/calc_timer", response_class=PlainTextResponse)
async def get_calc_timer():
return calc_timer()
@app.get("/traised", response_class=JSONResponse)
async def traised_fields():
so_far_total_min = calc_time_so_far().total_seconds() / 60
return {
"so_far_total_min": so_far_total_min,
"so_far_hrs": so_far_total_min // 60,
"so_far_min": so_far_total_min % 60,
"min_paid_for": Donos().calc_chat_minutes(),
"min_total": Donos().calc_total_minutes(),
"min_end_at": calc_end().total_seconds() / 60,
"end_ts": End().end_ts,
"end_min": End().end_min,
"total_value": Donos().calc_dollars(),
"countdown": calc_timer(),
"bits": Donos().bits,
"tips": Donos().tips,
"subs": Donos().subs,
"subs_t1": Donos().subs_t1,
"subs_t2": Donos().subs_t2,
"subs_t3": Donos().subs_t3,
"pause_min": Pause().minutes,
"pause_start": Pause().start or "Not Currently Paused",
}
@app.get("/events", response_class=HTMLResponse)
async def get_events(timezone: Optional[str] = None):
if timezone is None:
tz = ZoneInfo("UTC")
else:
try:
tz = ZoneInfo(timezone)
except ZoneInfoNotFoundError as e:
return f"<html><body><xmp>{e}</xmp></body></html>"
events_per_day = {}
for row in Donos.csv_iter():
row["time"]: datetime = datetime.fromtimestamp(int(row["time"]) / 1000).astimezone(tz)
day = row["time"].date()
day_list = events_per_day.setdefault(day, [])
day_list.append(row)
build_table = ""
for date, rows in events_per_day.items():
build_table += f"\n<h1>{date:%Y-%m-%d}</h1>\n<table>\n"
build_table += "<tr>" + "".join(f"<th>{s}</th>" for s in CSV_COLUMNS) + "</tr>\n"
for row in rows:
build_table += "<tr>" + "".join(f"<td>{row[s]}</td>" for s in CSV_COLUMNS) + "</tr>\n"
build_table += "</table>\n"
style = """table {border: 2px solid rgb(140 140 140);}
th,td {border: 1px solid rgb(160 160 160);}"""
return f"<html><head><style>{style}</style></head><body>{build_table}</body></html>"
@app.get("/events_csv", response_class=PlainTextResponse)
async def get_events_csv():
return Donos.dono_path.read_text()
@app.get("/events_targets", response_class=JSONResponse)
async def get_events_targets():
donation_targets = {"tips": {}, "bits": {}}
for row in Donos.csv_iter():
if row["type"] not in donation_targets:
continue
target = row["target"] or "<untagged>"
value = float(row["amount"]) if row["type"] == "tips" else int(row["amount"])
if target in donation_targets[row["type"]]:
donation_targets[row["type"]][target] += value
else:
donation_targets[row["type"]][target] = value
return donation_targets
@app.get("/donors", response_class=HTMLResponse)
async def get_donors(sort: str = "total"):
donor_keys = {
"name": (lambda x: x["name"].lower(), False),
"total": (lambda x: x["total"], True),
**{k: (lambda x, y=k: x[y], True) for k in CSV_TYPES},
}
if sort not in donor_keys:
return f"<html><body><xmp>{sort} not in {tuple(donor_keys.keys())}</xmp></body></html>"
donor_db = {}
for row in Donos.csv_iter():
user_db = donor_db.setdefault(
row["user"].lower(), {"name": row["user"], "total": 0, **{k: 0 for k in CSV_TYPES}}
)
amount = float(row["amount"]) if row["type"] == TIPS else int(row["amount"])
user_db[row["type"]] += amount
user_db["total"] += amount * SETTINGS.get_value(row["type"])
build_table = "<table>\n"
build_table += "<tr>" + "".join(f"<th><a href='?sort={s}'>{s}</a></th>" for s in donor_keys) + "</tr>\n"
for row in sorted(donor_db.values(), key=donor_keys[sort][0], reverse=donor_keys[sort][1]):
build_table += "<tr>" + "".join(f"<td>{row[s]}</td>" for s in donor_keys) + "</tr>\n"
build_table += "</table>\n"
style = """table {border: 2px solid rgb(140 140 140);}
th,td {border: 1px solid rgb(160 160 160);}"""
return f"<html><head><style>{style}</style></head><body>{build_table}</body></html>"
websocket_html = """
<!DOCTYPE html>
<html>
<head>
<title>{name}</title>
<style>{css}</style>
</head>
<body>
<div id='text'>Not Yet Connected</div>
<script>
var ws
function connect() {{
ws = new WebSocket("{hostname}/{path}");
ws.onmessage = function(event) {{ document.getElementById('text').innerText = event.data }}
}}
connect()
var interval = setInterval(function() {{
if (ws.readyState === WebSocket.CLOSED) {{ connect() }}
}}, 60000);
</script>
</body>
</html>
"""
@app.get("/live", response_class=HTMLResponse)
async def get_live_timer():
return websocket_html.format(name="countdown", css=SETTINGS.output.css, hostname=SETTINGS.output.public, path="ws")
@app.get("/live_counter", response_class=HTMLResponse)
async def get_live_counter(
item: Optional[Literal["tips", "bits", "subs", "subs_t1", "subs_t2", "subs_t3", "total"]] = None
):
if item is None:
return (
"<html><body>"
", ".join(
f"<a href='?item={s}'>{s}</a>"
for s in ("tips", "bits", "subs", "subs_t1", "subs_t2", "subs_t3", "total")
)
+ "</body></html>"
)
else:
return websocket_html.format(
name=f"{item} counter",
css=SETTINGS.output.css,
hostname=SETTINGS.output.public,
path=f"ws_counter?item={item}",
)
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
try:
while True:
try:
await websocket.send_text(calc_timer())
await asyncio.sleep(0.5)
except ConnectionClosedOK:
break
except WebSocketDisconnect:
pass
@app.websocket("/ws_counter")
async def websocket_counter_endpoint(
websocket: WebSocket, item: Literal["tips", "bits", "subs", "subs_t1", "subs_t2", "subs_t3", "total"]
):
last_sent = None
money = {"tips", "total"}
await websocket.accept()
try:
while True:
try:
if item in money:
cur_msg = f"${getattr(Donos(), item):.02f}"
else:
cur_msg = str(getattr(Donos(), item))
if cur_msg != last_sent:
await websocket.send_text(cur_msg)
last_sent = cur_msg
await asyncio.sleep(1)
except ConnectionClosedOK:
break
except WebSocketDisconnect:
pass
@app.put("/admin/end/clear", response_class=JSONResponse)
async def put_end_clear(password: str):
SETTINGS.raise_on_bad_password(password)
old_values = End().to_dict()
try:
End().clear("/admin/end/clear")
return {"old": old_values, "new": End().to_dict()}
except EndException as e:
raise HTTPException(status_code=409, detail=str(e))
@app.put("/admin/pause/begin", response_class=JSONResponse)
async def put_pause_begin(password: str, time: Optional[datetime] = None):
SETTINGS.raise_on_bad_password(password)
old_values = Pause().to_dict()
try:
if time is None:
Pause().start_pause("/admin/pause/start")
return {"old": old_values, "new": Pause().to_dict()}
else:
Pause().set_pause_start(time, "/admin/pause/start")
return {"old": old_values, "new": Pause().to_dict()}
except PauseException as e:
raise HTTPException(status_code=409, detail=str(e))
@app.put("/admin/pause/resume", response_class=JSONResponse)
async def put_pause_resume(password: str, time: Optional[datetime] = None):
SETTINGS.raise_on_bad_password(password)
old_values = Pause().to_dict()
try:
if time is None:
Pause().resume("/admin/pause/resume")
return {"old": old_values, "new": Pause().to_dict()}
else:
Pause().resumed_at(time, "/admin/pause/resume")
return {"old": old_values, "new": Pause().to_dict()}
except PauseException as e:
raise HTTPException(status_code=409, detail=str(e))
@app.put("/admin/pause/abort", response_class=JSONResponse)
async def put_pause_abort(password: str):
SETTINGS.raise_on_bad_password(password)
old_values = Pause().to_dict()
try:
Pause().abort_current("/admin/pause/resume")
return {"old": old_values, "new": Pause().to_dict()}
except PauseException as e:
raise HTTPException(status_code=409, detail=str(e))
@app.put("/admin/pause/minutes", response_class=JSONResponse)
async def put_set_minutes(password: str, minutes: float):
SETTINGS.raise_on_bad_password(password)
old_values = Pause().to_dict()
try:
Pause().pause_set(minutes, "/admin/pause/minutes")
return {"old": old_values, "new": Pause().to_dict()}
except PauseException as e:
raise HTTPException(status_code=409, detail=str(e))
@app.put("/admin/donos/reload", response_class=JSONResponse)
async def put_donos_reload(password: str):
SETTINGS.raise_on_bad_password(password)
old_values = Donos().to_dict()
Donos().reload_csv()
return {"old": old_values, "new": Donos().to_dict()}
@app.put("/admin/donos/wipe", response_class=JSONResponse)
async def put_donos_wipe(password: str, are_you_sure: bool = False):
SETTINGS.raise_on_bad_password(password)
old_values = Donos().to_dict()
filename = None
if are_you_sure:
filename = Donos().clear_csv()
return {"old": old_values, "new": Donos().to_dict(), "backup": filename}
if __name__ == "__main__":
Pause.load_pause()
Spins.load_spins()
Donos.load_csv()
End.load_end()
End().handle_end(calc_time_so_far, calc_end, Donos().calc_total_minutes)
log.info(f"Users who can run cmds in addition to mods {SETTINGS.twitch.admin_users}")
import uvicorn
try:
uvicorn.run(app, host=SETTINGS.output.listen, port=SETTINGS.output.port)
except KeyboardInterrupt:
pass