This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
if sys.version_info[0] == 3: | |
from io import StringIO | |
else: | |
from cStringIO import StringIO | |
def replace_entities(text, grouped_entities, **replacers): | |
entities = [] | |
for entity_type, entity_list in grouped_entities.items(): | |
replacer = replacers.get(entity_type) | |
if replacer is None: | |
continue | |
for entity in entity_list: | |
entity_pair = (entity['indices'], replacer(entity)) | |
entities.append(entity_pair) | |
entities.sort(key = lambda x: x[0][0]) | |
output = StringIO() | |
last_idx = 0 | |
for entity_pair in entities: | |
indices, entity = entity_pair | |
i, j = indices | |
output.write(text[last_idx : i]) | |
output.write(entity) | |
last_idx = j | |
output.write(text[last_idx :]) | |
return output.getvalue() | |
def tweet_replace_links(tweet): | |
def url_replacer(url): | |
return '<a href="%s">%s</a>' % (url['expanded_url'], url['display_url']) | |
return replace_entities(tweet['text'], tweet['entities'], urls = url_replacer) | |
tweet = { | |
"text": "Today, Twitter is updating embedded Tweets to enable a richer photo experience: https://t.co/XdXRudPXH5", | |
"entities": { | |
"hashtags": [], | |
"symbols": [], | |
"urls": [{ | |
"url": "https://t.co/XdXRudPXH5", | |
"expanded_url": "https://blog.twitter.com/2013/rich-photo-experience-now-in-embedded-tweets-3", | |
"display_url": "blog.twitter.com/2013/rich-phot...", | |
"indices": [80, 103] | |
}], | |
"user_mentions": [] | |
} | |
} | |
print(tweet_replace_links(tweet)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment