How do I stop receiving hashtags as links from Twitter?

喜欢而已 提交于 2019-12-11 16:21:12

问题


I wanted a Twitter forwarder to Telegram.

I found this one: https://github.com/franciscod/telegram-twitter-forwarder-bot

The problem is now, that if a tweet contains a hashtag before a link, Telegram show me the link to the hashtag.

I tried different things and searched about that, but I don't know how to only receive plain text from twitter.

Also I don't get the short link t.co if the tweet is to long. It's just a long link.

for tweet in tweets:
    self.logger.debug("- Got tweet: {}".format(tweet.text))

    # Check if tweet contains media, else check if it contains a link to an image
    extensions = ('.jpg', '.jpeg', '.png', '.gif')
    pattern = '[(%s)]$' % ')('.join(extensions)
    photo_url = ''
    tweet_text = html.unescape(tweet.text)
    if 'media' in tweet.entities:
        photo_url = tweet.entities['media'][0]['media_url_https']
    else:
        for url_entity in tweet.entities['urls']:
            expanded_url = url_entity['expanded_url']
            if re.search(pattern, expanded_url):
                photo_url = expanded_url
                break
    if photo_url:
        self.logger.debug("- - Found media URL in tweet: " + photo_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]]
        tweet_text = tweet_text.replace(display_url, expanded_url)

    tw_data = {
        'tw_id': tweet.id,
        'text': tweet_text,
        'created_at': tweet.created_at,
        'twitter_user': tw_user,
        'photo_url': photo_url,
    }
    try:
        t = Tweet.get(Tweet.tw_id == tweet.id)
        self.logger.warning("Got duplicated tw_id on this tweet:")
        self.logger.warning(str(tw_data))
    except Tweet.DoesNotExist:
        tweet_rows.append(tw_data)

    if len(tweet_rows) >= self.TWEET_BATCH_INSERT_COUNT:
        Tweet.insert_many(tweet_rows).execute()
        tweet_rows = []

回答1:


Just disable markdown_twitter_hashtags() function, make it return text without replace that.



来源:https://stackoverflow.com/questions/47366980/how-do-i-stop-receiving-hashtags-as-links-from-twitter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!