-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomment_logger_srt.py
122 lines (100 loc) · 3.66 KB
/
comment_logger_srt.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
import os
import sys
import time
from datetime import datetime
import socket
import irc_bot
import configparser
from pysrt import SubRipFile
from pysrt import SubRipItem
from pysrt import SubRipTime
def iso8601_utc_now():
return datetime.utcnow().isoformat(sep='T') + "Z"
def make_offset_str(offset_hours):
offset_hours = int(offset_hours)
if offset_hours == 0:
return "Z"
if offset_hours > 0:
sign = "+"
else:
sign = "-"
offset_str = str(abs(offset_hours))
if len(offset_str) < 2:
offset_str = "0" + offset_str
return sign + offset_str + ":00"
def iso8601_local_now():
return datetime.now().isoformat(sep='T') + make_offset_str(utc_offset_hours)
def parse_chat_server(chat_server):
return chat_server.replace(' ', '').split(':')
def ensure_dir(dir_path):
if not os.path.exists(dir_path):
print("creating directory " + dir_path)
os.makedirs(dir_path)
def log_add(path, content):
with open(path, mode='a', encoding='utf-8') as log_file:
log_file.write(content)
def safe_print(content):
try:
print(content)
except UnicodeEncodeError:
print(content.encode('utf-8'))
def get_timestamp(ts_format):
if ts_format == 0:
return str(time.time())[:15]
elif ts_format == 2:
return iso8601_local_now()
else:
return iso8601_utc_now()
if(len(sys.argv) != 3):
print(__file__ + ' channel server_type')
sys.exit(0)
current_directory = os.path.dirname(os.path.abspath(__file__))
config_path = current_directory + "/config.txt"
if os.path.isfile(config_path):
config = configparser.ConfigParser()
config.read(config_path)
username = config.get('Settings', 'username').replace(' ', '').lower()
oauth = config.get('Settings', 'oauth')
record_raw = config.getboolean('Settings', 'record_raw')
timestamp_format = config.getint('Settings', 'timestamp_format')
twitchclient_version = config.getint('Settings', 'twitchclient_version')
regular_chat_server = config.get('Settings', 'regular_chat_server')
group_chat_server = config.get('Settings', 'group_chat_server')
event_chat_server = config.get('Settings', 'event_chat_server')
else:
print("config.txt not found", file=sys.stderr)
sys.exit(0)
ts = time.time()
utc_offset_hours = int(int((datetime.fromtimestamp(ts) - datetime.utcfromtimestamp(ts)).total_seconds()) / 3600)
server_dict = {'r':parse_chat_server(regular_chat_server), 'g':parse_chat_server(group_chat_server), 'e':parse_chat_server(event_chat_server)}
chat_channel = sys.argv[1]
chat_server = server_dict[sys.argv[2].lower()]
ensure_dir(current_directory + '/comment_log')
if record_raw:
ensure_dir(current_directory + '/comment_log_raw')
raw_log_path = current_directory + '/comment_log_raw/' + chat_channel + '.txt'
log_path = current_directory + '/comment_log/' + chat_channel + '.txt'
srt_log_path = current_directory + '/comment_log/' + chat_channel + '.srt'
bot = irc_bot.irc_bot(username, oauth, chat_channel, chat_server[0], chat_server[1], twitchclient_version = twitchclient_version)
outsrt = SubRipFile()
text = ''
while 1:
raw_msg_list = bot.get_message()
if len(raw_msg_list) > 0:
if len(text) > 0:
end = SubRipTime.from_time(datetime.now())
item = SubRipItem(0, start, end, text)
outsrt.append(item)
start = SubRipTime.from_time(datetime.now())
text = ''
timestamp = get_timestamp(timestamp_format)
for item in raw_msg_list:
if record_raw:
log_add(raw_log_path, timestamp + ' ' + item + '\n')
username, message = irc_bot.parse_user(item)
if username != '':
safe_print(chat_channel + " " + username + ": " + message)
log_add(log_path, timestamp + ' ' + username + ': ' + message + '\n')
text += username + ": " + message + '\n'
outsrt.clean_indexes()
outsrt.save(srt_log_path, encoding='utf-8')