Experimental communication tools (televex screen + televex print) https://televex.vvvvvvaria.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

131 lines
2.8 KiB

# https://github.com/python-escpos/python-escpos
# https://python-escpos.readthedocs.io/en/latest/
import sys
import os
import re
import flask
from flask import request
import flask_apscheduler
import urllib
import json
from datetime import date, datetime
from escpos.escpos import Escpos
from escpos import printer
# Create the application.
APP = flask.Flask(__name__)
# Connect to the printer
# Get the printer's USB initializing code with $ sudo lsbusb
lp = printer.Usb(0x4b8,0xe15)
buffer = printer.Dummy()
# Define the JSON databased for storing printed items
db = 'printed.json'
if not os.path.exists(db):
with open(db, 'w') as f:
f.write(json.dumps({ "printed" : [] }, indent=4))
# 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()
def update_db(item):
try:
items = json.loads(open(db).read())
except:
# Hmm ...
items = { "printed" : [] }
if item not in items['printed']:
new_item = True
with open(db, 'w') as out:
items['printed'].append(item)
out.write(json.dumps(items, indent=4))
out.close()
else:
new_item = False
return new_item
def html2plain(html):
plaintext = re.sub('<[^<]+?>', '', html)
return plaintext
@scheduler.task('interval', id='check', minutes=10)
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:
# add post to database
year = post['published_parsed'][0]
month = post['published_parsed'][1]
day = post['published_parsed'][2]
post_date = date(year, month, day)
post_date_str = post_date.strftime('%Y-%m-%d')
plaintext = html2plain(post['summary'])
item = {
'source' : post['feed_details']['rss'],
'date' : post_date_str,
'printed' : plaintext
}
new_item = update_db(item)
# add RSS post to printing buffer
if new_item == True:
buffer.text(plaintext)
buffer.cut()
break
# print
lp._raw(buffer.output)
@APP.route('/print/', methods=['GET', 'POST'])
def print():
plaintext = request.args.get('printing', '')
if plaintext:
# add plaintext to printing buffer
buffer.text(plaintext)
buffer.cut()
# add plaintext to database
date = datetime.now()
date_str = date.strftime('%Y-%m-%d_%H-%M-%S')
item = {
'source' : 'TeleVex',
'date' : date_str,
'printed' : plaintext
}
update_db(item)
# print
lp._raw(buffer.output)
return flask.render_template('index.html')
@APP.route('/printed/', methods=['GET', 'POST'])
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)