You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.6 KiB
Python

5 years ago
import argparse
5 years ago
import sys
import os
import six
from random import randint
5 years ago
import random
5 years ago
from PIL import Image, ImageOps
import curses
import sys, termios, tty, os, time
5 years ago
import apirequest
import imageprinter
import requests
class Printer():
ESC = b'\x1B'
CHANGE_EMULATION = ESC+b'\x7B'+b'\x65'
LINE_FEED = b'\n'
CARIDGE_RET = b'\x0D'
CHARWIDTH = 78
strokes = 0
debug = True
target = "/dev/usb/lp0"
if debug:
target = "/dev/tty"
def printchar(self,char):
with open(self.target, 'wb') as printer:
5 years ago
if self.debug:
printer.write(bytes(str(char), 'utf-8'))
else:
printer.write(self.ESC+b"\x7B"+b"\x41")
char=self.strokes*" "+char
printer.write(bytes(str(char), 'utf-8'))
printer.write(self.ESC+b"\x25"+b"\x46"+b"\x0F"+b"\x00"+b"\x04"+b"\x08"+b"\x00")
self.strokes += 1
if(self.strokes>self.CHARWIDTH):
printer.write(self.LINE_FEED)
self.strokes = 0
def printstring(self,string):
with open(self.target, 'wb') as printer:
5 years ago
if self.debug:
printer.write(bytes(str(string), 'utf-8'))
else:
printer.write(self.ESC+b"\x7B"+b"\x41")
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")
self.newline()
def newline(self):
global strokes
with open(self.target, 'wb') as printer:
printer.write(self.LINE_FEED)
self.strokes = 0
class Terminal():
lineinput = ""
5 years ago
commandMode = True
writeloop = True
5 years ago
def __init__(self,printer, persons):
self.printer = printer
5 years ago
self.persons = persons
def waittostart(self):
self.writeloop = True
while self.writeloop:
char = self.getch()
self.writeloop = False
self.lineinput = ""
continue
def waitforinput(self):
5 years ago
self.writeloop = True
while self.writeloop:
char = self.getch()
if (ord(char) == 27):
print("exit publication :-(")
exit(0)
if (ord(char) == 13):
self.printer.newline()
5 years ago
self.command(self.lineinput)
self.lineinput = ""
else:
5 years ago
self.lineinput+=char
self.printer.printchar(char)
def getch(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
5 years ago
def command(self, command):
#print("received command: "+str(command))
if "help" == str(command):
self.printer.printstring("type commands to print the publication")
self.printer.printstring("help:")
self.printer.printstring(" get help on the commands")
#self.writeloop = False
elif str(command).split(" ")[0] == "exercise" or str(command).split(" ")[0] == "thesis":
part = str(command).split(" ")[0]
try:
arguments = str(command.strip()).split(" ")
arguments.pop(0)
for i,arg in enumerate(arguments):
if arg.isdigit():
number = int(arg)
if number > len(apirequest.getcategory(part)):
self.printer.printstring("Number must be inbetween 1 and "+ str(len(apirequest.getcategory(part))))
else:
result = apirequest.get(part, number)
for line in result:
self.printer.printstring(line)
self.printer.newline()
else:
raise Exeption("not a number")
except:
self.printer.printstring("ERROR: no argument given. Usage: "+part+" <number>")
elif str(command).split(" ")[0] == "whois":
if str(command).split(" ")[1] in self.persons:
self.printer.printstring(self.persons[str(command).split(" ")[1]])
else:
self.printer.printstring("Sorry not found, try " + random.choice(list(self.persons)))
elif "exit" == str(command):
self.printer.printstring("Imprint")
result = apirequest.get("imprint", 1)
for line in result:
self.printer.printstring(line)
self.printer.newline()
main()
else:
self.printer.printstring("~ command not found. try help for a list of available commands")
self.printer.newline()
#if the command is valid we want to execute the command and continue the story
#self.writeloop = False
5 years ago
persons = {
"Kittler":"Friedrich A. Kittler (June 12, 1943 October 18, 2011) was a literary scholar and a media theorist. His works relate to media, technology, and the military. ",
"Alex":"Alexander Roidl created Poetic Software as part of his Master's thesis"
}
printer = Printer()
5 years ago
term = Terminal(printer, persons)
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():
term.waittostart()
printer.printstring("W E L C O M E !")
printimagefromurl('https://apod.nasa.gov/apod/image/1701/potw1636aN159_HST_2048.jpg')
printer.printstring("---------------")
printer.printstring("POETIC SOFTWARE")
printer.printstring("---------------")
printer.printstring("type help for help")
printer.newline()
result = apirequest.get("introduction", 1)
for line in result:
printer.printstring(line)
printer.newline()
printer.printstring("theses on POETIC SOFTWARE")
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.newline()
term.waitforinput()
main()