Ejemplo n.º 1
0
    def parse_tweet(self, tweet) -> Tweet:
        t = Tweet(
            id=tweet.id,
            text=html.unescape(tweet.text),
            created_at=tweet.created_at,
            user_name=tweet.user.name,
            user_screen_name=tweet.user.screen_name,
        )

        if hasattr(tweet, 'retweeted_status'):
            t.text = u'\u267B' + ' @' + tweet.retweeted_status.user.screen_name + ': ' + html.unescape(
                tweet.retweeted_status.text)

        if hasattr(tweet, 'quoted_status'):
            t.text = re.sub(r' https://t\.co/[1-9a-zA-Z]+$', r'',
                            t.text) + "\n"
            t.text += u'\u267B' + ' @' + tweet.quoted_status.user.screen_name + ': ' + html.unescape(
                tweet.quoted_status.text)
            tweet.entities.urls = []
            if 'extended_entities' in tweet.quoted_status:
                self.parse_tweet_media(t,
                                       tweet.quoted_status.extended_entities)

        if hasattr(tweet, 'extended_entities'):
            self.parse_tweet_media(t, tweet.extended_entities)
        elif tweet.entities.urls:
            t.link_url = tweet.entities.urls[0].expanded_url

        for url_entity in tweet.entities.urls:
            expanded_url = url_entity.expanded_url
            indices = url_entity.indices
            display_url = tweet.text[indices[0]:indices[1]]
            t.text = t.text.replace(display_url, expanded_url)

        return t
def parse_tweet(tweet) -> Tweet:
    t = Tweet(
        id=tweet.id,
        text=html.unescape(tweet.full_text),
        created_at=tweet.created_at,
        user_name=tweet.author.name,
        user_screen_name=tweet.author.screen_name,
    )

    if hasattr(tweet, 'retweeted_status'):
        t.text = u'\u267B' + ' @' + tweet.retweeted_status.user.screen_name + ': ' + html.unescape(
            tweet.retweeted_status.full_text)
        if hasattr(tweet.retweeted_status, 'entities'):
            parse_tweet_urls(t, tweet.retweeted_status.entities)
        if hasattr(tweet.retweeted_status, 'extended_entities'):
            parse_tweet_media(t, tweet.retweeted_status.extended_entities)

    if hasattr(tweet, 'quoted_status'):
        t.text = re.sub(r' https://t\.co/[1-9a-zA-Z]+$', r'', t.text) + "\n"
        t.text += u'\u267B' + ' @' + tweet.quoted_status.user.screen_name + ': ' + html.unescape(
            tweet.quoted_status.full_text)
        tweet.entities['urls'] = []
        if hasattr(tweet.quoted_status, 'entities'):
            parse_tweet_urls(t, tweet.quoted_status.entities)
        if hasattr(tweet.quoted_status, 'extended_entities'):
            parse_tweet_media(t, tweet.quoted_status.extended_entities)

    if hasattr(tweet, 'extended_entities'):
        parse_tweet_media(t, tweet.extended_entities)
    elif tweet.entities['urls']:
        t.link_url = tweet.entities['urls'][0]['expanded_url']

    for url_entity in tweet.entities['urls']:
        expanded_url = url_entity['expanded_url']
        indices = url_entity['indices']
        display_url = tweet.full_text[indices[0]:indices[1]]
        t.text = t.text.replace(display_url, expanded_url)

    return t