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:
parent
11086049af
commit
860b2fa48b
42
printer.py
42
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):
|
||||
|
|
Loading…
Reference in New Issue