From 860b2fa48bfec9c22ed15c77eb1f207a8df820ac Mon Sep 17 00:00:00 2001 From: Daniel Schulte Date: Sun, 18 Mar 2018 19:05:28 +0100 Subject: [PATCH] Refactor Tweet printing to work in a line based fashion using the textwrap library The textwrap library is contained in the python standard library. The code contains a workaround for textwrap.wrap discarding newlines which should be preserved. Therefore the text is first split into lines and non empty lines get processed by textwrap.wrap. Empty lines get appended to the line buffer. --- printer.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/printer.py b/printer.py index 28f2eb9..4fda241 100755 --- a/printer.py +++ b/printer.py @@ -13,6 +13,7 @@ import datetime import html import logging import sys +import textwrap import toml import tweepy @@ -131,6 +132,27 @@ class StreamListener(tweepy.StreamListener): self.ignore_rt = ignore_rt self.printer = printer + @classmethod + def split_and_encode_text(cls, text, extra_lines=[]): + lines = extra_lines + for line in text.splitlines(True): + stripped = line.strip() + if stripped: + lines += textwrap.wrap(stripped, printer_line_width) + else: + lines.append(stripped) + return [line.encode("latin-1") for line in lines] + + def print_tweet(self, text, headers): + try: + encoded_lines = self.split_and_encode_text(text, headers) + for line in encoded_lines: + self.printer.write(line) + self.printer.write("", False, True) + self.printer.flush() + except UnicodeEncodeError: + print("Can't print. Encoding issue.") + def on_status(self, status): if status.id in self.seen_tweets: @@ -152,14 +174,7 @@ class StreamListener(tweepy.StreamListener): print(header) print(text) if self.printer: - try: - encoded_header = header.encode("latin-1") - encoded_text = text.encode("latin-1") - self.printer.write(encoded_header) - self.printer.write(encoded_text, True, True) - self.printer.flush() - except UnicodeEncodeError: - print("Can't print. Encoding issue.") + self.print_tweet(text, [header]) else: if self.ignore_rt: #print("******** Ignoring Retweet") @@ -177,16 +192,7 @@ class StreamListener(tweepy.StreamListener): print(rt_header) print(rt_text) if self.printer: - try: - encoded_header = header.encode("latin-1") - encoded_rt_header = rt_header.encode("latin-1") - encoded_rt_text = rt_text.encode("latin-1") - self.printer.write(encoded_header) - self.printer.write(encoded_rt_header) - self.printer.write(encoded_rt_text, True, True) - self.printer.flush() - except UnicodeEncodeError: - print("Can't print. Encoding issue.") + self.print_tweet(rt_text, [header, rt_header]) return True def on_error(self, status_code):