From a2bf4189e74af25f5736766fb6f538d6dc8001f1 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Wed, 6 Apr 2022 10:46:12 +1000 Subject: [PATCH] twitter: switch to tweepy python-twitter has stopped working after Twitter upstream removed an endpoint it was querying. Switch to the better maintained tweepy, which is basically the same. tweepy doesn't automatically split tweets, so implement that. Split tweets will now be in reply to each other, which is a feature. Change-Id: Id40af0e314ce8a6cb542f6138f5deaa8a587b260 --- requirements.txt | 2 +- statusbot/bot.py | 32 +++++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/requirements.txt b/requirements.txt index ad81374..f214bc2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,5 +4,5 @@ simplemediawiki>=1.2.0b2 irc python-daemon kitchen -python-twitter>=3.4 +tweepy ib3 diff --git a/statusbot/bot.py b/statusbot/bot.py index 6801083..a795d37 100755 --- a/statusbot/bot.py +++ b/statusbot/bot.py @@ -69,7 +69,8 @@ import simplemediawiki import datetime import re import requests -import twitter +import tweepy +import textwrap import urllib try: @@ -244,14 +245,31 @@ class Tweet(UpdateInterface): self.consumer_secret = config.get('twitter', 'consumer_secret') self.access_token_key = config.get('twitter', 'access_token_key') self.access_token_secret = config.get('twitter', 'access_token_secret') - self.api = twitter.Api( - consumer_key=self.consumer_key, - consumer_secret=self.consumer_secret, - access_token_key=self.access_token_key, - access_token_secret=self.access_token_secret) + + auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret) + auth.set_access_token(self.access_token_key, self.access_token_secret) + + self.api = tweepy.API(auth) def update(self, msg): - self.api.PostUpdates(msg, continuation=u'\u2026') + # NOTE(ianw) python-twitter, used originally, used to do some + # automated splitting. It didn't account for emoji, + # link-shortening, etc. Ergo, this could be better, but KISS + # for now + tweets = textwrap.wrap(msg, 270) + if len(tweets) > 1: + for i in range(0, len(tweets)): + tweet = tweets[i] + tweets[i] = tweet + " %d/%d" % (i+1, len(tweets)) + + last_tweet = None + for tweet in tweets: + if not last_tweet: + last_tweet = self.api.update_status(status=tweet) + else: + last_tweet = self.api.update_status(status=tweet, + in_reply_to_status_id = last_tweet.id, + auto_populate_reply_metadata=True) def alert(self, msg=None): self.update(msg)