Fix html_for_tweet()s handling of media URLs
We were trying to link to each media item using its `url`/`expanded_url`. But there is only one of these, shared across all of a tweet's media items. So attempting to put it in several times, in the same location, was a bit of a mess! So it now only puts the `url`/`expanded_url` in once, no matter how many media items there are.
This commit is contained in:
parent
1511ee7b4d
commit
a27efd9da8
4 changed files with 173 additions and 17 deletions
|
|
@ -39,3 +39,4 @@ test_tweet_identical_urls = {u'entities': {u'hashtags': [], u'user_mentions': []
|
||||||
|
|
||||||
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 }
|
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 }
|
||||||
|
|
||||||
|
test_tweet_media = {'user': {'name': 'Phil Gyford', 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/1167616130/james_200208_300x300_normal.jpg', 'verified': False, 'screen_name': 'philgyford', 'id': 12552, 'id_str': '12552', 'protected': False}, 'geo': {}, 'id': 905105588279013377, 'text': 'I made some D3.js charts showing the years covered by books in a series compared to their publishing dates https://t.co/2yUmmn3TOc https://t.co/OwNc6uJklg', 'entities': {'user_mentions': [], 'hashtags': [], 'media': [{'url': 'https://t.co/OwNc6uJklg', 'display_url': 'pic.twitter.com/OwNc6uJklg', 'media_url_https': 'https://pbs.twimg.com/media/DI-UuNlWAAA_cvz.jpg', 'id': 905105571765944320, 'media_url': 'http://pbs.twimg.com/media/DI-UuNlWAAA_cvz.jpg', 'sizes': [{'h': 256, 'w': 680, 'resize': 'fit'}, {'h': 376, 'w': 1000, 'resize': 'fit'}, {'h': 150, 'w': 150, 'resize': 'crop'}, {'h': 376, 'w': 1000, 'resize': 'fit'}, {'h': 376, 'w': 1000, 'resize': 'fit'}], 'indices': [131, 154], 'expanded_url': 'https://twitter.com/philgyford/status/905105588279013377/photo/1', 'id_str': '905105571765944320'}, {'url': 'https://t.co/OwNc6uJklg', 'display_url': 'pic.twitter.com/OwNc6uJklg', 'media_url_https': 'https://pbs.twimg.com/media/DI-UuQbXUAQ1WlR.jpg', 'id': 905105572529393668, 'media_url': 'http://pbs.twimg.com/media/DI-UuQbXUAQ1WlR.jpg', 'sizes': [{'h': 399, 'w': 1000, 'resize': 'fit'}, {'h': 271, 'w': 680, 'resize': 'fit'}, {'h': 399, 'w': 1000, 'resize': 'fit'}, {'h': 150, 'w': 150, 'resize': 'crop'}, {'h': 399, 'w': 1000, 'resize': 'fit'}], 'indices': [131, 154], 'expanded_url': 'https://twitter.com/philgyford/status/905105588279013377/photo/1', 'id_str': '905105572529393668'}, {'url': 'https://t.co/OwNc6uJklg', 'display_url': 'pic.twitter.com/OwNc6uJklg', 'media_url_https': 'https://pbs.twimg.com/media/DI-UuTIXcAA-t1_.jpg', 'id': 905105573255016448, 'media_url': 'http://pbs.twimg.com/media/DI-UuTIXcAA-t1_.jpg', 'sizes': [{'h': 287, 'w': 1000, 'resize': 'fit'}, {'h': 195, 'w': 680, 'resize': 'fit'}, {'h': 150, 'w': 150, 'resize': 'crop'}, {'h': 287, 'w': 1000, 'resize': 'fit'}, {'h': 287, 'w': 1000, 'resize': 'fit'}], 'indices': [131, 154], 'expanded_url': 'https://twitter.com/philgyford/status/905105588279013377/photo/1', 'id_str': '905105573255016448'}], 'urls': [{'display_url': 'gyford.com/phil/writing/2…', 'url': 'https://t.co/2yUmmn3TOc', 'indices': [107, 130], 'expanded_url': 'http://www.gyford.com/phil/writing/2017/09/05/book-series-charts.php'}]}, 'source': 'web', 'created_at': '2017-09-05 16:29:22 +0000', 'id_str': '905105588279013377'}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ from .config import (
|
||||||
test_tweet_object, test_tweet_html, test_tweet_symbols_object,
|
test_tweet_object, test_tweet_html, test_tweet_symbols_object,
|
||||||
test_tweet_compat_object, test_tweet_extended_object,
|
test_tweet_compat_object, test_tweet_extended_object,
|
||||||
test_tweet_extended_html, test_tweet_identical_urls, test_tweet_reply,
|
test_tweet_extended_html, test_tweet_identical_urls, test_tweet_reply,
|
||||||
|
test_tweet_media,
|
||||||
unittest
|
unittest
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -72,3 +73,11 @@ class TestHtmlForTweetTestCase(unittest.TestCase):
|
||||||
tweet_text = self.api.html_for_tweet(test_tweet_extended_object)
|
tweet_text = self.api.html_for_tweet(test_tweet_extended_object)
|
||||||
# full tweet rendered with suffix
|
# full tweet rendered with suffix
|
||||||
self.assertEqual(test_tweet_extended_html, tweet_text)
|
self.assertEqual(test_tweet_extended_html, tweet_text)
|
||||||
|
|
||||||
|
def test_media(self):
|
||||||
|
tweet_text = self.api.html_for_tweet(test_tweet_media)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
"""I made some D3.js charts showing the years covered by books in a series compared to their publishing dates <a href="https://t.co/2yUmmn3TOc" class="twython-url">gyford.com/phil/writing/2…</a> <a href="https://t.co/OwNc6uJklg" class="twython-media">pic.twitter.com/OwNc6uJklg</a>""",
|
||||||
|
tweet_text)
|
||||||
|
|
||||||
|
|
|
||||||
142
tweet.json
Normal file
142
tweet.json
Normal file
|
|
@ -0,0 +1,142 @@
|
||||||
|
{
|
||||||
|
"source":"web",
|
||||||
|
"entities":{
|
||||||
|
"user_mentions":[ ],
|
||||||
|
"media":[
|
||||||
|
{
|
||||||
|
"expanded_url":"https://twitter.com/philgyford/status/905105588279013377/photo/1",
|
||||||
|
"indices":[ 131, 154 ],
|
||||||
|
"url":"https://t.co/OwNc6uJklg",
|
||||||
|
"media_url":"http://pbs.twimg.com/media/DI-UuNlWAAA_cvz.jpg",
|
||||||
|
"id_str":"905105571765944320",
|
||||||
|
"id":905105571765944320,
|
||||||
|
"media_url_https":"https://pbs.twimg.com/media/DI-UuNlWAAA_cvz.jpg",
|
||||||
|
"sizes":[
|
||||||
|
{
|
||||||
|
"h":256,
|
||||||
|
"resize":"fit",
|
||||||
|
"w":680
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"h":376,
|
||||||
|
"resize":"fit",
|
||||||
|
"w":1000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"h":150,
|
||||||
|
"resize":"crop",
|
||||||
|
"w":150
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"h":376,
|
||||||
|
"resize":"fit",
|
||||||
|
"w":1000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"h":376,
|
||||||
|
"resize":"fit",
|
||||||
|
"w":1000
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"display_url":"pic.twitter.com/OwNc6uJklg"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"expanded_url":"https://twitter.com/philgyford/status/905105588279013377/photo/1",
|
||||||
|
"indices":[ 131, 154 ],
|
||||||
|
"url":"https://t.co/OwNc6uJklg",
|
||||||
|
"media_url":"http://pbs.twimg.com/media/DI-UuQbXUAQ1WlR.jpg",
|
||||||
|
"id_str":"905105572529393668",
|
||||||
|
"id":905105572529393668,
|
||||||
|
"media_url_https":"https://pbs.twimg.com/media/DI-UuQbXUAQ1WlR.jpg",
|
||||||
|
"sizes":[
|
||||||
|
{
|
||||||
|
"h":399,
|
||||||
|
"resize":"fit",
|
||||||
|
"w":1000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"h":271,
|
||||||
|
"resize":"fit",
|
||||||
|
"w":680
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"h":399,
|
||||||
|
"resize":"fit",
|
||||||
|
"w":1000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"h":150,
|
||||||
|
"resize":"crop",
|
||||||
|
"w":150
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"h":399,
|
||||||
|
"resize":"fit",
|
||||||
|
"w":1000
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"display_url":"pic.twitter.com/OwNc6uJklg"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"expanded_url":"https://twitter.com/philgyford/status/905105588279013377/photo/1",
|
||||||
|
"indices":[ 131, 154 ],
|
||||||
|
"url":"https://t.co/OwNc6uJklg",
|
||||||
|
"media_url":"http://pbs.twimg.com/media/DI-UuTIXcAA-t1_.jpg",
|
||||||
|
"id_str":"905105573255016448",
|
||||||
|
"id":905105573255016448,
|
||||||
|
"media_url_https":"https://pbs.twimg.com/media/DI-UuTIXcAA-t1_.jpg",
|
||||||
|
"sizes":[
|
||||||
|
{
|
||||||
|
"h":287,
|
||||||
|
"resize":"fit",
|
||||||
|
"w":1000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"h":195,
|
||||||
|
"resize":"fit",
|
||||||
|
"w":680
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"h":150,
|
||||||
|
"resize":"crop",
|
||||||
|
"w":150
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"h":287,
|
||||||
|
"resize":"fit",
|
||||||
|
"w":1000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"h":287,
|
||||||
|
"resize":"fit",
|
||||||
|
"w":1000
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"display_url":"pic.twitter.com/OwNc6uJklg"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"hashtags":[ ],
|
||||||
|
"urls":[
|
||||||
|
{
|
||||||
|
"indices":[ 107, 130 ],
|
||||||
|
"url":"https://t.co/2yUmmn3TOc",
|
||||||
|
"expanded_url":"http://www.gyford.com/phil/writing/2017/09/05/book-series-charts.php",
|
||||||
|
"display_url":"gyford.com/phil/writing/2\u2026"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"geo":{ },
|
||||||
|
"id_str":"905105588279013377",
|
||||||
|
"text":"I made some D3.js charts showing the years covered by books in a series compared to their publishing dates https://t.co/2yUmmn3TOc https://t.co/OwNc6uJklg",
|
||||||
|
"id":905105588279013377,
|
||||||
|
"created_at":"2017-09-05 16:29:22 +0000",
|
||||||
|
"user":{
|
||||||
|
"name":"Phil Gyford",
|
||||||
|
"screen_name":"philgyford",
|
||||||
|
"protected":false,
|
||||||
|
"id_str":"12552",
|
||||||
|
"profile_image_url_https":"https://pbs.twimg.com/profile_images/1167616130/james_200208_300x300_normal.jpg",
|
||||||
|
"id":12552,
|
||||||
|
"verified":false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -629,26 +629,30 @@ class Twython(EndpointsMixin, object):
|
||||||
else:
|
else:
|
||||||
suffix_text = suffix_text.replace(orig_tweet_text[temp['start']:temp['end']], url_html)
|
suffix_text = suffix_text.replace(orig_tweet_text[temp['start']:temp['end']], url_html)
|
||||||
|
|
||||||
if 'media' in tweet['entities']:
|
if 'media' in tweet['entities'] and len(tweet['entities']['media']) > 0:
|
||||||
for entity in tweet['entities']['media']:
|
# We just link to the overall URL for the tweet's media,
|
||||||
temp = {}
|
# rather than to each individual item.
|
||||||
temp['start'] = entity['indices'][0]
|
# So, we get the URL from the first media item:
|
||||||
temp['end'] = entity['indices'][1]
|
entity = tweet['entities']['media'][0]
|
||||||
|
|
||||||
if use_display_url and entity.get('display_url') and not use_expanded_url:
|
temp = {}
|
||||||
shown_url = entity['display_url']
|
temp['start'] = entity['indices'][0]
|
||||||
elif use_expanded_url and entity.get('expanded_url'):
|
temp['end'] = entity['indices'][1]
|
||||||
shown_url = entity['expanded_url']
|
|
||||||
else:
|
|
||||||
shown_url = entity['url']
|
|
||||||
|
|
||||||
url_html = '<a href="%s" class="twython-media">%s</a>' % (entity['url'], shown_url)
|
if use_display_url and entity.get('display_url') and not use_expanded_url:
|
||||||
|
shown_url = entity['display_url']
|
||||||
|
elif use_expanded_url and entity.get('expanded_url'):
|
||||||
|
shown_url = entity['expanded_url']
|
||||||
|
else:
|
||||||
|
shown_url = entity['url']
|
||||||
|
|
||||||
if display_text_start <= temp['start'] <= display_text_end:
|
url_html = '<a href="%s" class="twython-media">%s</a>' % (entity['url'], shown_url)
|
||||||
temp['replacement'] = url_html
|
|
||||||
entities.append(temp)
|
if display_text_start <= temp['start'] <= display_text_end:
|
||||||
else:
|
temp['replacement'] = url_html
|
||||||
suffix_text = suffix_text.replace(orig_tweet_text[temp['start']:temp['end']], url_html)
|
entities.append(temp)
|
||||||
|
else:
|
||||||
|
suffix_text = suffix_text.replace(orig_tweet_text[temp['start']:temp['end']], url_html)
|
||||||
|
|
||||||
# Now do all the replacements, starting from the end, so that the
|
# Now do all the replacements, starting from the end, so that the
|
||||||
# start/end indices still work:
|
# start/end indices still work:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue