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.
95 lines
2.2 KiB
95 lines
2.2 KiB
3 years ago
|
import sys, os, re, json
|
||
|
from datetime import datetime, date
|
||
|
import html2text
|
||
|
from escpos import printer
|
||
|
|
||
|
from tools.asciiWriter.text import make_column
|
||
|
from tools.asciiWriter.utils import merge, print_lines, make_lines, translate
|
||
|
|
||
|
def connect():
|
||
|
# Get the printer's USB initializing code with $ sudo lsbusb
|
||
|
try:
|
||
|
lp = printer.Usb(0x4b8,0xe15)
|
||
|
lp_printer = True
|
||
|
except:
|
||
|
lp = None
|
||
|
lp_printer = False
|
||
|
|
||
|
return lp, lp_printer
|
||
|
|
||
|
def database(db):
|
||
|
if not os.path.exists(db):
|
||
|
with open(db, 'w') as f:
|
||
|
f.write(json.dumps({ "printed" : [] }, indent=4))
|
||
|
|
||
|
return db
|
||
|
|
||
|
def html2plain(html):
|
||
|
# remove HTML tags
|
||
|
txt = re.sub(r'<.*?>', '', html)
|
||
|
|
||
|
# make line breaks
|
||
|
# https://git.vvvvvvaria.org/mb/ascii-art-but-with-unicode
|
||
|
width = 48
|
||
|
layers = []
|
||
|
lines, remaining = make_column(txt, line_width=width)
|
||
|
layers.append(lines)
|
||
|
merged = merge(width, len(lines), ' ', layers)
|
||
|
txt = ''
|
||
|
for line in merged:
|
||
|
txt += '{}\n'.format(''.join(line))
|
||
|
|
||
|
return txt
|
||
|
|
||
|
def update_db(txt, source, db, post=None):
|
||
|
try:
|
||
|
items = json.loads(open(db).read())
|
||
|
except:
|
||
|
items = { "printed" : [] } # Hmm ...
|
||
|
|
||
|
# save the publishing date of this post
|
||
|
if post:
|
||
|
year = post['published_parsed'][0]
|
||
|
month = post['published_parsed'][1]
|
||
|
day = post['published_parsed'][2]
|
||
|
post_date = date(year, month, day)
|
||
|
date_str = post_date.strftime('%Y-%m-%d')
|
||
|
post = None
|
||
|
else:
|
||
|
post_date = datetime.now()
|
||
|
date_str = post_date.strftime('%Y-%m-%d_%H-%M-%S')
|
||
|
|
||
|
# clean txt from HTML tags
|
||
|
txt = html2plain(txt)
|
||
|
|
||
|
# add txt to database
|
||
|
# and check if this item is new
|
||
|
item = {
|
||
|
'source' : source,
|
||
|
'date' : date_str,
|
||
|
'printed' : txt
|
||
|
}
|
||
|
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, txt
|
||
|
|
||
|
def print_now(txt, source, db, post=None, lp=None, lp_printer=None):
|
||
|
# check if this is a new item
|
||
|
new_item, txt = update_db(txt, source, db, post=post)
|
||
|
|
||
|
# if this item is new
|
||
|
# then print!
|
||
|
if new_item == True:
|
||
|
if lp_printer == True:
|
||
|
lp.text(txt)
|
||
|
lp.cut()
|
||
|
else:
|
||
|
# or print in the terminal!
|
||
|
sys.stderr.write('Printing output to the terminal:\n\n'+txt+'\n\n')
|