From 8f8ee7b6afe77c904fc4b69c3583cdd5cf57de63 Mon Sep 17 00:00:00 2001 From: Brendan Howell Date: Wed, 3 May 2017 21:52:47 +0200 Subject: [PATCH] small printer tweaks for twitter timeline --- screenless/bureau/bureau.py | 19 +++++----- .../bureau/publicrelations/publicrelations.py | 35 ++++++++++++++++--- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/screenless/bureau/bureau.py b/screenless/bureau/bureau.py index 51c5695..ad3f612 100644 --- a/screenless/bureau/bureau.py +++ b/screenless/bureau/bureau.py @@ -247,29 +247,28 @@ class Bureau(object): subprocess.call("lpr " + pdfpath, shell=True) # 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. """ # TODO: look up device and width in config - # TODO: refactor this to use escpos library - lp = open(printer, "w") - text = textwrap.fill(text, width=32) - text += "\r\n" * 5 - # text += ".d0" - lp.write(text) - lp.close() + prn = printer.Usb(0x416, 05011, in_ep=0x81, out_ep=0x03) + text = textwrap.fill(text, width=48) + text += "\r\n" * 2 + prn.text(text + "\r\n\r\n") + if cut: + prn.cut() def print_small_image(self, img): """ print an image on the mini thermal printer. """ # 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) # NOTE: might be worth tring to push up brightness 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 ?? @add_command("test") diff --git a/screenless/bureau/publicrelations/publicrelations.py b/screenless/bureau/publicrelations/publicrelations.py index 2ac572c..d1e635e 100644 --- a/screenless/bureau/publicrelations/publicrelations.py +++ b/screenless/bureau/publicrelations/publicrelations.py @@ -1,4 +1,9 @@ +import textwrap +import urllib + +from escpos import printer import facebook +import PIL import requests import twitter @@ -82,14 +87,36 @@ class PublicRelations(Bureau): else: 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 tweets = self.t.t.statuses.home_timeline(count=count) out = "" for t in tweets: - out += t["user"]["name"] + " : " + t["text"] + "\r\n\r\n" - self.print_small(out) - # TODO: download and print image entities: - # https://dev.twitter.com/overview/api/entities-in-twitter-objects + prn.set(text_type="U") + prn.text(t["user"]["name"] + "\r\n") + prn.set(text_type="NORMAL") + 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():