-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pogsmithy-Twitch.py
721 lines (607 loc) · 29.7 KB
/
Pogsmithy-Twitch.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
718
719
720
721
import sys
import asyncio
import queue
import socket
import websockets
import time
from datetime import datetime
import logging
import signal
import requests
import threading
import os
import shutil
import argparse
import pickle
import os.path
import pytz
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from cachetools import TTLCache
last_huge_squad = datetime.utcnow()
huge_squad_cooldown_seconds = 30
last_marker = datetime.utcnow()
marker_cooldown_seconds = 15
vanish_user_cache = TTLCache(maxsize=256, ttl=3600)
twitch_wss_uri = "wss://irc-ws.chat.twitch.tv:443"
max_backoff = 64
backoff_time = 1
# Port defaults
port = 8883
# Configure logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
streamHandler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)
log_rotation = 0
twitch_chat_logger = None
config_user = None
config_channel = None
config_token = None
config_tabwire_token = None
config_drive_folder_id = None
config_drive_pickle = None
log_timer = None
log_frequency = 86400 # 1 day
shoutout_queue = queue.Queue()
siege_rank_names = {}
siege_rank_mmrs = {}
siege_rank_strings = {}
valorant_rank_names = {}
valorant_rank_strings = {}
account_uuids = {
'MontagneMontoya': '3cb45a4a-9208-48ac-8690-27dcbf1b6604',
'Burt-Macklin': '8da0d780-e164-408b-ab03-26bc329346bb',
'Dinger-Bringer': 'd8d20ab9-5747-43ed-a0fb-d0a2e0c0d75f',
'Jerry-Gergich': 'ce351727-71cc-4a05-858a-a9dcd208d25c',
'DoubleEh7': 'a0ae5914-21ae-4850-9482-fb10e294492e',
}
banned_words_original = {'joel', 'ellie', 'tlou', 'naughtydog', 'lastofus'}
banned_words_permutated = set().union(banned_words_original)
leet_speak_map = {
'a': ['@'],
'o': ['0'],
'i': ['1', 'l', '!'],
'l': ['1', 'i', '!'],
't': ['7'],
's': ['5'],
'e': ['3'],
'b': ['8']
}
def create_permutations(passed_word):
index = 0
while index < len(passed_word):
original_word_start = passed_word[:index]
original_word_end = passed_word[index:]
for letter in original_word_end:
if letter in leet_speak_map.keys():
for new_letter in leet_speak_map[letter]:
new_word = original_word_start + original_word_end.replace(letter, new_letter)
if new_word not in banned_words_permutated:
banned_words_permutated.add(new_word)
create_permutations(new_word)
index += 1
for original_word in banned_words_original:
create_permutations(original_word)
def spoiler_check(message_passed):
formatted = message_passed.replace(' ', '')
formatted = formatted.lower()
length = len(formatted)
substring_list = [formatted[i:j + 1] for i in range(length) for j in range(i, length)]
for substring in substring_list:
if substring in banned_words_permutated:
return True
return False
class ValorantRankUpdateThread(threading.Thread):
def __init__(self, account_name):
threading.Thread.__init__(self)
self.account_name = account_name
def run(self):
get_valorant_rank_with_name(self.account_name)
class SiegeRankUpdateThread(threading.Thread):
def __init__(self, tabwire_token, account_name):
threading.Thread.__init__(self)
self.tabwire_token = tabwire_token
self.account_name = account_name
def run(self):
get_siege_rank_with_uuid(self.tabwire_token, self.account_name)
def get_short_siege_rank_from_number(number):
rank_name_array = ['', '', '', '', '', 'Copper 3', 'Copper 2', 'Copper 1', 'Bronze 3', 'Bronze 2', 'Bronze 1',
'Silver 3', 'Silver 2', 'Silver 1', 'Gold 3', 'Gold 2', 'Gold 1',
'Plat 3', 'Plat 2', 'Plat 1', 'Diamond']
return rank_name_array[number]
def get_valorant_rank_from_number(number):
rank_name_array = ['Unrated', '', '', 'Iron 1', 'Iron 2', 'Iron 3', 'Bronze 1', 'Bronze 2', 'Bronze 3', 'Silver 1',
'Silver 2', 'Silver 3', 'Gold 1', 'Gold 2', 'Gold 3', 'Platinum 1', 'Platinum 2', 'Platinum 3',
'Diamond 1', 'Diamond 2', 'Diamond 3', 'Immortal 1', 'Immortal 2', 'Immortal 3', 'Radiant']
return rank_name_array[number]
def update_siege_rank_with_uuid(tabwire_token, account_uuid):
requests.get(f'https://r6.apitab.com/update/{account_uuid}?cid={tabwire_token}')
def get_siege_rank_with_uuid(tabwire_token, account_name):
account_uuid = account_uuids[account_name]
unix_stamp = int(datetime.utcnow().timestamp())
update_siege_rank_with_uuid(tabwire_token, account_uuid)
response = requests.get(f'https://r6.apitab.com/player/{account_uuid}?u={unix_stamp}&cid={tabwire_token}')
response_json = response.json()
siege_rank_names[account_name] = get_short_siege_rank_from_number(response_json['ranked']['rank'])
siege_rank_mmrs[account_name] = str(response_json['ranked']['mmr'])
if siege_rank_names[account_name] == '' or siege_rank_mmrs[account_name] == '0':
siege_rank_strings[account_name] = "Not placed"
else:
siege_rank_strings[account_name] = f"{siege_rank_names[account_name]} ({siege_rank_mmrs[account_name]})"
def get_valorant_rank_with_name(account_name):
formatted_name = account_name.replace('#', '-').lower()
unix_stamp_millis = int(datetime.utcnow().timestamp()) * 1000
response = requests.get(f'https://valorant.iesdev.com/player/{formatted_name}?version={unix_stamp_millis}')
response_json = response.json()
valorant_rank_names[account_name] = get_valorant_rank_from_number(response_json['ranks']['competitive']['tier'])
if valorant_rank_names[account_name] == '':
valorant_rank_strings[account_name] = "Not placed"
else:
valorant_rank_strings[account_name] = f"{valorant_rank_names[account_name]}"
def reset_backoff():
global backoff_time
backoff_time = 1
def increase_backoff():
global backoff_time
if backoff_time < max_backoff:
backoff_time = backoff_time * 2
def parse_irc_message(websocket_message):
try:
tags = None
if websocket_message.startswith('@'):
tags = {}
tags_string = websocket_message[1:websocket_message.find(' ')]
tags_list = tags_string.split(';')
for tag_string in tags_list:
tag_split = tag_string.split('=')
tags[tag_split[0]] = tag_split[1]
tagless_message = websocket_message[websocket_message.find(' ')+1:]
else:
tagless_message = websocket_message
user = None
exclaim_delimiter_index = tagless_message.find('!')
if exclaim_delimiter_index != -1 and exclaim_delimiter_index < tagless_message.find('tmi.twitch.tv'):
user = tagless_message[1:exclaim_delimiter_index]
command_index = tagless_message.find(' ')
command = tagless_message[command_index+1:tagless_message.find(' ', command_index+1)]
colon_index = tagless_message.find(':', 1)
message = tagless_message[colon_index + 1:].rstrip()
return tags, user, command, message
except:
logger.error("Error parsing this message: " + websocket_message)
async def connect_client(token, user):
logger.info('Attempting to connect...')
websocket_client = await websockets.connect(twitch_wss_uri, ssl=True)
await websocket_client.send("PASS {}".format(token))
await websocket_client.send("NICK {}".format(user))
name = await websocket_client.recv()
logger.debug(f"< {name}")
return websocket_client
async def join_channel(websocket_client, channel_name):
await websocket_client.send("JOIN #{}".format(channel_name))
stuff = await websocket_client.recv()
logger.debug(f"< {stuff}")
async def request_capabilities(websocket_client):
await websocket_client.send("CAP REQ :twitch.tv/membership")
stuff = await websocket_client.recv()
logger.debug(f"< {stuff}")
await websocket_client.send("CAP REQ :twitch.tv/tags")
stuff = await websocket_client.recv()
logger.debug(f"< {stuff}")
await websocket_client.send("CAP REQ :twitch.tv/commands")
stuff = await websocket_client.recv()
logger.debug(f"< {stuff}")
async def send_message(websocket_client, channel_name, message):
await websocket_client.send("PRIVMSG #{} :{}".format(channel_name, message))
# This kills the huge pogs
# response = await websocket_client.recv()
# logger.debug(f"< {response}")
async def handle_command(websocket_client, channel, user, command, args):
logger.info(f'Handling "{command}" command for {user} with args: {args}')
if command == 'pogproxy':
proxy_command = ' '.join(args)
logger.info(user + ' sent a pogproxy command: ' + proxy_command)
if user == 'gunsmithy':
await send_message(websocket_client, channel, proxy_command)
elif command == "paxy":
await send_message(websocket_client, channel, "https://i.imgur.com/7mqx3DV.png")
elif command == "iggy":
await send_message(websocket_client, channel, "Unlucky my dood FeelsBadMan")
elif command == "angery" or command == "grompy":
await send_message(websocket_client, channel, ">:(")
elif command == "dong":
await send_message(websocket_client, channel, "Huge Dongers! ヽ༼ຈل͜ຈ༽ノ")
elif command == "permitdylan":
await send_message(websocket_client, channel, "!permit Gunsmithy")
elif command == "bobs":
await send_message(websocket_client, channel, '( CoolStoryBob )( CoolStoryBob )')
elif command == "vanish":
if not vanish_user_cache.get(user):
await send_message(websocket_client, channel, "/timeout " + user + " 1")
vanish_user_cache[user] = True
elif command == "rank":
await send_message(websocket_client, channel, "Which rank? Try !r6rank or !valrank")
elif command == "r6rank" or command == "siegerank":
if channel == 'gunsmithy':
monty_thread = SiegeRankUpdateThread(config_tabwire_token, 'MontagneMontoya')
monty_thread.start()
monty_thread.join()
rank_message = '/me MontagneMontoya: ' + siege_rank_strings['MontagneMontoya']
elif channel == 'sasslyn':
burt_thread = SiegeRankUpdateThread(config_tabwire_token, 'Burt-Macklin')
dinger_thread = SiegeRankUpdateThread(config_tabwire_token, 'Dinger-Bringer')
jerry_thread = SiegeRankUpdateThread(config_tabwire_token, 'Jerry-Gergich')
doubleeh_thread = SiegeRankUpdateThread(config_tabwire_token, 'DoubleEh7')
burt_thread.start()
dinger_thread.start()
jerry_thread.start()
doubleeh_thread.start()
burt_thread.join()
dinger_thread.join()
jerry_thread.join()
doubleeh_thread.join()
try:
rank_message = '/me Burt\'s Rank: ' + siege_rank_strings['Burt-Macklin'] + \
' // Dinger\'s Rank: ' + siege_rank_strings['Dinger-Bringer'] + \
' // Jerry\'s Rank: ' + siege_rank_strings['Jerry-Gergich'] + \
' // DE7\'s Rank: ' + siege_rank_strings['DoubleEh7'] + \
' sasslyFlex'
except KeyError:
rank_message = "Woops, can't get your rank right now. Let Gunsmithy know or try again."
else:
rank_message = '/me I don\'t know you...'
await send_message(websocket_client, channel, rank_message)
elif command == "valrank" or command == "valorantrank":
if channel == 'sasslyn':
sass_thread = ValorantRankUpdateThread('Sasslyn#5455')
sass_thread.start()
sass_thread.join()
rank_message = '/me Sass\' Rank: ' + valorant_rank_strings.get('Sasslyn#5455', 'Unknown') + ' sasslyFlex'
if not valorant_rank_strings.get('Sasslyn#5455'):
logger.error("Unable to find rank in valorant_rank_strings dict.")
else:
rank_message = '/me I don\'t know you...'
await send_message(websocket_client, channel, rank_message)
elif command == "delhype":
await send_message(websocket_client, channel,
'sasslySip sasslyHype sasslySip sasslyHype sasslySip sasslyHype sasslySip')
elif command == "marker":
if args is not None and len(args) > 0:
await create_marker(websocket_client, channel, ' '.join(args))
else:
await create_marker(websocket_client, channel, f"{user}'s marker")
else:
logger.debug("Unrecognized command: " + command)
async def create_marker(websocket_client, channel, marker_message):
global last_marker
current_datetime = datetime.utcnow()
if (current_datetime - last_marker).seconds > marker_cooldown_seconds or \
(current_datetime - last_marker).days > 0:
await send_message(websocket_client, channel, '/marker ' + marker_message)
last_marker = current_datetime
async def handle_message(websocket_client, channel, user, message):
global last_huge_squad
msg_lower = message.lower()
# CHECK FOR POTENTIAL LAST OF US 2 SPOILERS
# if spoiler_check(message):
# await send_message(websocket_client, channel, "/timeout " + user + " 1")
# await send_message(websocket_client, channel, "@" + user + " No Last of Us 2 spoilers! marvHowdy")
# If a chat message starts with an exclamation point, try to run it as a command
if message.startswith('!'):
if ' ' in message:
args = message[message.find(' ')+1:].split(' ')
await handle_command(websocket_client, channel, user, message[1:message.find(' ')], args)
else:
await handle_command(websocket_client, channel, user, message[1:], None)
# Otherwise, look for other fun stuff in the message
elif "huge pogs" in msg_lower:
await send_message(websocket_client, channel, "FeelsBadMan")
elif "huge squad" in msg_lower:
if channel == 'sasslyn':
current_datetime = datetime.utcnow()
if (current_datetime - last_huge_squad).seconds > huge_squad_cooldown_seconds or \
(current_datetime - last_huge_squad).days > 0:
await send_message(websocket_client, channel, "sasslySquad1 sasslySquad2 sasslySquad3")
await send_message(websocket_client, channel, "sasslySquad4 sasslySquad5 sasslySquad6")
await send_message(websocket_client, channel, "sasslySquad7 sasslySquad8 sasslySquad9")
last_huge_squad = current_datetime
elif " lit " in msg_lower or msg_lower.startswith('lit ') or msg_lower.endswith(' lit') or msg_lower == 'lit':
if channel == 'jrod0901':
await send_message(websocket_client, channel, "blobSabers blobSabers blobSabers blobSabers blobSabers")
elif (channel == 'brittahkiin' or channel == 'iggyow') and 'bort' in msg_lower:
await send_message(websocket_client, channel, "bortTap bortTap bortTap bortTap bortTap")
elif "pogchamp" in msg_lower or "poggers" in msg_lower:
pog_champ_count = msg_lower.count("pogchamp")
poggers_count = msg_lower.count("poggers")
pog_count = pog_champ_count + poggers_count
if pog_count == 1:
await send_message(websocket_client, channel, "Pogs!")
elif pog_count == 2:
await send_message(websocket_client, channel, "POGGGGGERS!")
elif pog_count == 3:
await send_message(websocket_client, channel, "BIG POGS!")
elif pog_count == 4:
await send_message(websocket_client, channel, "HUUUUUGE POGS!")
elif pog_count > 4:
await send_message(websocket_client, channel, "MASSSSSIVE POGS!")
elif "pogs" in msg_lower:
pogs_count = msg_lower.count("pogs")
poggers_string = ''
for x in range(pogs_count):
poggers_string += 'POGGERS '
await send_message(websocket_client, channel, poggers_string)
elif "iggyowSmile" in message:
smile_count = message.count("iggyowSmile")
smile_string = ''
for x in range(smile_count):
smile_string += ':) '
await send_message(websocket_client, channel, smile_string)
elif "smile" in msg_lower or ":)" in msg_lower:
smile_text_count = msg_lower.count("smile")
smile_face_count = msg_lower.count(":)")
smile_count = smile_text_count + smile_face_count
smile_string = ''
for x in range(smile_count):
smile_string += 'iggyowSmile '
await send_message(websocket_client, channel, smile_string)
async def shoutout_run_loop():
while True:
queue_item = shoutout_queue.get()
if queue_item is None:
break
time.sleep(5)
await shout_out(queue_item['WebsocketClient'], queue_item['ChannelName'], queue_item['DisplayName'],
queue_item['Login'])
class ShoutoutThread(threading.Thread):
def __init__(self):
asyncio.get_event_loop()
threading.Thread.__init__(self)
def run(self):
asyncio.new_event_loop().run_until_complete(shoutout_run_loop())
async def shout_out(websocket_client, channel_name, display_name, login):
shout_out_message = f"HEY! Make sure you shoot {display_name} a good ole follow over at https://twitch.tv/{login}"
await send_message(websocket_client, channel_name, shout_out_message)
async def handle_messages(websocket_client, channel_name, chat_logger):
while True:
try:
received = await asyncio.wait_for(websocket_client.recv(), timeout=60.0)
if received == "PING :tmi.twitch.tv\r\n":
logger.debug('PING received, responding with PONG.')
await websocket_client.send("PONG :tmi.twitch.tv\r\n") # TODO - Maybe wrap this call in a wait_for too?
continue
tags, user, command, message = parse_irc_message(received)
if command == "PRIVMSG":
chat_logger.info(f'{user} - {message}')
await handle_message(websocket_client, channel_name, user, message)
elif command == "USERNOTICE":
if tags.get('msg-id') == 'raid':
if int(tags['msg-param-viewerCount']) > 1:
shoutout_queue.put({
"WebsocketClient": websocket_client,
"ChannelName": channel_name,
"DisplayName": tags['msg-param-displayName'],
"Login": tags['msg-param-login']
})
else:
logger.debug("Not a PRIVMSG or USERNOTICE, outputting below:")
logger.debug(f"< {received}")
except asyncio.TimeoutError:
# current_time_string = datetime.now(pytz.timezone('America/Toronto')).isoformat(timespec='seconds')
# logger.debug(f'Timeout: {current_time_string}')
pass
except KeyboardInterrupt:
shutdown()
def read_secret_file(file_path):
with open(file_path, 'r') as auth_file:
return auth_file.readline()
def config():
bot_user = os.getenv('POGSMITHY_TWITCH_USER')
bot_channel = os.getenv('POGSMITHY_TWITCH_CHANNEL')
bot_token = os.getenv('POGSMITHY_TWITCH_TOKEN')
bot_token_file = os.getenv('POGSMITHY_TWITCH_TOKEN_FILE')
bot_tabwire_token = os.getenv('POGSMITHY_TWITCH_TABWIRE_TOKEN')
bot_tabwire_token_file = os.getenv('POGSMITHY_TWITCH_TABWIRE_TOKEN_FILE')
bot_drive_folder = os.getenv('POGSMITHY_TWITCH_GDRIVE_FOLDER')
bot_drive_pickle_file = os.getenv('POGSMITHY_TWITCH_GDRIVE_PICKLE')
parser = argparse.ArgumentParser(description='Run Pogsmithy for Twitch.')
parser.add_argument('--user', dest='user', help='The Twitch User used to run the bot.')
parser.add_argument('--channel', dest='channel', help='The Twitch channel in which to run the bot.')
parser.add_argument('--token', dest='token', help='The Twitch OAuth token used to run the bot.')
parser.add_argument('--token-file', dest='token_file', help='The path to the file containing the Twitch OAuth token'
' used to run the bot.')
parser.add_argument('--tabwire-token', dest='tabwire_token', help='The Tabwire API token used to call their APIs.')
parser.add_argument('--tabwire-token-file', dest='tabwire_token_file', help='The path to the file containing the '
'Tabwire API token.')
parser.add_argument('--gdrive-folder', dest='gdrive_folder', help='The ID of the Google Drive folder for chat logs'
'if desired.')
parser.add_argument('--gdrive-pickle', dest='gdrive_pickle', help='The path to the Google Drive credentials pickle '
'file if uploading chat logs.')
args = parser.parse_args()
if args.user is not None:
logger.info('Using --user command-line argument...')
user = args.user
elif bot_user is not None:
logger.info('Using POGSMITHY_TWITCH_USER environment variable...')
user = bot_user
else:
logger.error('Bot user could not be derived from environment or arguments. Aborting...')
sys.exit(1)
if args.channel is not None:
logger.info('Using --channel command-line argument...')
channel = args.channel
elif bot_channel is not None:
logger.info('Using POGSMITHY_TWITCH_CHANNEL environment variable...')
channel = bot_channel
else:
logger.error('Bot channel could not be derived from environment or arguments. Aborting...')
sys.exit(1)
if args.token is not None:
logger.info('Using --token command-line argument...')
token = args.token
elif args.token_file is not None:
logger.info('Using --token-file command-line argument...')
token = read_secret_file(args.token_file)
elif bot_token is not None:
logger.info('Using POGSMITHY_TWITCH_TOKEN environment variable...')
token = bot_token
elif bot_token_file is not None:
logger.info('Using POGSMITHY_TWITCH_TOKEN_FILE environment variable...')
token = read_secret_file(bot_token_file)
else:
logger.error('Bot token could not be derived from environment or arguments. Aborting...')
sys.exit(1)
if args.tabwire_token is not None:
logger.info('Using --tabwire-token command-line argument...')
tabwire_token = args.tabwire_token
elif args.tabwire_token_file is not None:
logger.info('Using --tabwire-token-file command-line argument...')
tabwire_token = read_secret_file(args.tabwire_token_file)
elif bot_tabwire_token is not None:
logger.info('Using POGSMITHY_TWITCH_TABWIRE_TOKEN environment variable...')
tabwire_token = bot_tabwire_token
elif bot_tabwire_token_file is not None:
logger.info('Using POGSMITHY_TWITCH_TABWIRE_TOKEN_FILE environment variable...')
tabwire_token = read_secret_file(bot_tabwire_token_file)
else:
logger.error('Tabwire API token could not be derived from environment or arguments. Aborting...')
sys.exit(1)
if args.gdrive_folder is not None:
logger.info('Using --gdrive-folder command-line argument...')
folder_id = args.gdrive_folder
elif bot_drive_folder is not None:
logger.info('Using POGSMITHY_TWITCH_GDRIVE_FOLDER environment variable...')
folder_id = bot_drive_folder
else:
logger.info('No Google Drive folder ID provided. Chat logs will not be uploaded.')
folder_id = None
if args.gdrive_pickle is not None:
logger.info('Using --gdrive-pickle command-line argument...')
drive_pickle_file = args.gdrive_pickle
elif bot_drive_pickle_file is not None:
logger.info('Using POGSMITHY_TWITCH_GDRIVE_PICKLE environment variable...')
drive_pickle_file = bot_drive_pickle_file
else:
logger.info('No Google Drive pickle file provided. Chat logs will not be uploaded.')
drive_pickle_file = None
return user, channel, token, tabwire_token, folder_id, drive_pickle_file
def create_chat_logger():
# Logger for chat messages
chat_logger = logging.getLogger("ChatLogger")
chat_logger.setLevel(logging.INFO)
chat_file_log_handler = logging.FileHandler('twitch-chat-0.log', mode='w')
chat_file_log_handler.setLevel(logging.INFO)
chat_formatter = logging.Formatter('%(asctime)s - %(message)s')
chat_file_log_handler.setFormatter(chat_formatter)
chat_logger.addHandler(chat_file_log_handler)
return chat_logger
def upload_log_file(log_name, folder_id, drive_pickle):
if not folder_id:
logger.debug('Folder ID was not provided. Deleting chat logs...')
os.remove(log_name)
return
if not drive_pickle:
logger.debug('Drive pickle was not provided. Deleting chat logs...')
os.remove(log_name)
return
if os.path.getsize(log_name) == 0:
logger.debug(f'{log_name} is empty. Deleting chat logs...')
os.remove(log_name)
return
else:
logger.debug(f'{log_name} is not empty. Uploading...')
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is created automatically when the
# authorization flow completes for the first time.
if os.path.exists(drive_pickle):
with open(drive_pickle, 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
try:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', ['https://www.googleapis.com/auth/drive'])
creds = flow.run_local_server(port=0)
except (FileNotFoundError, IOError) as e:
logger.error('Credentials expired and could not open credentials.json. Probably running in Docker.'
'Not uploading or deleting log file.')
return
# Save the credentials for the next run
with open(drive_pickle, 'wb') as token:
pickle.dump(creds, token)
drive_service = build('drive', 'v3', credentials=creds)
file_metadata = {
'name': log_name,
'parents': [folder_id],
}
media = MediaFileUpload(log_name, mimetype='text/plain')
drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
del media
os.remove(log_name)
def move_log_file(channel_name):
global log_rotation
current_time_string = datetime.now(pytz.timezone('America/Toronto')).isoformat(timespec='seconds').replace(':', '.')
logger.debug(f'Moving log file {log_rotation} at {current_time_string}')
new_log_name = f'Twitch-Chat-{channel_name}-{current_time_string}.txt'
shutil.move(f'twitch-chat-{log_rotation}.log', new_log_name)
return new_log_name
def rotate_log_file(channel_name, folder_id, chat_logger):
global log_rotation, log_timer
chat_file_log_handler = logging.FileHandler(f'twitch-chat-{log_rotation+1}.log', mode='w')
chat_file_log_handler.setLevel(logging.INFO)
chat_formatter = logging.Formatter('%(asctime)s - %(message)s')
chat_file_log_handler.setFormatter(chat_formatter)
chat_logger.addHandler(chat_file_log_handler)
chat_logger.removeHandler(chat_logger.handlers[0])
new_log_name = move_log_file(channel_name)
upload_log_file(new_log_name, folder_id, config_drive_pickle)
log_rotation += 1
log_timer = threading.Timer(log_frequency, rotate_log_file, [config_channel, config_drive_folder_id, chat_logger])
log_timer.start()
def shutdown():
logger.info('Shutting down...')
twitch_chat_logger.removeHandler(twitch_chat_logger.handlers[0])
new_log_name = move_log_file(config_channel)
upload_log_file(new_log_name, config_drive_folder_id, config_drive_pickle)
log_timer.cancel()
shoutout_queue.put(None)
sys.exit(1)
def main(username, channel, token, chat_logger):
while True:
time.sleep(backoff_time)
try:
client = asyncio.get_event_loop().run_until_complete(connect_client(token, username))
reset_backoff()
asyncio.get_event_loop().run_until_complete(join_channel(client, channel))
asyncio.get_event_loop().run_until_complete(request_capabilities(client))
asyncio.get_event_loop().run_until_complete(handle_messages(client, channel, chat_logger))
except websockets.exceptions.ConnectionClosedError:
logger.error('Oof, ConnectionClosedError')
except socket.gaierror:
logger.error('Oof, gaierror')
increase_backoff()
except KeyboardInterrupt:
shutdown()
def receive_signal(signal_number, frame):
if signal_number == signal.SIGINT or signal_number == signal.SIGTERM:
shutdown()
return
if __name__ == "__main__":
config_user, config_channel, config_token, config_tabwire_token, config_drive_folder_id, config_drive_pickle = \
config()
twitch_chat_logger = create_chat_logger()
signal.signal(signal.SIGINT, receive_signal)
signal.signal(signal.SIGTERM, receive_signal)
log_timer = threading.Timer(log_frequency, rotate_log_file,
[config_channel, config_drive_folder_id, twitch_chat_logger])
log_timer.start()
shoutout_thread = ShoutoutThread()
shoutout_thread.start()
main(config_user, config_channel, config_token, twitch_chat_logger)