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
This commit is contained in:
Ian Wienand 2022-04-06 10:46:12 +10:00
parent 067bc37ec0
commit a2bf4189e7
2 changed files with 26 additions and 8 deletions

View File

@ -4,5 +4,5 @@ simplemediawiki>=1.2.0b2
irc
python-daemon
kitchen
python-twitter>=3.4
tweepy
ib3

View File

@ -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)