|
|
@ -3,50 +3,103 @@ |
|
|
|
|
|
|
|
import sys |
|
|
|
import os |
|
|
|
import flask |
|
|
|
import re |
|
|
|
import flask |
|
|
|
from flask import request |
|
|
|
import flask_apscheduler |
|
|
|
import urllib |
|
|
|
import json |
|
|
|
from datetime import datetime |
|
|
|
from escpos.escpos import Escpos |
|
|
|
from escpos import printer |
|
|
|
|
|
|
|
# Create the application. |
|
|
|
APP = flask.Flask(__name__) |
|
|
|
|
|
|
|
# File Initialization |
|
|
|
try: |
|
|
|
lp = printer.File("/dev/usb/lp0") |
|
|
|
except: |
|
|
|
lp = sys.stdout |
|
|
|
# Connect to the printer |
|
|
|
# Get the printer's USB initializing code with $ sudo lsbusb |
|
|
|
lp = printer.Usb(0x4b8,0xe15) |
|
|
|
|
|
|
|
# USB initialization |
|
|
|
# Bus 001 Device 006: ID 04b8:0e15 Seiko Epson Corp. |
|
|
|
# lp = printer.Usb(0x4b8, 0xe15) |
|
|
|
# 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)) |
|
|
|
|
|
|
|
lp.text('test :)') |
|
|
|
lp.image('') |
|
|
|
lp.cut() |
|
|
|
# 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): |
|
|
|
items = json.loads(open(db).read()) |
|
|
|
if not item in items['printed']: |
|
|
|
with open(db, 'w') as out: |
|
|
|
items['printed'].append(item) |
|
|
|
out.write(json.dumps(items, indent=4)) |
|
|
|
out.close() |
|
|
|
|
|
|
|
@APP.route('/', methods=['GET', 'POST']) |
|
|
|
def index(): |
|
|
|
def html2plain(html): |
|
|
|
plain = re.sub('<[^<]+?>', '', text) |
|
|
|
return plain |
|
|
|
|
|
|
|
plaintext = request.args.get('plaintext', '') |
|
|
|
@scheduler.task('interval', id='check', minutes=10) |
|
|
|
def check(): |
|
|
|
print('Checking the Multifeeder!') |
|
|
|
|
|
|
|
url = 'https://multi.vvvvvvaria.org/API/latest/10' |
|
|
|
response = urllib.request.urlopen(url).read() |
|
|
|
feed = json.loads(response) |
|
|
|
for post in feed: |
|
|
|
year = post['published_parsed'][0] |
|
|
|
month = post['published_parsed'][1] |
|
|
|
day = post['published_parsed'][2] |
|
|
|
post_date = date(year, month, day) |
|
|
|
|
|
|
|
plaintext = html2plain(post.summary) |
|
|
|
|
|
|
|
item = { |
|
|
|
'source' : post.feed_details.rss, |
|
|
|
'date' : post_date, |
|
|
|
'printed' : plaintext |
|
|
|
} |
|
|
|
update_db(item) |
|
|
|
|
|
|
|
@APP.route('/print/', methods=['GET', 'POST']) |
|
|
|
def print(): |
|
|
|
|
|
|
|
buffer = printer.Dummy() |
|
|
|
plaintext = request.args.get('printing', '') |
|
|
|
|
|
|
|
if plaintext: |
|
|
|
lp.text(plaintext) |
|
|
|
lp.cut() |
|
|
|
|
|
|
|
url = 'https://multi.vvvvvvaria.org/API/latest/5' |
|
|
|
response = urllib.request.urlopen(url).read() |
|
|
|
multifeeder = json.loads(response) |
|
|
|
buffer.text(plaintext) |
|
|
|
buffer.cut() |
|
|
|
|
|
|
|
url = 'https://multi.vvvvvvaria.org/API/today/' |
|
|
|
response = urllib.request.urlopen(url).read() |
|
|
|
feedstoday = json.loads(response) |
|
|
|
lp._raw(buffer.output) |
|
|
|
|
|
|
|
date = datetime.now() |
|
|
|
date_str = date.strftime('%Y-%m-%d_%H-%M-%S') |
|
|
|
|
|
|
|
item = { |
|
|
|
'source' : 'TeleVex', |
|
|
|
'date' : date_str, |
|
|
|
'printed' : plaintext |
|
|
|
} |
|
|
|
update_db(item) |
|
|
|
|
|
|
|
return flask.render_template('index.html', plaintext=plaintext) |
|
|
|
|
|
|
|
@APP.route('/printed/', methods=['GET', 'POST']) |
|
|
|
def printed(): |
|
|
|
|
|
|
|
items = json.loads(open(db).read()) |
|
|
|
|
|
|
|
return flask.render_template('index.html', plaintext=plaintext, multifeeder=multifeeder, feedstoday=feedstoday) |
|
|
|
return flask.render_template('printed.html', items=items) |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
APP.debug=True |
|
|
|
APP.run(port=5000) |
|
|
|
APP.run(port=5000) |
|
|
|