From 2ea45abb734af5a3de1e89a4ed2ea1a63e482160 Mon Sep 17 00:00:00 2001 From: ping Date: Sat, 15 Aug 2015 19:43:59 +0800 Subject: [PATCH] Fix html_for_tweet when a hashtag/mention is a substring of another --- twython/api.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/twython/api.py b/twython/api.py index d2d5c03..d83c2da 100644 --- a/twython/api.py +++ b/twython/api.py @@ -10,6 +10,7 @@ dealing with the Twitter API """ import warnings +import re import requests from requests.auth import HTTPBasicAuth @@ -550,19 +551,22 @@ class Twython(EndpointsMixin, object): entities = tweet['entities'] # 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] mention_html = '@%(screen_name)s' - text = text.replace(tweet['text'][start:end], - mention_html % {'screen_name': entity['screen_name']}) + text = re.sub(r'(?)' + tweet['text'][start:end] + '(?!)', + mention_html % {'screen_name': entity['screen_name']}, text) # 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] hashtag_html = '#%(hashtag)s' - text = text.replace(tweet['text'][start:end], hashtag_html % {'hashtag': entity['text']}) + text = re.sub(r'(?)' + tweet['text'][start:end] + '(?!)', + hashtag_html % {'hashtag': entity['text']}, text) # Urls for entity in entities['urls']: -- 2.39.5