#!/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('/') 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('//') 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('///') 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/') 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'])