@ -10,6 +10,11 @@ import sys, termios, tty, os, time
import apirequest
import apirequest
import imageprinter
import imageprinter
import requests
import requests
import subprocess
from subprocess import check_output
import datetime
import time
class Printer ( ) :
class Printer ( ) :
ESC = b ' \x1B '
ESC = b ' \x1B '
@ -18,7 +23,7 @@ class Printer():
CARIDGE_RET = b ' \x0D '
CARIDGE_RET = b ' \x0D '
CHARWIDTH = 78
CHARWIDTH = 78
strokes = 0
strokes = 0
debug = Tru e
debug = Fals e
target = " /dev/usb/lp0 "
target = " /dev/usb/lp0 "
if debug :
if debug :
target = " /dev/tty "
target = " /dev/tty "
@ -37,16 +42,26 @@ class Printer():
printer . write ( self . LINE_FEED )
printer . write ( self . LINE_FEED )
self . strokes = 0
self . strokes = 0
def printstring ( self , string ) :
def printstring ( self , string , formating = " p " ) :
with open ( self . target , ' wb ' ) as printer :
with open ( self . target , ' wb ' ) as printer :
if self . debug :
if self . debug :
printer . write ( bytes ( str ( string ) , ' utf-8 ' ) )
if formating == " u " :
printer . write ( b " \x1B " + b " \x5B " + b " \x34 " + b " \x6D " )
printer . write ( bytes ( str ( string ) , ' utf-8 ' ) )
printer . write ( b " \x1B " + b " \x5B " + b " \x30 " + b " \x6D " )
else :
printer . write ( bytes ( str ( string ) , ' utf-8 ' ) )
else :
else :
printer . write ( self . ESC + b " \x7B " + b " \x41 " )
printer . write ( self . ESC + b " \x7B " + b " \x41 " )
printer . write ( bytes ( str ( string ) , ' utf-8 ' ) )
printer . write ( bytes ( str ( string ) , ' utf-8 ' ) )
printer . write ( self . ESC + b " \x25 " + b " \x46 " + b " \x0F " + b " \x00 " + b " \x04 " + b " \x08 " + b " \x00 " )
printer . write ( self . ESC + b " \x25 " + b " \x46 " + b " \x0F " + b " \x00 " + b " \x04 " + b " \x08 " + b " \x00 " )
try :
self . newline ( )
except :
time . sleep ( 1 )
self . newline ( )
self . newline ( )
def newline ( self ) :
def newline ( self ) :
global strokes
global strokes
@ -55,12 +70,35 @@ class Printer():
self . strokes = 0
self . strokes = 0
def printimagefromurl ( self , url ) :
page = requests . get ( url )
f_ext = os . path . splitext ( url ) [ - 1 ]
f_name = ' img {} ' . format ( f_ext )
with open ( f_name , ' wb ' ) as f :
f . write ( page . content )
if printer . debug :
rows , columns = os . popen ( ' stty size ' , ' r ' ) . read ( ) . split ( )
subprocess . call ( [ " catimg " , " -w " , columns , f_name ] )
else :
imageprinter . print ( f_name )
class Terminal ( ) :
class Terminal ( ) :
lineinput = " "
lineinput = " "
commandMode = True
commandMode = True
writeloop = True
writeloop = True
commands = {
" help " : " get help on the commands " ,
" start " : " start publication " ,
" list " : " list theses and exercises " ,
" exercise <number> " : " see the exercise with the <number> specified " ,
" thesis <number> " : " see the thesis with the <number> specified " ,
" exit " : " finish the publication " ,
}
def __init__ ( self , printer , persons ) :
def __init__ ( self , printer , persons ) :
self . printer = printer
self . printer = printer
self . persons = persons
self . persons = persons
@ -73,8 +111,9 @@ class Terminal():
self . lineinput = " "
self . lineinput = " "
continue
continue
def waitforinput ( self ):
def waitforinput ( self , nocommand = False ):
self . writeloop = True
self . writeloop = True
self . printer . printchar ( " > " )
while self . writeloop :
while self . writeloop :
char = self . getch ( )
char = self . getch ( )
if ( ord ( char ) == 27 ) :
if ( ord ( char ) == 27 ) :
@ -82,8 +121,14 @@ class Terminal():
exit ( 0 )
exit ( 0 )
if ( ord ( char ) == 13 ) :
if ( ord ( char ) == 13 ) :
self . printer . newline ( )
self . printer . newline ( )
self . command ( self . lineinput )
if not nocommand :
self . lineinput = " "
if not self . lineinput == " " :
self . command ( self . lineinput )
self . lineinput = " "
self . printer . printchar ( " > " )
else :
self . writeloop = False
else :
else :
self . lineinput + = char
self . lineinput + = char
self . printer . printchar ( char )
self . printer . printchar ( char )
@ -99,33 +144,65 @@ class Terminal():
termios . tcsetattr ( fd , termios . TCSADRAIN , old_settings )
termios . tcsetattr ( fd , termios . TCSADRAIN , old_settings )
return ch
return ch
def scanwifi ( self ) :
self . printer . newline ( )
out = check_output ( [ " nmcli " , " dev " , " wif " ] )
self . printer . printstring ( out . decode ( " utf-8 " ) )
def list ( self ) :
printer . printstring ( " theses on POETIC SOFTWARE " , " u " )
result = apirequest . getcategory ( " thesis " )
for i , line in enumerate ( result ) :
printer . printstring ( str ( i + 1 ) + " " + line )
printer . newline ( )
printer . printstring ( " exercises in POETIC SOFTWARE " , " u " )
result = apirequest . getcategory ( " exercise " )
for i , line in enumerate ( result ) :
printer . printstring ( str ( i + 1 ) + " " + line )
printer . newline ( )
def command ( self , command ) :
def command ( self , command ) :
#print("received command: "+str(command))
#print("received command: "+str(command))
if " help " == str ( command ) :
if " help " == str ( command ) :
self . printer . printstring ( " type commands to print the publication " )
self . printer . printstring ( " The user in this publication is an active agent. The publication can be assembled according to your own wishes using commands. The following commands can be used. " )
self . printer . printstring ( " help: " )
for key , help in self . commands . items ( ) :
self . printer . printstring ( " get help on the commands " )
self . printer . printstring ( key + " : " )
self . printer . printstring ( " " + help )
#self.writeloop = False
#self.writeloop = False
elif " list " == str ( command ) :
self . list ( )
elif " start " == str ( command ) or " restart " == str ( command ) :
main ( )
elif str ( command ) . split ( " " ) [ 0 ] == " exercise " or str ( command ) . split ( " " ) [ 0 ] == " thesis " :
elif str ( command ) . split ( " " ) [ 0 ] == " exercise " or str ( command ) . split ( " " ) [ 0 ] == " thesis " :
part = str ( command ) . split ( " " ) [ 0 ]
part = str ( command ) . split ( " " ) [ 0 ]
try :
arguments = str ( command . strip ( ) ) . split ( " " )
arguments = str ( command . strip ( ) ) . split ( " " )
arguments . pop ( 0 )
arguments . pop ( 0 )
if len ( arguments ) < 1 :
self . printer . printstring ( " no argument given. Usage: " + part + " <number> " )
else :
for i , arg in enumerate ( arguments ) :
for i , arg in enumerate ( arguments ) :
if arg . isdigit ( ) :
if arg . isdigit ( ) :
number = int ( arg )
number = int ( arg )
if number > len ( apirequest . getcategory ( part ) ) :
if number > len ( apirequest . getcategory ( part ) ) :
self . printer . printstring ( " Number must be inbetween 1 and " + str ( len ( apirequest . getcategory ( part ) ) ) )
self . printer . printstring ( " Number must be inbetween 1 and " + str ( len ( apirequest . getcategory ( part ) ) ) )
else :
else :
result = apirequest . get ( part , number )
result = apirequest . getimages ( part , number )
for img in result :
printer . printimagefromurl ( img )
result = apirequest . gettitle ( part , number )
for line in result :
self . printer . printstring ( line , " u " )
result = apirequest . getcontent ( part , number )
for line in result :
for line in result :
self . printer . printstring ( line )
self . printer . printstring ( line )
if apirequest . gettitle ( part , number ) [ 0 ] == " Wifi Poem " :
self . scanwifi ( )
self . printer . newline ( )
self . printer . newline ( )
else :
else :
raise Exeption ( " not a number " )
self . printer . printstring ( " not a number " )
except :
self . printer . printstring ( " ERROR: no argument given. Usage: " + part + " <number> " )
elif str ( command ) . split ( " " ) [ 0 ] == " whois " :
elif str ( command ) . split ( " " ) [ 0 ] == " whois " :
if str ( command ) . split ( " " ) [ 1 ] in self . persons :
if str ( command ) . split ( " " ) [ 1 ] in self . persons :
@ -134,10 +211,15 @@ class Terminal():
self . printer . printstring ( " Sorry not found, try " + random . choice ( list ( self . persons ) ) )
self . printer . printstring ( " Sorry not found, try " + random . choice ( list ( self . persons ) ) )
elif " exit " == str ( command ) :
elif " exit " == str ( command ) :
self . printer . printstring ( " Imprint " )
self . printer . printstring ( " Imprint " , " u " )
result = apirequest . get ( " imprint " , 1 )
self . printer . newline ( )
for line in result :
self . printer . printstring ( " 2019, Piet Zwart Institute, Rotterdam " )
self . printer . printstring ( line )
self . printer . printstring ( " Published by Alexander Roidl " )
self . printer . printstring ( " and (enter your name) " )
self . printer . newline ( )
self . waitforinput ( nocommand = True )
self . printer . newline ( )
self . printer . printstring ( " Special thanks to Aymeric Mansoux, Marloes de Valk, Michael Murtaugh, André Castro, Clara Balaguer, Amy Suo Wu, Leslie Robbins, Angeliki, Alice, Tash, Zalan, Joca, Jule " )
self . printer . newline ( )
self . printer . newline ( )
main ( )
main ( )
@ -155,41 +237,28 @@ persons = {
}
}
printer = Printer ( )
printer = Printer ( )
term = Terminal ( printer , persons )
term = Terminal ( printer , persons )
now = datetime . datetime . now ( )
def printimagefromurl ( url ) :
page = requests . get ( url )
f_ext = os . path . splitext ( url ) [ - 1 ]
f_name = ' img {} ' . format ( f_ext )
with open ( f_name , ' wb ' ) as f :
f . write ( page . content )
imageprinter . print ( f_name )
def main ( ) :
def main ( ) :
term . waittostart ( )
term . waittostart ( )
printer . printstring ( " W E L C O M E ! " )
printer . printstring ( " W E L C O M E ! " )
printer . newline ( )
printimagefromurl ( ' https://apod.nasa.gov/apod/image/1701/potw1636aN159_HST_2048.jpg ' )
#printimagefromurl('https://apod.nasa.gov/apod/image/1701/potw1636aN159_HST_2048.jpg')
printer . printstring ( now . strftime ( " % Y- % m- %d % H: % M " ) )
printer . newline ( )
printer . printstring ( " --------------- " )
printer . printstring ( " --------------- " )
printer . printstring ( " POETIC SOFTWARE " )
printer . printstring ( " POETIC SOFTWARE " )
printer . printstring ( " --------------- " )
printer . printstring ( " --------------- " )
printer . printstring ( " type help for help " )
printer . printstring ( " type help for help " )
printer . newline ( )
printer . newline ( )
result = apirequest . get ( " introduction " , 1 )
result = apirequest . gettitle ( " introduction " , 1 )
for line in result :
printer . printstring ( line , " u " )
result = apirequest . getcontent ( " introduction " , 1 )
for line in result :
for line in result :
printer . printstring ( line )
printer . printstring ( line )
printer . newline ( )
printer . newline ( )
printer . printstring ( " theses on POETIC SOFTWARE " )
term . list ( )
result = apirequest . getcategory ( " thesis " )
for i , line in enumerate ( result ) :
printer . printstring ( str ( i + 1 ) + " " + line )
printer . newline ( )
printer . printstring ( " exercises in POETIC SOFTWARE " )
result = apirequest . getcategory ( " exercise " )
for i , line in enumerate ( result ) :
printer . printstring ( str ( i + 1 ) + " " + line )
printer . newline ( )
printer . printstring ( " SELECT which one to see, for exercise type: exercise <number> and for thesis type: thesis <number> " )
printer . printstring ( " SELECT which one to see, for exercise type: exercise <number> and for thesis type: thesis <number> " )
printer . newline ( )
printer . newline ( )
term . waitforinput ( )
term . waitforinput ( )