From 0ee9b76b5cb6fd86ce8b75287ca33579fd1d18b5 Mon Sep 17 00:00:00 2001 From: Phil Gyford Date: Tue, 3 Oct 2017 19:12:02 +0100 Subject: [PATCH] Make html_for_tweet() link a replied-to username If a tweet was a reply, then when `html_for_tweet()` tried to turn the initial "@username" into a link, there was: > NameError: name 'sub_expr' is not defined This is now fixed, with a test to ensure the "@username" becomes a link. --- tests/config.py | 2 ++ tests/test_core.py | 10 ++++++++-- twython/api.py | 3 +++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/config.py b/tests/config.py index 9e0aa15..c3ea809 100644 --- a/tests/config.py +++ b/tests/config.py @@ -37,3 +37,5 @@ test_tweet_extended_html = 'Say more about what\'s happening! Rolling out now: p test_tweet_identical_urls = {u'entities': {u'hashtags': [], u'user_mentions': [], u'symbols': [], u'urls': [{u'display_url': u'buff.ly/2sEhrgO', u'expanded_url': u'http://buff.ly/2sEhrgO', u'indices': [42, 65], u'url': u'https://t.co/W0uArTMk9N'}, {u'display_url': u'buff.ly/2sEhrgO', u'expanded_url': u'http://buff.ly/2sEhrgO', u'indices': [101, 124], u'url': u'https://t.co/W0uArTMk9N'}]}, u'full_text': u'Use Cases, Trials and Making 5G a Reality https://t.co/W0uArTMk9N #5G #innovation via @5GWorldSeries https://t.co/W0uArTMk9N'} +test_tweet_reply = { u'display_text_range': [12,114], u'in_reply_to_status_id_str':u'742374355531923456', u'source':u'web', u'geo':None, u'full_text':u'@philgyford Here\u2019s a test tweet that goes on as much as possible and includes an image. Hi to my fans in testland! https://t.co/tzhyk2QWSr', u'extended_entities':{ u'media':[] }, u'id_str':u'300', u'in_reply_to_status_id':742374355531923456, u'id':300, u'in_reply_to_screen_name':u'philgyford', u'retweet_count':0, u'user':{ }, u'created_at':u'Mon Jun 13 15:48:06 +0000 2016', u'lang':u'en', u'favorite_count':0, u'coordinates':None, u'place':None, u'contributors':None, u'in_reply_to_user_id':12552, u'in_reply_to_user_id_str':u'12552', u'retweeted':False, u'favorited':False, u'truncated':False, u'entities':{ u'user_mentions':[ { u'id_str':u'12552', u'id':12552, u'screen_name':u'philgyford', u'name':u'Phil Gyford', u'indices':[ 0, 11 ] } ], u'media':[ ], u'hashtags':[ ], u'symbols':[ ], u'urls':[ ] }, u'is_quote_status':False, u'possibly_sensitive':False } + diff --git a/tests/test_core.py b/tests/test_core.py index 44adf95..b824f86 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -3,8 +3,8 @@ from twython import Twython, TwythonError, TwythonAuthError, TwythonRateLimitErr from .config import ( test_tweet_object, test_tweet_html, test_tweet_symbols_object, - test_tweet_compat_object, test_tweet_extended_object, test_tweet_extended_html, - test_tweet_identical_urls, + test_tweet_compat_object, test_tweet_extended_object, + test_tweet_extended_html, test_tweet_identical_urls, test_tweet_reply, unittest ) @@ -308,6 +308,12 @@ class TwythonAPITestCase(unittest.TestCase): tweet_text = self.api.html_for_tweet(test_tweet_object) self.assertEqual(test_tweet_html, tweet_text) + def test_html_for_tweet_reply(self): + """Test HTML for Tweet links the replied-to username.""" + tweet_text = self.api.html_for_tweet(test_tweet_reply) + self.assertEqual(tweet_text, + u'@philgyford Here’s a test tweet that goes on as much as possible and includes an image. Hi to my fans in testland! https://t.co/tzhyk2QWSr') + def test_html_for_tweet_expanded_url(self): """Test using expanded url in HTML for Tweet displays full urls""" tweet_text = self.api.html_for_tweet(test_tweet_object, diff --git a/twython/api.py b/twython/api.py index 5f0c50d..2782168 100644 --- a/twython/api.py +++ b/twython/api.py @@ -578,6 +578,9 @@ class Twython(EndpointsMixin, object): temp['replacement'] = mention_html entities.append(temp) else: + # Make the '@username' at the start, before + # display_text, into a link: + sub_expr = r'(?)' + orig_tweet_text[temp['start']:temp['end']] + '(?!)' prefix_text = re.sub(sub_expr, mention_html, prefix_text) # Hashtags