|
|
|
# https://github.com/python-escpos/python-escpos
|
|
|
|
# https://python-escpos.readthedocs.io/en/latest/
|
|
|
|
|
|
|
|
import flask
|
|
|
|
from flask import request
|
|
|
|
import flask_apscheduler
|
|
|
|
import urllib, json
|
|
|
|
from escpos import printer
|
|
|
|
from tools.televex import televex
|
|
|
|
|
|
|
|
# Create the application.
|
|
|
|
APP = flask.Flask(__name__)
|
|
|
|
|
|
|
|
# Connect to the printer
|
|
|
|
lp, lp_printer = televex.connect()
|
|
|
|
|
|
|
|
# Set up a JSON database for storing printed items
|
|
|
|
db = televex.database('printed.json')
|
|
|
|
|
|
|
|
# Check the Multifeeder regulary (every 10 min)
|
|
|
|
# https://github.com/viniciuschiele/flask-apscheduler
|
|
|
|
# Thanks Crunk for pointing to this extention!
|
|
|
|
scheduler = flask_apscheduler.APScheduler()
|
|
|
|
scheduler.api_enabled = False
|
|
|
|
scheduler.init_app(APP)
|
|
|
|
scheduler.start()
|
|
|
|
|
|
|
|
@scheduler.task('interval', id='check', minutes=1)
|
|
|
|
def check():
|
|
|
|
try:
|
|
|
|
url = 'https://multi.vvvvvvaria.org/API/latest/10'
|
|
|
|
response = urllib.request.urlopen(url).read()
|
|
|
|
feed = json.loads(response)
|
|
|
|
except:
|
|
|
|
feed = []
|
|
|
|
|
|
|
|
for post in feed:
|
|
|
|
source = post['feed_details']['rss']
|
|
|
|
txt = post['summary']
|
|
|
|
televex.print_now(txt, source, db, post=post, lp=lp, lp_printer=lp_printer)
|
|
|
|
|
|
|
|
@APP.route('/print/', methods=['GET', 'POST'])
|
|
|
|
def print():
|
|
|
|
txt = request.args.get('printing', '')
|
|
|
|
|
|
|
|
if txt:
|
|
|
|
source = 'TeleVex'
|
|
|
|
televex.print_now(txt, source, db, lp=lp, lp_printer=lp_printer)
|
|
|
|
|
|
|
|
return flask.redirect(flask.url_for('print'))
|
|
|
|
|
|
|
|
return flask.render_template('index.html')
|
|
|
|
|
|
|
|
@APP.route('/printed/', methods=['GET'])
|
|
|
|
def printed():
|
|
|
|
items = json.loads(open(db).read())
|
|
|
|
return flask.render_template('printed.html', items=items)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
APP.debug=True
|
|
|
|
APP.run(port=5000)
|