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.
 
 
 
 

130 lines
3.1 KiB

import sys, os, re, json
from datetime import datetime, date
import html2text
from escpos import printer
from PIL import Image
import base64
import io
def connect():
# Get the printer's USB initializing code with $ sudo lsbusb
try:
# lp = printer.Usb(0x4b8,0xe15) # televex printer @ Varia
lp = printer.Usb(0x4b8,0xe03) # printer @ poel
except:
lp = None
return lp
def database(db_path):
if not os.path.exists(db_path):
with open(db_path, 'w') as f:
f.write(json.dumps({ "printed" : [] }, indent=4))
f = open(db_path).read()
db = json.loads(f)
return db
def html2plain(html):
# remove HTML tags
txt = re.sub(r'<.*?>', '', html)
return txt
def preprocess(output, type=None):
# date_str = None
if type == 'txt':
txt = output
if type == 'txt' or type == 'img':
post_date = datetime.now()
date_str = post_date.strftime('%Y-%m-%d_%H-%M-%S')
if type == 'rss':
post = output
txt = post['summary']
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')
if type == 'txt' or type == 'rss':
output = html2plain(txt)
# limit printouts to 25 lines (this is a test)
# lines = [line for line in txt.split('\n')][:25]
# txt = '\n'.join(lines)
return output, date_str
def update_db(db, db_path, post, source, date, type=None):
# add post to database
# and check if this item is new
item = {
'source' : source,
'date' : date,
'printed' : post
}
if item not in db['printed']:
already_in_db = False
with open(db_path, 'w') as out:
db['printed'].append(item)
out.write(json.dumps(db, indent=4))
out.close()
else:
already_in_db = True
return already_in_db
def print_now(output, lp=None, type=None, size=None):
# configure the printouts
if lp:
if type == 'rss':
lp.set(
invert=True,
font='b',
width=6, # 1-8
height=6, # 1-8
)
lp.line_spacing(spacing=75) # 0 - 255
columns = 12 # b 8 8
elif size == "normal":
lp.set(
align='left',
font='a',
text_type='normal', # B, U, U2, BU, BU2, NORMAL
width=1, # 1-8
height=1, # 1-8
density=9, # 0-8, 9=keep unchanged
invert=False,
smooth=False,
flip=False
)
lp.line_spacing(spacing=65) # 0 - 255
# columns = 48 # a 1 1
columns = 48 # b 1 1
elif size == "big":
lp.set(
align='left',
font='b',
text_type='normal', # B, U, U2, BU, BU2, NORMAL
width=8, # 1-8
height=8, # 1-8
invert=True
)
lp.line_spacing(spacing=75) # 0 - 255
# columns = 48 # a 1 1
columns = 8 # b 1 1
elif type == 'img':
lp.line_spacing(spacing=50) # 0 - 255
# print
if lp:
if type == 'txt' or type == 'rss':
# lp._raw(b'\x1b'+b'\x52'+b'\x32') # switch to code table, see http://docshare03.docshare.tips/files/24677/246773902.pdf for more info (does not work)
# lp.magic.force_encoding('CP858') # does not work
lp.block_text(output, columns=columns) # columns=line width
lp.cut()
elif type == 'img':
lp.image(output)
lp.cut()
else:
sys.stderr.write('Printing output to the terminal:\n\n'+str(output)+'\n\n')