MCPcopy Index your code
hub / github.com/ryanmcgrath/twython / html_for_tweet

Method html_for_tweet

twython/api.py:562–709  ·  view source on GitHub ↗

Return HTML for a tweet (urls, mentions, hashtags, symbols replaced with links) :param tweet: Tweet object from received from Twitter API :param use_display_url: Use display URL to represent link (ex. google.com, github.com). Default: True :param use_expanded_url: Us

(tweet, use_display_url=True, use_expanded_url=False, expand_quoted_status=False)

Source from the content-addressed store, hash-verified

560
561 @staticmethod
562 def html_for_tweet(tweet, use_display_url=True, use_expanded_url=False, expand_quoted_status=False):
563 """Return HTML for a tweet (urls, mentions, hashtags, symbols replaced with links)
564
565 :param tweet: Tweet object from received from Twitter API
566 :param use_display_url: Use display URL to represent link
567 (ex. google.com, github.com). Default: True
568 :param use_expanded_url: Use expanded URL to represent link
569 (e.g. http://google.com). Default False
570
571 If use_expanded_url is True, it overrides use_display_url.
572 If use_display_url and use_expanded_url is False, short url will
573 be used (t.co/xxxxx)
574
575 """
576 if 'retweeted_status' in tweet:
577 tweet = tweet['retweeted_status']
578
579 if 'extended_tweet' in tweet:
580 tweet = tweet['extended_tweet']
581
582 orig_tweet_text = tweet.get('full_text') or tweet['text']
583
584 display_text_range = tweet.get('display_text_range') or [0, len(orig_tweet_text)]
585 display_text_start, display_text_end = display_text_range[0], display_text_range[1]
586 display_text = orig_tweet_text[display_text_start:display_text_end]
587 prefix_text = orig_tweet_text[0:display_text_start]
588 suffix_text = orig_tweet_text[display_text_end:len(orig_tweet_text)]
589
590 if 'entities' in tweet:
591 # We'll put all the bits of replacement HTML and their starts/ends
592 # in this list:
593 entities = []
594
595 # Mentions
596 if 'user_mentions' in tweet['entities']:
597 for entity in tweet['entities']['user_mentions']:
598 temp = {}
599 temp['start'] = entity['indices'][0]
600 temp['end'] = entity['indices'][1]
601
602 mention_html = '<a href="https://twitter.com/%(screen_name)s" class="twython-mention">@%(screen_name)s</a>' % {'screen_name': entity['screen_name']}
603
604 if display_text_start <= temp['start'] <= display_text_end:
605 temp['replacement'] = mention_html
606 temp['start'] -= display_text_start
607 temp['end'] -= display_text_start
608 entities.append(temp)
609 else:
610 # Make the '@username' at the start, before
611 # display_text, into a link:
612 sub_expr = r'(?<!>)' + orig_tweet_text[temp['start']:temp['end']] + '(?!</a>)'
613 prefix_text = re.sub(sub_expr, mention_html, prefix_text)
614
615 # Hashtags
616 if 'hashtags' in tweet['entities']:
617 for entity in tweet['entities']['hashtags']:
618 temp = {}
619 temp['start'] = entity['indices'][0] - display_text_start

Callers 13

test_basicMethod · 0.80
test_replyMethod · 0.80
test_expanded_urlMethod · 0.80
test_short_urlMethod · 0.80
test_identical_urlsMethod · 0.80
test_symbolsMethod · 0.80
test_no_symbolsMethod · 0.80
test_compatmodeMethod · 0.80
test_extendedmodeMethod · 0.80
test_mediaMethod · 0.80
test_quotedMethod · 0.80

Calls 1

getMethod · 0.80

Tested by 13

test_basicMethod · 0.64
test_replyMethod · 0.64
test_expanded_urlMethod · 0.64
test_short_urlMethod · 0.64
test_identical_urlsMethod · 0.64
test_symbolsMethod · 0.64
test_no_symbolsMethod · 0.64
test_compatmodeMethod · 0.64
test_extendedmodeMethod · 0.64
test_mediaMethod · 0.64
test_quotedMethod · 0.64