Fix html_for_tweet when a hashtag/mention is a substring of another
This commit is contained in:
parent
26ea0f3ae0
commit
2ea45abb73
1 changed files with 9 additions and 5 deletions
|
|
@ -10,6 +10,7 @@ dealing with the Twitter API
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import warnings
|
import warnings
|
||||||
|
import re
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from requests.auth import HTTPBasicAuth
|
from requests.auth import HTTPBasicAuth
|
||||||
|
|
@ -550,19 +551,22 @@ class Twython(EndpointsMixin, object):
|
||||||
entities = tweet['entities']
|
entities = tweet['entities']
|
||||||
|
|
||||||
# Mentions
|
# Mentions
|
||||||
for entity in entities['user_mentions']:
|
for entity in sorted(entities['user_mentions'],
|
||||||
|
key=lambda mention: len(mention['screen_name']), reverse=True):
|
||||||
start, end = entity['indices'][0], entity['indices'][1]
|
start, end = entity['indices'][0], entity['indices'][1]
|
||||||
|
|
||||||
mention_html = '<a href="https://twitter.com/%(screen_name)s" class="twython-mention">@%(screen_name)s</a>'
|
mention_html = '<a href="https://twitter.com/%(screen_name)s" class="twython-mention">@%(screen_name)s</a>'
|
||||||
text = text.replace(tweet['text'][start:end],
|
text = re.sub(r'(?<!>)' + tweet['text'][start:end] + '(?!</a>)',
|
||||||
mention_html % {'screen_name': entity['screen_name']})
|
mention_html % {'screen_name': entity['screen_name']}, text)
|
||||||
|
|
||||||
# Hashtags
|
# Hashtags
|
||||||
for entity in entities['hashtags']:
|
for entity in sorted(entities['hashtags'],
|
||||||
|
key=lambda hashtag: len(hashtag['text']), reverse=True):
|
||||||
start, end = entity['indices'][0], entity['indices'][1]
|
start, end = entity['indices'][0], entity['indices'][1]
|
||||||
|
|
||||||
hashtag_html = '<a href="https://twitter.com/search?q=%%23%(hashtag)s" class="twython-hashtag">#%(hashtag)s</a>'
|
hashtag_html = '<a href="https://twitter.com/search?q=%%23%(hashtag)s" class="twython-hashtag">#%(hashtag)s</a>'
|
||||||
text = text.replace(tweet['text'][start:end], hashtag_html % {'hashtag': entity['text']})
|
text = re.sub(r'(?<!>)' + tweet['text'][start:end] + '(?!</a>)',
|
||||||
|
hashtag_html % {'hashtag': entity['text']}, text)
|
||||||
|
|
||||||
# Urls
|
# Urls
|
||||||
for entity in entities['urls']:
|
for entity in entities['urls']:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue