Added logger, parse_trade and some code format

This commit is contained in:
Oliver Hartmann 2021-03-12 15:32:10 +01:00
parent f0e10a7679
commit dd6f3f189c

View File

@ -3,14 +3,18 @@ import time
import datetime import datetime
from enum import Enum from enum import Enum
import re import re
import logging
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
class Channel(Enum): class Channel(Enum):
WHISPER = 0 WHISPER = 0
GLOBAL = 1 GLOBAL = 1
PARTY = 2 PARTY = 2
LOCAL = 3 LOCAL = 3
TRADE = 4 TRADE = 4
GUILD = 5 GUILD = 5
@ -21,6 +25,24 @@ channel_mapping = {'#': Channel.GLOBAL,
'$': Channel.TRADE, '$': Channel.TRADE,
'&': Channel.GUILD} '&': Channel.GUILD}
class Trade():
def __init__(self,
item: str,
amount: int,
currency: str,
tab: str,
row: int,
col: int,
league: str) -> None:
self.item = item
self.amount = amount
self.currency = currency
self.tab = tab
self.row = row
self.col = col
self.league = league
class Message(): class Message():
def __init__(self, def __init__(self,
message: str, message: str,
@ -30,17 +52,33 @@ class Message():
guild: str) -> None: guild: str) -> None:
self.message = message self.message = message
self.date = date self.date = date
self.user = user
self.channel = channel self.channel = channel
if channel is Channel.WHISPER:
user = user.lstrip('From ')
self.user = user
self.guild = guild self.guild = guild
def __str__(self) -> str: def __str__(self) -> str:
text = f'{self.date} - {self.channel.name}: ' text = f'{self.date} - {self.channel.name}: '
if self.guild: if self.guild:
text = text + f'<{self.guild}> ' text = text + f'<{self.guild}> '
text = text + f'{self.user}: {self.message}' text = text + f'{self.user}: {self.message}'
return text return text
def parse_trade(self) -> Trade:
# @From NyhaiPuki: Hi, I would like to buy your level 21 23% Vaal Impurity of Lightning listed for 18 chaos in Ritual (stash tab "$"; position: left 22, top 5)
regex = 'Hi, I would like to buy your (?P<item>.+) listed for (?P<amount>\d+) (?P<currency>\S+) in (?P<league>\S+) \(stash tab "(?P<tab>.+)"; position: left (?P<col>\d+), top (?P<row>\d+)\)'
res = re.search(regex, self.message)
return Trade(item=res['item'],
amount=int(res['amount']),
currency=res['currency'],
tab=res['tab'],
row=int(res['row']),
col=int(res['col']),
league=res['league'])
def follow(thefile: str): def follow(thefile: str):
thefile.seek(0, 2) thefile.seek(0, 2)
while True: while True:
@ -49,14 +87,16 @@ def follow(thefile: str):
time.sleep(0.1) time.sleep(0.1)
continue continue
yield line yield line
def parse_string(text: str) -> Message: def parse_string(text: str) -> Message:
# result = parse('{date} {time} {timemicro} {noidea} [{level} {client} {id}]{mess_type:1}{user}: {message}', text) result = re.search(
result = re.search('(?P<date>\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d) (\d+) (\S+) \[(?P<level>\S+) (\S+) (\d+)\] (?P<channel>[#@%$&]?)(?P<guild><\S+>)? ?(?P<user>[^:]+): (?P<message>.*)', text) '(?P<date>\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d) (\d+) (\S+) \[(?P<level>\S+) (\S+) (\d+)\] (?P<channel>[#@%$&]?)(?P<guild><\S+>)? ?(?P<user>[^:]+): (?P<message>.*)', text)
if not result: if not result:
log.debug(f'Result is none for text "{text}"')
return None return None
date = datetime.datetime.strptime(result.group('date'), '%Y/%m/%d %H:%M:%S') date = datetime.datetime.strptime(
result.group('date'), '%Y/%m/%d %H:%M:%S')
guild = result.group('guild') guild = result.group('guild')
if guild: if guild:
guild = guild.strip('<>') guild = guild.strip('<>')
@ -79,4 +119,7 @@ if __name__ == "__main__":
loglines = follow(logfile) loglines = follow(logfile)
for line in loglines: for line in loglines:
message = parse_string(line) message = parse_string(line)
print(message) log.debug(message)
if message and message.channel is Channel.WHISPER:
log.info('TRADE')