-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
65 lines (52 loc) · 1.64 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from collections import namedtuple
import os
from pathlib import Path
from flask import abort, Flask, render_template
from flask_common import Common
app = Flask(__name__)
common = Common(app)
LOGDIR = Path.home()/'.weechat/logs' # WIP: Make configurable.
p = Path(LOGDIR)
Message = namedtuple('Message', ['timestamp', 'author', 'text'])
chart = {}
num = 0
class Chat(object):
def __init__(self, filepath, slug):
self.filepath = filepath
self.slug = slug
@property
def messages(self):
msgs = []
with open(self.filepath) as f:
lines = f.readlines()
for line in lines:
broken_message = line.split()
author = broken_message[2]
if author in ('<--', '-->', '--', '=!=', '***'):
continue
timestamp = ' '.join(broken_message[:2])
text = ' '.join(broken_message[3:])
msgs.append(Message(timestamp, author, text))
return msgs
@property
def title(self):
return self.filepath.stem
@app.route('/')
def index():
return render_template('index.html', chats=list(chart.values()))
@app.route('/chat/<slug>')
def show_chat(slug):
try:
chat = chart[int(slug)]
return render_template('chat.html', chat=chat)
except (KeyError, ValueError):
return 'Oops, that chat does not exist! :-)'
except IOError:
abort(404)
if __name__ == '__main__':
for f in sorted(list(p.iterdir()), key=os.path.getctime):
if f.suffix == '.weechatlog':
chart[num] = Chat(f.absolute(), slug=num)
num += 1
app.debug = True
common.serve()