Added factory function for Message
This commit is contained in:
parent
3c7c5c5f5d
commit
381dceb80a
21
src/data.py
21
src/data.py
@ -1,6 +1,7 @@
|
||||
from enum import Enum
|
||||
import re
|
||||
import datetime
|
||||
import logging
|
||||
|
||||
re_trade = re.compile(
|
||||
r'Hi, I would like to buy your (?P<item>.+) listed for (?P<amount>\d+) (?P<currency>\S+) in (?P<league>\S+) '
|
||||
@ -10,6 +11,8 @@ re_log = re.compile(
|
||||
r'(?P<date>\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d) (\d+) (\S+) \[(?P<level>\S+) (\S+) (\d+)\] '
|
||||
r'(?P<channel>[#@%$&]?)(?P<ToFrom>To|From)?\s?(?P<guild><\S+>)? ?(?P<user>[^:]+): (?P<message>.*)'
|
||||
)
|
||||
log = logging.getLogger(__name__)
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
|
||||
def compile_regex(conf: dict):
|
||||
@ -74,6 +77,24 @@ class Message():
|
||||
if self.channel is Channel.WHISPER:
|
||||
self.parse_trade()
|
||||
|
||||
@classmethod
|
||||
def from_text(cls, text: str):
|
||||
result = re_log.search(text)
|
||||
if not result:
|
||||
log.debug(f'Result is none for text "{text}"')
|
||||
return None
|
||||
date = datetime.datetime.strptime(
|
||||
result.group('date'), '%Y/%m/%d %H:%M:%S')
|
||||
guild = result.group('guild')
|
||||
if guild:
|
||||
guild = guild.strip('<>')
|
||||
return cls(result.group('message'),
|
||||
date,
|
||||
result.group('user'),
|
||||
channel_mapping[result.group('channel')],
|
||||
guild,
|
||||
result.group('ToFrom'))
|
||||
|
||||
def __str__(self) -> str:
|
||||
text = f'{self.date} - {self.channel.name}: '
|
||||
if self.to_from:
|
||||
|
@ -1,36 +1,17 @@
|
||||
import time
|
||||
import datetime
|
||||
import logging
|
||||
from .data import Message, channel_mapping, re_log
|
||||
from .data import Message
|
||||
from . import gui
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
|
||||
def parse_log(text: str) -> Message:
|
||||
result = re_log.search(text)
|
||||
if not result:
|
||||
log.debug(f'Result is none for text "{text}"')
|
||||
return None
|
||||
date = datetime.datetime.strptime(
|
||||
result.group('date'), '%Y/%m/%d %H:%M:%S')
|
||||
guild = result.group('guild')
|
||||
if guild:
|
||||
guild = guild.strip('<>')
|
||||
return Message(result.group('message'),
|
||||
date,
|
||||
result.group('user'),
|
||||
channel_mapping[result.group('channel')],
|
||||
guild,
|
||||
result.group('ToFrom'))
|
||||
|
||||
|
||||
def read_log(logfile: str, app: gui.Gui) -> None:
|
||||
logfile = open(logfile, 'r', encoding='utf8')
|
||||
loglines = follow(logfile)
|
||||
for line in loglines:
|
||||
message = parse_log(line)
|
||||
message = Message.from_text(line)
|
||||
log.debug(message)
|
||||
if message and message.trade and message.to_from == 'From':
|
||||
app.add_tab(30, message)
|
||||
|
Loading…
x
Reference in New Issue
Block a user