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.
This commit is contained in:
Daniel Schulte 2018-03-18 19:05:28 +01:00
parent 11086049af
commit 860b2fa48b
1 changed files with 24 additions and 18 deletions

View File

@ -13,6 +13,7 @@ import datetime
import html import html
import logging import logging
import sys import sys
import textwrap
import toml import toml
import tweepy import tweepy
@ -131,6 +132,27 @@ class StreamListener(tweepy.StreamListener):
self.ignore_rt = ignore_rt self.ignore_rt = ignore_rt
self.printer = printer 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): def on_status(self, status):
if status.id in self.seen_tweets: if status.id in self.seen_tweets:
@ -152,14 +174,7 @@ class StreamListener(tweepy.StreamListener):
print(header) print(header)
print(text) print(text)
if self.printer: if self.printer:
try: self.print_tweet(text, [header])
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.")
else: else:
if self.ignore_rt: if self.ignore_rt:
#print("******** Ignoring Retweet") #print("******** Ignoring Retweet")
@ -177,16 +192,7 @@ class StreamListener(tweepy.StreamListener):
print(rt_header) print(rt_header)
print(rt_text) print(rt_text)
if self.printer: if self.printer:
try: self.print_tweet(rt_text, [header, rt_header])
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.")
return True return True
def on_error(self, status_code): def on_error(self, status_code):