From 5aba3f509e6e7a88f9ae6efc1735729297fa1701 Mon Sep 17 00:00:00 2001 From: jesopo Date: Wed, 9 Oct 2019 15:35:20 +0100 Subject: [PATCH] format multi-line tweets on a single line using things like double spaces --- modules/tweets/format.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/tweets/format.py b/modules/tweets/format.py index 0a4926e4..8b84cba3 100644 --- a/modules/tweets/format.py +++ b/modules/tweets/format.py @@ -6,6 +6,13 @@ def _timestamp(dt): since, unit = utils.time_unit(seconds_since) return "%s %s ago" % (since, unit) +def _normalise(tweet): + while " " in tweet: + tweet = tweet.replace(" ", " ") + lines = [line.strip() for line in tweet.split("\n")] + lines = list(filter(None, lines)) + return html.unescape(" ".join(lines)) + def _tweet(exports, server, tweet, from_url): linked_id = tweet.id username = tweet.user.screen_name @@ -30,8 +37,8 @@ def _tweet(exports, server, tweet, from_url): original_timestamp = _timestamp(tweet.retweeted_status.created_at) return "(@%s%s (%s) retweeted @%s (%s)) %s%s" % (username, verified, created_at, original_username, original_timestamp, - html.unescape(original_text), short_url) + _normalise(original_text), short_url) else: return "(@%s%s, %s) %s%s" % (username, verified, created_at, - html.unescape(tweet.full_text), short_url) + _normalise(tweet.full_text), short_url)