forked from dekuNukem/hirc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hirc.py
63 lines (52 loc) · 1.77 KB
/
hirc.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
import os
import sys
import time
import threading
import irc_bot_noblock
from datetime import datetime
# change it to your own
# get your oauth here: https://twitchapps.com/tmi/
nickname = 'twitch_plays_3ds'
oauth = 'oauth:qmdwk3rsm4qau59zf2dpxixsf4wxzf'
def ensure_dir(dir_path):
if not os.path.exists(dir_path):
print("creating directory " + dir_path)
os.makedirs(dir_path)
def worker():
global last_message
while 1:
last_message = input()
time.sleep(0.01)
def safe_print(item):
try:
print(item)
except Exception:
print(item.encode('utf-8'))
def log_msg(name, msg):
msg = msg.replace('\r', '').replace('\n', '')
with open('./comment_log/' + chat_channel + ".txt", mode='a', encoding='utf-8') as log_file:
log_file.write(datetime.utcnow().isoformat(sep='T') + "Z " + name + ': ' + msg + '\r\n')
if(len(sys.argv) != 2):
print (__file__ + ' chat_channel')
exit()
ensure_dir('./comment_log')
last_message = ''
chat_channel = sys.argv[1].lower().lstrip().rstrip()
chat_server = ['irc.chat.twitch.tv', 6667]
bot = irc_bot_noblock.irc_bot(nickname, oauth, chat_channel, chat_server[0], chat_server[1], timeout=300)
bot.connect()
t = threading.Thread(target=worker)
t.start()
while 1:
tmi_list = bot.get_parsed_message()
tmi_list.reverse()
for item in [x for x in tmi_list if "." not in x.username]:
message_orig = item.message.replace(chr(1) + "ACTION", "/me").replace(chr(1), '').lstrip().rstrip()
log_msg(item.username, message_orig)
safe_print(item.username + ": " + message_orig)
if last_message != '':
safe_print(">> " + nickname + ": " + last_message)
bot.send_message(last_message)
log_msg(nickname, last_message)
last_message = ''
time.sleep(0.01)