Add feature to allow upside down printing of Tweets.

This is useful when the printer is mounted on a wall.
This commit is contained in:
Daniel Schulte 2018-03-18 19:06:57 +01:00
parent 860b2fa48b
commit ed1b9698f9
2 changed files with 14 additions and 3 deletions

View File

@ -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

View File

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