From: Magnus Hagander Date: Mon, 26 Jun 2023 09:47:15 +0000 (+0200) Subject: Update twitter posting to use api v2 X-Git-Url: http://git.postgresql.org/gitweb/edit?a=commitdiff_plain;h=6e195a482ee346fec51ff35cfe6919ea742d1da0;p=hamn.git Update twitter posting to use api v2 This is the only API version supported by free clients now. Also, the list syncing doesn't work, because that requires a paid client. We don't care about that one for now. --- diff --git a/posttotwitter.py b/posttotwitter.py index dccaaed..528b337 100755 --- a/posttotwitter.py +++ b/posttotwitter.py @@ -13,16 +13,19 @@ Copyright (C) 2009-2019 PostgreSQL Global Development Group import psycopg2 import psycopg2.extensions import configparser -from twitterclient import TwitterClient +import requests_oauthlib # Simple map used to shorten id values to URLs _urlvalmap = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '_'] -class PostToTwitter(TwitterClient): +class PostToTwitter(object): def __init__(self, cfg): - TwitterClient.__init__(self, cfg) + self.tw = requests_oauthlib.OAuth1Session(cfg.get('twitter', 'consumer'), + cfg.get('twitter', 'consumersecret'), + cfg.get('twitter', 'token'), + cfg.get('twitter', 'secret')) psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) self.db = psycopg2.connect(c.get('planet', 'db')) @@ -31,14 +34,14 @@ class PostToTwitter(TwitterClient): """ Actually make a post to twitter! """ - r = self.tw.post('{0}statuses/update.json'.format(self.twitter_api), data={ - 'status': msg, + r = self.tw.post('https://api.twitter.com/2/tweets', json={ + 'text': msg, }) - if r.status_code != 200: - raise Exception("Could not post to twitter, status code {0}".format(r.status_code)) + if r.status_code != 201: + raise Exception("Could not post to twitter, status code {0}, error {1}".format(r.status_code, r.text)) # Return the id of the tweet - return r.json()['id'] + return r.json()['data']['id'] def Run(self): c = self.db.cursor() diff --git a/setuptwitter.py b/setuptwitter.py index 0e98dfc..f3b21a6 100755 --- a/setuptwitter.py +++ b/setuptwitter.py @@ -22,7 +22,7 @@ if not cfg.has_option('twitter', 'consumer') or not cfg.has_option('twitter', 'c print("in the [twitter] section of planet.ini.") sys.exit(1) -oauth = requests_oauthlib.OAuth1Session(cfg.get('twitter', 'consumer'), cfg.get('twitter', 'consumersecret')) +oauth = requests_oauthlib.OAuth1Session(cfg.get('twitter', 'consumer'), cfg.get('twitter', 'consumersecret'), callback_uri='oob') fetch_response = oauth.fetch_request_token('https://api.twitter.com/oauth/request_token') auth_url = oauth.authorization_url('https://api.twitter.com/oauth/authorize')