Restructered and thread for reader
This commit is contained in:
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
74
src/data.py
Normal file
74
src/data.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from enum import Enum
|
||||
import re
|
||||
import datetime
|
||||
|
||||
re_trade = re.compile(
|
||||
'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+)\)')
|
||||
|
||||
|
||||
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 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():
|
||||
def __init__(self,
|
||||
message: str,
|
||||
date: datetime.datetime,
|
||||
user: str,
|
||||
channel: Channel,
|
||||
guild: str) -> None:
|
||||
self.message = message
|
||||
self.date = date
|
||||
self.channel = channel
|
||||
if channel is Channel.WHISPER:
|
||||
user = user.lstrip('From ')
|
||||
self.user = user
|
||||
self.guild = guild
|
||||
|
||||
def __str__(self) -> str:
|
||||
text = f'{self.date} - {self.channel.name}: '
|
||||
if self.guild:
|
||||
text = text + f'<{self.guild}> '
|
||||
text = text + f'{self.user}: {self.message}'
|
||||
return text
|
||||
|
||||
def parse_trade(self) -> Trade:
|
||||
res = re_trade.search(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'])
|
||||
29
src/gui.py
Normal file
29
src/gui.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from tkinter import ttk
|
||||
from tkinter import Tk
|
||||
from tkinter import Button
|
||||
from .data import Trade
|
||||
|
||||
class Gui(Tk):
|
||||
def __init__(self, parent=None) -> None:
|
||||
Tk.__init__(self, parent)
|
||||
self.title("Welcome to LikeGeeks app")
|
||||
self.geometry("300x200+10+20")
|
||||
self.overrideredirect(1) # remove border
|
||||
# window.withdraw() # hide window
|
||||
self.attributes('-topmost', True) # always on top
|
||||
self.tab_control = ttk.Notebook(self)
|
||||
self.tab_control.pack(expand=1, fill='both')
|
||||
self.add_tab(1, None)
|
||||
self.add_tab(2, None)
|
||||
self.add_tab(3, None)
|
||||
|
||||
def add_tab(self, number: int, trade: Trade) -> None:
|
||||
tab = ttk.Frame(self.tab_control)
|
||||
Button(tab, text='Accept').pack()
|
||||
Button(tab, text='Decline').pack()
|
||||
self.tab_control.add(tab, text=f'{number}')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = Gui()
|
||||
app.mainloop()
|
||||
48
src/trader.py
Normal file
48
src/trader.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import time
|
||||
import datetime
|
||||
import re
|
||||
import logging
|
||||
from .data import Message, Channel, channel_mapping
|
||||
from . import gui
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
|
||||
re_log = re.compile(
|
||||
'(?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>.*)')
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
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)
|
||||
log.debug(message)
|
||||
if message and message.channel is Channel.WHISPER:
|
||||
log.info('TRADE')
|
||||
trade = message.parse_trade()
|
||||
app.add_tab(30, trade)
|
||||
|
||||
|
||||
def follow(thefile: str):
|
||||
thefile.seek(0, 2)
|
||||
while True:
|
||||
line = thefile.readline()
|
||||
if not line:
|
||||
time.sleep(0.1)
|
||||
continue
|
||||
yield line
|
||||
Reference in New Issue
Block a user