televex/print/start.py

76 lines
2.1 KiB
Python

# 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__)
# Set up a JSON database for storing printed items
db_path = 'printed.json'
db = televex.database(db_path)
# Connect to the printer
lp = televex.connect()
# 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/3'
response = urllib.request.urlopen(url).read()
feed = json.loads(response)
except:
feed = []
for post in feed:
source = post['feed_details']['rss']
# only print toots for now (this is a test)
if 'post.lurk.org' in source:
txt, date = televex.preprocess(post, type='rss')
already_in_db = televex.update_db(db, db_path, txt, source, date)
if already_in_db == False:
televex.print_now(txt, lp=lp, type='rss')
@APP.route('/print/', methods=['GET', 'POST'])
def print():
txt = request.args.get('printing', '')
img = request.files.get('img')
if txt:
source = 'TeleVex'
txt, date = televex.preprocess(txt, type='txt')
televex.update_db(db, db_path, txt, source, date)
televex.print_now(txt, lp=lp, type='txt')
return flask.redirect(flask.url_for('print'))
elif img:
source = 'TeleVex'
img, date = televex.preprocess(img, type='img')
televex.update_db(db, db_path, 'IMAGE', source, date)
televex.print_now(img, lp=lp, type='img')
return flask.redirect(flask.url_for('print'))
return flask.render_template('print.html')
@APP.route('/printed/', methods=['GET'])
def printed():
db = televex.database(db_path)
return flask.render_template('printed.html', items=db)
if __name__ == '__main__':
APP.debug=True
APP.run(port=5000)