|
@ -9,7 +9,7 @@ from flask import request |
|
|
import flask_apscheduler |
|
|
import flask_apscheduler |
|
|
import urllib |
|
|
import urllib |
|
|
import json |
|
|
import json |
|
|
from datetime import datetime |
|
|
from datetime import date, datetime |
|
|
from escpos.escpos import Escpos |
|
|
from escpos.escpos import Escpos |
|
|
from escpos import printer |
|
|
from escpos import printer |
|
|
|
|
|
|
|
@ -19,6 +19,7 @@ APP = flask.Flask(__name__) |
|
|
# Connect to the printer |
|
|
# Connect to the printer |
|
|
# Get the printer's USB initializing code with $ sudo lsbusb |
|
|
# Get the printer's USB initializing code with $ sudo lsbusb |
|
|
lp = printer.Usb(0x4b8,0xe15) |
|
|
lp = printer.Usb(0x4b8,0xe15) |
|
|
|
|
|
buffer = printer.Dummy() |
|
|
|
|
|
|
|
|
# Define the JSON databased for storing printed items |
|
|
# Define the JSON databased for storing printed items |
|
|
db = 'printed.json' |
|
|
db = 'printed.json' |
|
@ -35,8 +36,14 @@ scheduler.init_app(APP) |
|
|
scheduler.start() |
|
|
scheduler.start() |
|
|
|
|
|
|
|
|
def update_db(item): |
|
|
def update_db(item): |
|
|
items = json.loads(open(db).read()) |
|
|
|
|
|
if not item in items['printed']: |
|
|
try: |
|
|
|
|
|
items = json.loads(open(db).read()) |
|
|
|
|
|
except: |
|
|
|
|
|
# Hmm ... |
|
|
|
|
|
items = { "printed" : [] } |
|
|
|
|
|
|
|
|
|
|
|
if item not in items['printed']: |
|
|
new_item = True |
|
|
new_item = True |
|
|
with open(db, 'w') as out: |
|
|
with open(db, 'w') as out: |
|
|
items['printed'].append(item) |
|
|
items['printed'].append(item) |
|
@ -44,51 +51,62 @@ def update_db(item): |
|
|
out.close() |
|
|
out.close() |
|
|
else: |
|
|
else: |
|
|
new_item = False |
|
|
new_item = False |
|
|
|
|
|
|
|
|
return new_item |
|
|
return new_item |
|
|
|
|
|
|
|
|
def html2plain(html): |
|
|
def html2plain(html): |
|
|
plain = re.sub('<[^<]+?>', '', text) |
|
|
plaintext = re.sub('<[^<]+?>', '', html) |
|
|
return plain |
|
|
return plaintext |
|
|
|
|
|
|
|
|
@scheduler.task('interval', id='check', minutes=10) |
|
|
@scheduler.task('interval', id='check', minutes=10) |
|
|
def check(): |
|
|
def check(): |
|
|
url = 'https://multi.vvvvvvaria.org/API/latest/10' |
|
|
|
|
|
response = urllib.request.urlopen(url).read() |
|
|
try: |
|
|
feed = json.loads(response) |
|
|
url = 'https://multi.vvvvvvaria.org/API/latest/10' |
|
|
|
|
|
response = urllib.request.urlopen(url).read() |
|
|
|
|
|
feed = json.loads(response) |
|
|
|
|
|
except: |
|
|
|
|
|
feed = [] |
|
|
|
|
|
|
|
|
for post in feed: |
|
|
for post in feed: |
|
|
|
|
|
|
|
|
|
|
|
# add post to database |
|
|
year = post['published_parsed'][0] |
|
|
year = post['published_parsed'][0] |
|
|
month = post['published_parsed'][1] |
|
|
month = post['published_parsed'][1] |
|
|
day = post['published_parsed'][2] |
|
|
day = post['published_parsed'][2] |
|
|
post_date = date(year, month, day) |
|
|
post_date = date(year, month, day) |
|
|
|
|
|
post_date_str = post_date.strftime('%Y-%m-%d') |
|
|
plaintext = html2plain(post.summary) |
|
|
plaintext = html2plain(post['summary']) |
|
|
|
|
|
|
|
|
item = { |
|
|
item = { |
|
|
'source' : post.feed_details.rss, |
|
|
'source' : post['feed_details']['rss'], |
|
|
'date' : post_date, |
|
|
'date' : post_date_str, |
|
|
'printed' : plaintext |
|
|
'printed' : plaintext |
|
|
} |
|
|
} |
|
|
new_item = update_db(item) |
|
|
new_item = update_db(item) |
|
|
|
|
|
|
|
|
|
|
|
# add RSS post to printing buffer |
|
|
if new_item == True: |
|
|
if new_item == True: |
|
|
# print RSS post |
|
|
|
|
|
buffer.text(plaintext) |
|
|
buffer.text(plaintext) |
|
|
buffer.cut() |
|
|
buffer.cut() |
|
|
lp._raw(buffer.output) |
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
# print |
|
|
|
|
|
lp._raw(buffer.output) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route('/print/', methods=['GET', 'POST']) |
|
|
@APP.route('/print/', methods=['GET', 'POST']) |
|
|
def print(): |
|
|
def print(): |
|
|
|
|
|
|
|
|
buffer = printer.Dummy() |
|
|
|
|
|
plaintext = request.args.get('printing', '') |
|
|
plaintext = request.args.get('printing', '') |
|
|
|
|
|
|
|
|
if plaintext: |
|
|
if plaintext: |
|
|
|
|
|
|
|
|
|
|
|
# add plaintext to printing buffer |
|
|
buffer.text(plaintext) |
|
|
buffer.text(plaintext) |
|
|
buffer.cut() |
|
|
buffer.cut() |
|
|
|
|
|
|
|
|
lp._raw(buffer.output) |
|
|
# add plaintext to database |
|
|
|
|
|
|
|
|
date = datetime.now() |
|
|
date = datetime.now() |
|
|
date_str = date.strftime('%Y-%m-%d_%H-%M-%S') |
|
|
date_str = date.strftime('%Y-%m-%d_%H-%M-%S') |
|
|
item = { |
|
|
item = { |
|
@ -98,7 +116,10 @@ def print(): |
|
|
} |
|
|
} |
|
|
update_db(item) |
|
|
update_db(item) |
|
|
|
|
|
|
|
|
return flask.render_template('index.html', plaintext=plaintext) |
|
|
# print |
|
|
|
|
|
lp._raw(buffer.output) |
|
|
|
|
|
|
|
|
|
|
|
return flask.render_template('index.html') |
|
|
|
|
|
|
|
|
@APP.route('/printed/', methods=['GET', 'POST']) |
|
|
@APP.route('/printed/', methods=['GET', 'POST']) |
|
|
def printed(): |
|
|
def printed(): |
|
|