diff --git a/tests/config.py b/tests/config.py
index c3ea809..07ca96d 100644
--- a/tests/config.py
+++ b/tests/config.py
@@ -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_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'}
diff --git a/tests/test_html_for_tweet.py b/tests/test_html_for_tweet.py
index b8b5dc4..dc44047 100644
--- a/tests/test_html_for_tweet.py
+++ b/tests/test_html_for_tweet.py
@@ -5,6 +5,7 @@ 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_reply,
+ test_tweet_media,
unittest
)
@@ -72,3 +73,11 @@ class TestHtmlForTweetTestCase(unittest.TestCase):
tweet_text = self.api.html_for_tweet(test_tweet_extended_object)
# full tweet rendered with suffix
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 gyford.com/phil/writing/2… pic.twitter.com/OwNc6uJklg""",
+ tweet_text)
+
diff --git a/tweet.json b/tweet.json
new file mode 100644
index 0000000..5e61ed8
--- /dev/null
+++ b/tweet.json
@@ -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
+ }
+}
diff --git a/twython/api.py b/twython/api.py
index 2782168..fac7928 100644
--- a/twython/api.py
+++ b/twython/api.py
@@ -629,26 +629,30 @@ class Twython(EndpointsMixin, object):
else:
suffix_text = suffix_text.replace(orig_tweet_text[temp['start']:temp['end']], url_html)
- if 'media' in tweet['entities']:
- for entity in tweet['entities']['media']:
- temp = {}
- temp['start'] = entity['indices'][0]
- temp['end'] = entity['indices'][1]
+ if 'media' in tweet['entities'] and len(tweet['entities']['media']) > 0:
+ # We just link to the overall URL for the tweet's media,
+ # rather than to each individual item.
+ # So, we get the URL from the first media item:
+ entity = tweet['entities']['media'][0]
- 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']
+ temp = {}
+ temp['start'] = entity['indices'][0]
+ temp['end'] = entity['indices'][1]
- url_html = '%s' % (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:
- temp['replacement'] = url_html
- entities.append(temp)
- else:
- suffix_text = suffix_text.replace(orig_tweet_text[temp['start']:temp['end']], url_html)
+ url_html = '%s' % (entity['url'], shown_url)
+
+ if display_text_start <= temp['start'] <= display_text_end:
+ temp['replacement'] = 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
# start/end indices still work: