small printer tweaks for twitter timeline

workspace
Brendan Howell 8 years ago
parent 110241161f
commit 8f8ee7b6af

@ -247,29 +247,28 @@ class Bureau(object):
subprocess.call("lpr " + pdfpath, shell=True) subprocess.call("lpr " + pdfpath, shell=True)
# TODO: make this asynchronous # TODO: make this asynchronous
def print_small(self, text, printer="/dev/usb/lp0"): def print_small(self, text, cut=True):
""" """
print on Thermal Line printer. print on Thermal Line printer.
""" """
# TODO: look up device and width in config # TODO: look up device and width in config
# TODO: refactor this to use escpos library prn = printer.Usb(0x416, 05011, in_ep=0x81, out_ep=0x03)
lp = open(printer, "w") text = textwrap.fill(text, width=48)
text = textwrap.fill(text, width=32) text += "\r\n" * 2
text += "\r\n" * 5 prn.text(text + "\r\n\r\n")
# text += ".d0" if cut:
lp.write(text) prn.cut()
lp.close()
def print_small_image(self, img): def print_small_image(self, img):
""" """
print an image on the mini thermal printer. print an image on the mini thermal printer.
""" """
# TODO: make printer id/width configured and easy # TODO: make printer id/width configured and easy
prn = printer.Usb(0x416, 0x5011) prn = printer.Usb(0x416, 0x5011, in_ep=0x81, out_ep=0x03)
im = PIL.Image.open(img) im = PIL.Image.open(img)
# NOTE: might be worth tring to push up brightness # NOTE: might be worth tring to push up brightness
im = PIL.ImageOps.equalize(im) # stretch histogram for nicer dither im = PIL.ImageOps.equalize(im) # stretch histogram for nicer dither
im.thumbnail((384, 384), PIL.Image.ANTIALIAS) # resize to fit printer im.thumbnail((576, 576), PIL.Image.ANTIALIAS) # resize to fit printer
prn.image(im, impl="bitImageColumn") # not using this impl crashes ?? prn.image(im, impl="bitImageColumn") # not using this impl crashes ??
@add_command("test") @add_command("test")

@ -1,4 +1,9 @@
import textwrap
import urllib
from escpos import printer
import facebook import facebook
import PIL
import requests import requests
import twitter import twitter
@ -82,14 +87,36 @@ class PublicRelations(Bureau):
else: else:
count = 10 count = 10
prn = printer.Usb(0x416, 0x5011, in_ep=0x81, out_ep=0x03)
prn.codepage = "cp437"
# TODO: add fancier formatting i.e. inverted text for username/handle # TODO: add fancier formatting i.e. inverted text for username/handle
tweets = self.t.t.statuses.home_timeline(count=count) tweets = self.t.t.statuses.home_timeline(count=count)
out = "" out = ""
for t in tweets: for t in tweets:
out += t["user"]["name"] + " : " + t["text"] + "\r\n\r\n" prn.set(text_type="U")
self.print_small(out) prn.text(t["user"]["name"] + "\r\n")
# TODO: download and print image entities: prn.set(text_type="NORMAL")
# https://dev.twitter.com/overview/api/entities-in-twitter-objects t_wrapped = textwrap.fill(t["text"], width=48) + "\r\n"
t_enc = t_wrapped.encode("cp437", "ignore")
prn._raw(t_enc)
if "media" in t["entities"]:
if len(t["entities"]["media"]) > 0:
i_url = t["entities"]["media"][0]["media_url"]
filename = i_url.rsplit('/',1)[1]
filename = "/tmp/" + filename
print("fetching", i_url, filename)
urllib.request.urlretrieve(i_url, filename)
im = PIL.Image.open(filename)
im = PIL.ImageOps.equalize(im)
im.thumbnail((576, 576), PIL.Image.ANTIALIAS)
prn.image(im, impl="bitImageColumn")
prn.text("\r\n\r\n")
prn.cut()
def main(): def main():

Loading…
Cancel
Save