|
- import datetime
- import sys
-
-
- assert len(sys.argv) == 2, 'Arg: filename'
- filename = sys.argv[1]
-
- date = filename.rsplit('/', 1)[-1][:10]
-
- with open(filename, 'r') as fp:
- for line in fp:
- origLine = line
- if not (line[0] == '[' and line[3] == ':' and line[6:8] == '] ' and line[-1] == '\n'):
- print(f'MALFORMED LINE: {line!r}', file = sys.stderr)
- continue
- time = line[1:6]
- line = line[8:-1]
- ts = datetime.datetime(int(date[:4]), int(date[5:7]), int(date[8:]), int(time[:2]), int(time[3:]), 0).replace(tzinfo = datetime.timezone.utc).timestamp()
- if line.startswith('<'): #PRIVMSG
- print(f'{ts} PRIVMSG {line}')
- elif line.startswith('* '): #ACTION
- print(f'{ts} ACTION {line[2:]}')
- elif line.startswith('*** '):
- words = line.split(' ')[1:]
- if words[1:3] == ['has', 'joined']: # JOIN
- print(f'{ts} JOIN {words[0]} joins')
- elif words[0] == 'Joins:': # JOIN
- print(f'{ts} JOIN {words[1]} joins')
- elif words[1:3] == ['has', 'left']: # PART
- reason = f' [{" ".join(words[3:])}]' if len(words) > 4 or words[3] != '' else ''
- print(f'{ts} PART {words[0]} leaves{reason}')
- elif words[0] == 'Parts:': # PART
- reason = f' [{" ".join(words[3:])[1:-1]}]' if len(words) > 4 or words[3] != '()' else ''
- print(f'{ts} PART {words[1]} leaves{reason}')
- elif words[1:4] == ['has', 'quit', 'IRC']: # QUIT
- print(f'{ts} QUIT {words[0]} quits [{" ".join(words[4:])[1:-1]}]')
- elif words[0] == 'Quits:': # QUIT
- reason = f' [{" ".join(words[3:])[1:-1]}]' if len(words) > 4 or words[3] != '()' else ''
- print(f'{ts} QUIT {words[0]} quits{reason}')
- elif words[1:4] == ['was', 'kicked', 'by']: # KICK
- print(f'{ts} KICK {words[0]} is kicked by {words[4]} [{" ".join(words[5:])[1:-1]}]')
- elif words[1:3] == ['sets', 'mode:']: # MODE
- print(f'{ts} MODE {line[4:]}')
- elif words[1:4] == ['changes', 'topic', 'to:']: # TOPIC
- print(f'{ts} TOPIC {words[0]} sets the topic to: {" ".join(words[4:])}')
- elif words[1:4] == ['changes', 'topic', 'to']: # TOPIC
- print(f'{ts} TOPIC {words[0]} sets the topic to: {" ".join(words[4:])[1:-1]}')
- elif words[1:5] == ['is', 'now', 'known', 'as']: # NICK
- print(f'{ts} NICK {line[4:]}')
- elif words[1:3] == ['starts', 'logging']: # Silently ignore (there's already a JOIN)
- pass
- else:
- print(f'MALFORMED LINE: {origLine!r}', file = sys.stderr)
- else:
- print(f'MALFORMED LINE: {origLine!r}', file = sys.stderr)
|