summaryrefslogtreecommitdiffstats
path: root/run.py
blob: 0583ed97c7ace44a940d03ae9951ca9fbd6930b1 (plain)
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
#!/usr/bin/env python
from flask import Flask, send_from_directory, render_template, url_for
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_files(directory):
	files = os.listdir(directory)
	files.sort()
	return files

@app.route('/')
def index():
	networks = []
	for network_name in get_files(app.config['ZNC_LOG_DIR']):
		network_url = '{}/{}'.format(app.config['URL'], network_name)
		channels = []

		for channel_name in get_files(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]

		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_files(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_files(os.path.join(app.config['ZNC_LOG_DIR'], network_name, 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>/<log_file>')
def get_log(network_name, channel_name, log_file):
	with open(os.path.join(app.config['ZNC_LOG_DIR'], network_name, channel_name, log_file), 'rb') as file:
		log = file.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)

if __name__ == '__main__':
	app.run(port=app.config['PORT'])