Browse Source

syncing the televex pi with the git

master
televex 3 years ago
parent
commit
d4037120c7
  1. 47
      print/start.py
  2. 34
      print/templates/printed.html

47
print/start.py

@ -9,7 +9,7 @@ from flask import request
import flask_apscheduler
import urllib
import json
from datetime import datetime
from datetime import date, datetime
from escpos.escpos import Escpos
from escpos import printer
@ -19,6 +19,7 @@ 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'
@ -35,8 +36,14 @@ scheduler.init_app(APP)
scheduler.start()
def update_db(item):
try:
items = json.loads(open(db).read())
if not item in items['printed']:
except:
# Hmm ...
items = { "printed" : [] }
if item not in items['printed']:
new_item = True
with open(db, 'w') as out:
items['printed'].append(item)
@ -44,51 +51,62 @@ def update_db(item):
out.close()
else:
new_item = False
return new_item
def html2plain(html):
plain = re.sub('<[^<]+?>', '', text)
return plain
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)
plaintext = html2plain(post.summary)
post_date_str = post_date.strftime('%Y-%m-%d')
plaintext = html2plain(post['summary'])
item = {
'source' : post.feed_details.rss,
'date' : post_date,
'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:
# print RSS post
buffer.text(plaintext)
buffer.cut()
break
# print
lp._raw(buffer.output)
@APP.route('/print/', methods=['GET', 'POST'])
def print():
buffer = printer.Dummy()
plaintext = request.args.get('printing', '')
if plaintext:
# add plaintext to printing buffer
buffer.text(plaintext)
buffer.cut()
lp._raw(buffer.output)
# add plaintext to database
date = datetime.now()
date_str = date.strftime('%Y-%m-%d_%H-%M-%S')
item = {
@ -98,7 +116,10 @@ def print():
}
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'])
def printed():

34
print/templates/printed.html

@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset="utf-8" />
<title>TeleVex (print)</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='stylesheet.css')}}">
</head>
<body>
<div id="main">
<h1><a href="/print/">print</a>, printed</h1>
<div id="multifeeder">
<h2>TeleVex recently printed:</h2>
<div>(last 25)</div>
<br>
{% set count = 25 %}
{% for x in range(1, 25) %}
{% set item = items["printed"][-x] %}
{% if item %}
<div class="msg">
<strong>Source</strong>: {{ item.source }}<br>
<strong>Date</strong>: {{ item.date }}<br>
<strong>Printed</strong>: {{ item.printed }}<br>
<br>
</div>
{% endif %}
{% set count = count - 1 %}
{% endfor %}
</div>
</div>
</body>
</html>
Loading…
Cancel
Save