@ -2,94 +2,116 @@ 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
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 )
lp_printer = True
# lp = printer.Usb(0x4b8,0xe15) # televex printer @ Varia
lp = printer . Usb ( 0x4b8 , 0xe03 ) # printer @ poel
except :
lp = None
lp_printer = False
return lp , lp_printer
return lp
def database ( db ) :
if not os . path . exists ( db ) :
with open ( db , ' w ' ) as f :
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 )
# 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 :
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 ' )
post = None
else :
post_date = datetime . now ( )
date_str = post_date . strftime ( ' % Y- % m- %d _ % H- % M- % S ' )
if type == ' txt ' or type == ' rss ' :
output = html2plain ( txt )
# clean txt from HTML tags
txt = html2plain ( txt )
# limit printouts to 25 lines (this is a test)
# lines = [line for line in txt.split('\n')][:25]
# txt = '\n'.join(lines)
# add txt to database
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_str ,
' printed ' : tx t
' date ' : date ,
' printed ' : pos t
}
if item not in items [ ' printed ' ] :
new_item = Tru e
with open ( db , ' w ' ) as out :
items [ ' printed ' ] . append ( item )
out . write ( json . dumps ( items , indent = 4 ) )
if item not in db [ ' printed ' ] :
already_in_db = Fals e
with open ( db_path , ' w ' ) as out :
db [ ' printed ' ] . append ( item )
out . write ( json . dumps ( db , indent = 4 ) )
out . close ( )
else :
new_item = Fals e
already_in_db = Tru e
return new_item , txt
return already_in_db
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 )
def print_now ( output , lp = None , type = 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 type == " txt " :
lp . set (
align = ' left ' ,
font = ' b ' ,
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 = 65 # b 1 1
elif type == ' img ' :
lp . line_spacing ( spacing = 50 ) # 0 - 255
# if this item is new
# then print!
if new_item == True :
if lp_printer == True :
lp . text ( txt )
# print
if lp :
if type == ' txt ' or type == ' rss ' :
lp . block_text ( output , columns = columns ) # columns=line width
lp . cut ( )
elif type == ' img ' :
lp . image ( output )
lp . cut ( )
else :
# or print in the terminal!
sys . stderr . write ( ' Printing output to the terminal: \n \n ' + txt + ' \n \n ' )
else :
sys . stderr . write ( ' Printing output to the terminal: \n \n ' + txt + ' \n \n ' )