Ensure html_for_tweet() works if symbols is missing
Some Tweets, such as those in a downloaded archive, don't have a `symbols` element in their `entities` dict. This fix ensures `html_for_tweet()` still works if that is missing.
This commit is contained in:
parent
5a87fc7d84
commit
4a99ec0157
2 changed files with 20 additions and 6 deletions
|
|
@ -327,6 +327,19 @@ class TwythonAPITestCase(unittest.TestCase):
|
||||||
self.assertTrue('<a href="https://twitter.com/search?q=%24AAPL" class="twython-symbol">$AAPL</a>' in tweet_text)
|
self.assertTrue('<a href="https://twitter.com/search?q=%24AAPL" class="twython-symbol">$AAPL</a>' in tweet_text)
|
||||||
self.assertTrue('<a href="https://twitter.com/search?q=%24ANOTHER" class="twython-symbol">$ANOTHER</a>' not in tweet_text)
|
self.assertTrue('<a href="https://twitter.com/search?q=%24ANOTHER" class="twython-symbol">$ANOTHER</a>' not in tweet_text)
|
||||||
|
|
||||||
|
def test_html_for_tweet_no_symbols(self):
|
||||||
|
"""Should still work if tweet object has no symbols list"""
|
||||||
|
tweet = test_tweet_symbols_object
|
||||||
|
# Save a copy:
|
||||||
|
symbols = tweet['entities']['symbols']
|
||||||
|
del tweet['entities']['symbols']
|
||||||
|
tweet_text = self.api.html_for_tweet(tweet)
|
||||||
|
self.assertTrue('symbols: $AAPL and' in tweet_text)
|
||||||
|
self.assertTrue('and $ANOTHER and $A.' in tweet_text)
|
||||||
|
# Put the symbols back:
|
||||||
|
test_tweet_symbols_object['entities']['symbols'] = symbols
|
||||||
|
|
||||||
|
|
||||||
def test_html_for_tweet_compatmode(self):
|
def test_html_for_tweet_compatmode(self):
|
||||||
tweet_text = self.api.html_for_tweet(test_tweet_compat_object)
|
tweet_text = self.api.html_for_tweet(test_tweet_compat_object)
|
||||||
# link to compat web status link
|
# link to compat web status link
|
||||||
|
|
|
||||||
|
|
@ -581,6 +581,7 @@ class Twython(EndpointsMixin, object):
|
||||||
hashtag_html % {'hashtag': entity['text']}, display_text)
|
hashtag_html % {'hashtag': entity['text']}, display_text)
|
||||||
|
|
||||||
# Symbols
|
# Symbols
|
||||||
|
if 'symbols' in entities:
|
||||||
for entity in sorted(entities['symbols'],
|
for entity in sorted(entities['symbols'],
|
||||||
key=lambda symbol: len(symbol['text']), reverse=True):
|
key=lambda symbol: len(symbol['text']), reverse=True):
|
||||||
start, end = entity['indices'][0], entity['indices'][1]
|
start, end = entity['indices'][0], entity['indices'][1]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue