Cédric Krier pushed to branch branch/default at Tryton / tweet
Commits: 90309a45 by Cédric Krier at 2023-07-01T13:02:02+02:00 Use RawConfigParser to avoid interpolation - - - - - a4cb4d30 by Cédric Krier at 2023-07-01T13:03:54+02:00 Use home .config directory - - - - - f1ad67a8 by Cédric Krier at 2023-07-01T13:07:04+02:00 Unify configuration files - - - - - a5269496 by Cédric Krier at 2023-07-01T13:08:57+02:00 Use python-twitter-v2 library Closes #2 - - - - - 3144f259 by Cédric Krier at 2023-07-01T13:13:21+02:00 Add rst file extension to README - - - - - 3 changed files: - README → README.rst - requirements.txt - tweet Changes: ===================================== README → README.rst ===================================== @@ -5,6 +5,15 @@ A collection of tweets about Tryton, each tweet is separated by one empty line. The script `tweet` tweets one random tip or success-story. It requires a -configuration file in the same directory named `twitter.cfg` which contains the -credentials to the Twitter account. The file is composed of two sections -`consumer` and `access_token` each containing the two values `key` and `secret`. +configuration file in home `.config` directory named `tweet.cfg` like: + +.. code-block:: ini + + [twitter] + consumer_key = + consumer_secret = + access_token = + access_secret = + [mastodon] + token = + url = ===================================== requirements.txt ===================================== @@ -1,2 +1,2 @@ requests -python-twitter +python-twitter-v2 ===================================== tweet ===================================== @@ -7,6 +7,6 @@ from random import choice import requests -import twitter +import pytwitter curdir = os.path.dirname(__file__) @@ -11,5 +11,7 @@ curdir = os.path.dirname(__file__) +config_path = os.path.expanduser( + os.getenv('XDG_CONFIG_HOME', os.path.join('~', '.config'))) def block_separator(line): @@ -31,6 +33,6 @@ def tweet(text): - config = configparser.ConfigParser() - config.read(os.path.join(curdir, 'twitter.cfg')) + config = configparser.RawConfigParser() + config.read(os.path.join(config_path, 'tweet.cfg')) @@ -36,10 +38,10 @@ - twitter.Api( - consumer_key=config.get('consumer', 'key'), - consumer_secret=config.get('consumer', 'secret'), - access_token_key=config.get('access_token', 'key'), - access_token_secret=config.get('access_token', 'secret') - ).PostUpdate(text) + pytwitter.Api( + consumer_key=config.get('twitter', 'consumer_key'), + consumer_secret=config.get('twitter', 'consumer_secret'), + access_token=config.get('twitter', 'access_token'), + access_secret=config.get('twitter', 'access_secret') + ).create_tweet(text=text) def toot(text): @@ -43,9 +45,9 @@ def toot(text): - config = configparser.ConfigParser() - config.read(os.path.join(curdir, 'mastodon.cfg')) - token = config.get('tokens', 'access') + config = configparser.RawConfigParser() + config.read(os.path.join(config_path, 'tweet.cfg')) + token = config.get('mastodon', 'token') requests.post( urllib.parse.urljoin( @@ -49,7 +51,7 @@ requests.post( urllib.parse.urljoin( - config.get('instance', 'url'), '/api/v1/statuses'), + config.get('mastodon', 'url'), '/api/v1/statuses'), params={ 'status': text, }, View it on Heptapod: https://foss.heptapod.net/tryton/tweet/-/compare/72ac122ef01a8d5146caee376f07f47206f31d5c...3144f2595761ef1fd6ba11a56a14355d3900df1b -- View it on Heptapod: https://foss.heptapod.net/tryton/tweet/-/compare/72ac122ef01a8d5146caee376f07f47206f31d5c...3144f2595761ef1fd6ba11a56a14355d3900df1b You're receiving this email because of your account on foss.heptapod.net.