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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#!/usr/bin/env python
from flask import Flask, send_from_directory, render_template, session, abort
from classes import Network, Channel, Log
from urllib.parse import quote_plus
import sys, os
app = Flask(__name__)
app.config.from_pyfile('application.cfg', silent=True)
app.jinja_env.add_extension('jinja2_highlight.HighlightExtension')
app.jinja_env.extend(jinja2_highlight_cssclass = 'codehilite')
def get_directories(directory):
dirents = os.scandir(directory)
directories = filter(lambda de: de.is_dir(), dirents)
return map(lambda d: d.name.split('/')[-1], directories)
def get_channels(directory, network):
path = os.path.join(directory, network)
return get_directories(path)
def get_logs(base_dir, network, channel):
dirents = os.scandir(os.path.join(base_dir, network, channel))
files = filter(lambda de: de.is_file(), dirents)
log_names = map(lambda f: f.name.replace('{}.'.format(channel), '').replace('.log', ''), files)
return log_names
@app.route('/')
def index():
networks = []
for network_name in get_directories(app.config['ZNC_LOG_DIR']):
network_url = '{}/{}'.format(app.config['URL'], network_name)
channels = []
for channel_name in get_channels(app.config['ZNC_LOG_DIR'], network_name):
channel_url = '{}/{}'.format(network_url, quote_plus(channel_name))
channel = Channel(channel_name, channel_url, '')
channels += [channel]
networks += [Network(network_name, network_url, channels)]
return(render_template('list_networks.html', networks=networks, url=app.config['URL']))
@app.route('/<network_name>')
def get_network(network_name):
network_url = '{}/{}'.format(app.config['URL'], network_name)
channels = []
for channel_name in get_directories(os.path.join(app.config['ZNC_LOG_DIR'], network_name)):
channel_url = '{}/{}'.format(network_url, quote_plus(channel_name))
channel = Channel(channel_name, channel_url, '')
channels += [channel]
network = Network(network_name, network_url, channels)
return(render_template('show_network.html', network=network, url=app.config['URL']))
@app.route('/<network_name>/<channel_name>')
def channel_logs(network_name, channel_name):
network_url = '{}/{}'.format(app.config['URL'], network_name)
channel_url = '{}/{}'.format(network_url, quote_plus(channel_name))
logs = []
for log_file in get_logs(app.config['ZNC_LOG_DIR'], network=network_name, channel=channel_name):
log_url = '{}/{}'.format(channel_url, log_file)
log = Log(log_file, log_url)
logs += [log]
channel = Channel(channel_name, channel_url, logs)
network = Network(network_name, network_url, [channel])
return(render_template('show_channel.html', network=network, channel=channel, url=app.config['URL']))
@app.route('/<network_name>/<channel_name>/<date>')
def get_log(network_name, channel_name, date):
file_name = '{}.{}.log'.format(channel_name, date)
path = os.path.join(app.config['ZNC_LOG_DIR'], network_name, channel_name, file_name)
with open(path, 'rb') as fp:
log = fp.read().decode('utf-8', 'ignore')
return(render_template('log.html', log=log))
@app.route('/static/<path:filename>')
def send_static(filename):
return send_from_directory('static', filename)
@app.route('/favicon.ico')
def favicon():
return abort(404)
if __name__ == '__main__':
app.run(port=app.config['PORT'])
|