From ed1b9698f9df046831cb847bc185975f0bae5425 Mon Sep 17 00:00:00 2001 From: Daniel Schulte Date: Sun, 18 Mar 2018 19:06:57 +0100 Subject: [PATCH] Add feature to allow upside down printing of Tweets. This is useful when the printer is mounted on a wall. --- config.toml.example | 3 +++ printer.py | 14 +++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/config.toml.example b/config.toml.example index 39cf235..1f48879 100644 --- a/config.toml.example +++ b/config.toml.example @@ -5,3 +5,6 @@ secret="PUT_YOUR_CONSUMER_SECRET_HERE" [auth.access] token="PUT_YOUR_ACCESS_TOKEN_HERE" secret="PUT_YOUR_ACCESS_SECRET_HERE" + +[printer] +flipped=false diff --git a/printer.py b/printer.py index 4fda241..8200cf6 100755 --- a/printer.py +++ b/printer.py @@ -6,7 +6,7 @@ TODO: Figure out if/when to show unshortened URLs in links contained in Tweets. TODO: Figure out bitmap mode so stuff that is not Latin-1 encodable can still be printed. """ -__version__ = "0.0.1" +__version__ = "0.0.2" import argparse import datetime @@ -93,13 +93,17 @@ def format_header(handle, name, dt, is_rt=None): class Printer: - def __init__(self, port): + def __init__(self, port, **kwargs): self.port = port + for arg, default in [("flipped", False)]: + setattr(self, arg, kwargs.get(arg, default)) def __enter__(self): self.printer = open(self.port, "wb") self.printer.write(bytes([0x1b, 0x40])) # Initialize printer self.printer.write(bytes([0x1b, 0x74, 0x06])) # Set character code table to CP1252/Latin-1 + if self.flipped: + self.printer.write(bytes([0x1b, 0x7b, 0x01])) # Set printer to upside-down mode return self def __exit__(self, exc_type, exc_value, traceback): @@ -146,6 +150,8 @@ class StreamListener(tweepy.StreamListener): def print_tweet(self, text, headers): try: encoded_lines = self.split_and_encode_text(text, headers) + if self.printer.flipped: + encoded_lines.reverse() for line in encoded_lines: self.printer.write(line) self.printer.write("", False, True) @@ -227,6 +233,8 @@ def main(): args = parser.parse_args() config = load_config() + printer_config = config.get("printer", {}) + flip_printer = printer_config.get("flipped", False) consumer_key = config["auth"]["consumer"]["key"] consumer_secret = config["auth"]["consumer"]["secret"] @@ -258,7 +266,7 @@ def main(): try: if args.printer: - with Printer(args.printer) as thermal: + with Printer(args.printer, flipped=flip_printer) as thermal: logger.info("Streaming {} to thermal printer {}...".format(stream_name, thermal.port)) listener = StreamListener(stream_name, ignore_rt=not show_rts, printer=thermal) start_streaming(listener, api)