import argparse import time import datetime from enum import Enum import re class Channel(Enum): WHISPER = 0 GLOBAL = 1 PARTY = 2 LOCAL = 3 TRADE = 4 GUILD = 5 channel_mapping = {'#': Channel.GLOBAL, '@': Channel.WHISPER, '%': Channel.PARTY, '': Channel.LOCAL, '$': Channel.TRADE, '&': Channel.GUILD} class Message(): def __init__(self, message: str, date: datetime.datetime, user: str, channel: Channel) -> None: self.message = message self.date = date self.user = user self.channel = channel def __str__(self) -> str: return f'{self.date} - {self.channel.name}: {self.user}: {self.message}' def follow(thefile: str): thefile.seek(0, 2) while True: line = thefile.readline() if not line: time.sleep(0.1) continue yield line def parse_string(text: str) -> Message: # result = parse('{date} {time} {timemicro} {noidea} [{level} {client} {id}]{mess_type:1}{user}: {message}', text) result = re.search('(?P\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d) (\d+) (\S+) \[(?P\S+) (\S+) (\d+)\] (?P[#@%$&]*)(?P[^:]+): (?P.*)', text) if not result: return None date = datetime.datetime.strptime(result.group('date'), '%Y/%m/%d %H:%M:%S') return Message(result.group('message'), date, result.group('user'), channel_mapping[result.group('channel')]) def setup_args() -> argparse.Namespace: parser = argparse.ArgumentParser( description='Poe Trader', epilog="And that's how you trade") parser.add_argument( '-l', '--logfile', help='Path of the logfile that should be used', default=r'D:\Poe\logs\Client.txt') return parser.parse_args() if __name__ == "__main__": args = setup_args() logfile = open(args.logfile, 'r', encoding='utf8') loglines = follow(logfile) for line in loglines: message = parse_string(line) print(message)