Browse Source

updates to the print webpage, it works now! (still experimental)

master
televex 3 years ago
parent
commit
316de60697
  1. 1
      .gitignore
  2. 103
      print/start.py
  3. 27
      print/templates/index.html
  4. 1
      requirements.txt

1
.gitignore

@ -2,3 +2,4 @@
venv* venv*
*__pycache__* *__pycache__*
*.pyc *.pyc
print/printed.json

103
print/start.py

@ -3,50 +3,103 @@
import sys import sys
import os import os
import flask import re
import flask
from flask import request from flask import request
import flask_apscheduler
import urllib import urllib
import json import json
from datetime import datetime
from escpos.escpos import Escpos from escpos.escpos import Escpos
from escpos import printer from escpos import printer
# Create the application. # Create the application.
APP = flask.Flask(__name__) APP = flask.Flask(__name__)
# File Initialization # Connect to the printer
try: # Get the printer's USB initializing code with $ sudo lsbusb
lp = printer.File("/dev/usb/lp0") lp = printer.Usb(0x4b8,0xe15)
except:
lp = sys.stdout
# USB initialization # Define the JSON databased for storing printed items
# Bus 001 Device 006: ID 04b8:0e15 Seiko Epson Corp. db = 'printed.json'
# lp = printer.Usb(0x4b8, 0xe15) if not os.path.exists(db):
with open(db, 'w') as f:
f.write(json.dumps({ "printed" : [] }, indent=4))
lp.text('test :)') # Check the Multifeeder regulary (every 10 min)
lp.image('') # https://github.com/viniciuschiele/flask-apscheduler
lp.cut() # 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 html2plain(html):
def index(): 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: if plaintext:
lp.text(plaintext)
lp.cut()
url = 'https://multi.vvvvvvaria.org/API/latest/5' buffer.text(plaintext)
response = urllib.request.urlopen(url).read() buffer.cut()
multifeeder = json.loads(response)
url = 'https://multi.vvvvvvaria.org/API/today/' lp._raw(buffer.output)
response = urllib.request.urlopen(url).read()
feedstoday = json.loads(response) 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__': if __name__ == '__main__':
APP.debug=True APP.debug=True
APP.run(port=5000) APP.run(port=5000)

27
print/templates/index.html

@ -8,36 +8,25 @@
<body> <body>
<div id="main"> <div id="main">
<h1>print, <a href="/printed/">printed</a></h1>
<div id="print"> <div id="print">
<h1>Plain Text Printing Point (ASCII only)</h1> <h2>Plain Text Printing Point (ASCII only)</h2>
<form action="" method="GET"> <form action="" method="GET">
<textarea name="plaintext" cols="42" rows="30">{{ plaintext }}</textarea> <textarea name="printing" cols="42" rows="30">{{ plaintext }}</textarea>
<br> <br>
<br> <br>
<input class="submit" type="submit" value="print"/> <input class="submit" type="submit" value="print"/>
</form> </form>
<br> <br>
<h1>Image printing access point</h1>
<h2>Image printing access point (not working yet)</h2>
<form action="" method="GET"> <form action="" method="GET">
<button>Select image</button> <button>Select image</button>
<input class="submit" type="submit" value="print image"/> <input class="submit" type="submit" value="print image"/>
</form> </form>
</div> </div>
<br>
<div id="multifeeder">
<h1 id="title">Multifeeder</h1>
<div>Last 5 posts added to the Televex print stream:</div>
<br>
{% for post in multifeeder %}
<div class="multipost">
<strong>Title</strong>: {{ post.title }}<br>
<strong>Link</strong>: {{ post.link }}<br>
<strong>Author</strong>: {{ post.author }}<br>
<strong>Date</strong>: {{ post.published }}<br>
</div>
{% endfor %}
</div>
</div> </div>
</body> </body>
</html> </html>

1
requirements.txt

@ -4,3 +4,4 @@ requests
pypandoc pypandoc
python-escpos python-escpos
flask flask
Flask-APScheduler

Loading…
Cancel
Save