import flask from flask import request, redirect from urllib.request import urlopen from urllib.parse import urlencode import json import os import pypandoc from jinja2 import Template APP = flask.Flask(__name__) APP.config.from_object("config.Config") # --- def get_pad_content(pad): arguments = { 'padID' : pad, 'apikey' : APP.config['PAD_API_KEY'] } api_call = 'getText' response = json.load(urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode())) content = response['data']['text'] return content def update_pad_contents(): # download the stylesheet + template pad css = get_pad_content('feedmode.css') template = get_pad_content('feedmode.template') # !!! this breaks the whole idea that this application can be shared by multiple projects at the same time # !!! but py_pandoc needs to run with files........ hmmm with open('templates/pandoc-template.html', 'w') as f: f.write(template) return css, template def get_multifeeder_feed(query): feed_json = json.load(urlopen(query)) multifeeder_template = open('templates/multifeeder-template.html').read() jinja_template = Template(multifeeder_template) feed_html = jinja_template.render(response=feed_json) return feed_html, feed_json def load_query(): query = open('static/query.txt').read().strip() # !!! needs fixing return query # --- @APP.route('/', methods=['GET']) def index(): return redirect("/preview/", code=302) @APP.route('/update/', methods=['POST']) def update(): query = request.form['query'] with open('static/query.txt', 'w') as out: out.write(query) return redirect("/select/", code=302) @APP.route('/select/', methods=['GET']) def select(): # get feed contents # render selection template with checkboxes query = load_query() x, feed_json = get_multifeeder_feed(query) return flask.render_template('selection-template.html', feed=feed_json, query=query) @APP.route('/preview/', methods=['GET']) def preview(): # update pad contents x, template_content = update_pad_contents() # get feed contents query = load_query() feed_html, x = get_multifeeder_feed(query) # render multifeeder feed in template template = Template(template_content) html = template.render(feed=feed_html, mode="screen") return flask.render_template('html.html', html=html, query=query) @APP.route('/stylesheet/', methods=['GET']) def stylesheet(): ext = '.css' query = load_query() return flask.render_template('pad.html', name="feedmode", ext=ext, query=query) @APP.route('/template/', methods=['GET']) def template(): ext = '.template' query = load_query() return flask.render_template('pad.html', name="feedmode", ext=ext, query=query) @APP.route('/pdf/') def pdf(): query = load_query() return flask.render_template('pdf.html', query=query) # ////////////// # rendered resources (not saved as a file on the server) @APP.route('/print.css') def css(): css, x = update_pad_contents() return css, 200, {'Content-Type': 'text/css; charset=utf-8'} @APP.route('/pandoc-template.html') def pandoc_template(): x, template = update_pad_contents() return template, 200, {'Content-Type': 'text/html; charset=utf-8'} @APP.route('/pagedjs.html') def pagedjs(): # update pad contents x, template = update_pad_contents() # get feed contents query = "https://multi.vvvvvvaria.org/API/latest/50" # !!! needs fixing feed_html, feed_json = get_multifeeder_feed(query) # render multifeeder feed in template jinja_template = Template(template) html = jinja_template.render(feed=feed_html, mode="print") return html, 200, {'Content-Type': 'text/html; charset=utf-8'} # ////////////// if __name__ == '__main__': APP.debug=True APP.run(host="0.0.0.0", port=f'{ APP.config["PORTNUMBER"] }', threaded=True)