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)
# 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")

@ -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():

Loading…
Cancel
Save