changeset 457ea2c6531c in tryton-tweet:default
details: https://hg.tryton.org/tryton-tweet?cmd=changeset&node=457ea2c6531c
description:
        Post on mastodon too
diffstat:

 requirements.txt |   1 +
 tweet            |  68 +++++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 49 insertions(+), 20 deletions(-)

diffs (93 lines):

diff -r 9e0d01e3d806 -r 457ea2c6531c requirements.txt
--- a/requirements.txt  Thu Oct 28 12:05:37 2021 +0200
+++ b/requirements.txt  Tue May 10 18:13:12 2022 +0200
@@ -1,1 +1,2 @@
+requests
 python-twitter
diff -r 9e0d01e3d806 -r 457ea2c6531c tweet
--- a/tweet     Thu Oct 28 12:05:37 2021 +0200
+++ b/tweet     Tue May 10 18:13:12 2022 +0200
@@ -1,10 +1,12 @@
 #!/usr/bin/env python3
+import configparser
 import os
-import configparser
 import sys
+import urllib.parse
 from itertools import groupby
 from random import choice
 
+import requests
 import twitter
 
 curdir = os.path.dirname(__file__)
@@ -14,24 +16,50 @@
     return line == '\n'
 
 
-data, prefix = {
-    'tip': ('tips', "#TrytonTips "),
-    'success-story': ('success-stories', ""),
-    }[sys.argv[1]]
-tweets = []
-with open(os.path.join(curdir, data), 'r') as fp:
-    for key, lines in groupby(fp, block_separator):
-        if key:
-            continue
-        tweets.append(prefix + ' '.join(map(str.strip, lines)))
-tweet = choice(tweets)
+def choose_text():
+    data, prefix = {
+        'tip': ('tips', "#TrytonTips "),
+        'success-story': ('success-stories', ""),
+        }[sys.argv[1]]
+    tweets = []
+    with open(os.path.join(curdir, data), 'r') as fp:
+        for key, lines in groupby(fp, block_separator):
+            if key:
+                continue
+            tweets.append(prefix + ' '.join(map(str.strip, lines)))
+    return choice(tweets)
+
+
+def tweet(text):
+    config = configparser.ConfigParser()
+    config.read(os.path.join(curdir, 'twitter.cfg'))
 
-config = configparser.ConfigParser()
-config.read(os.path.join(curdir, 'twitter.cfg'))
+    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)
+
+
+def toot(text):
+    config = configparser.ConfigParser()
+    config.read(os.path.join(curdir, 'mastodon.cfg'))
+    token = config.get('tokens', 'access')
 
-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(tweet)
+    requests.post(
+        urllib.parse.urljoin(
+            config.get('instance', 'url'), '/api/v1/statuses'),
+        params={
+            'status': text,
+            },
+        headers={
+            'Authorization': f"Bearer {token}",
+            })
+
+
+
+if __name__ == '__main__':
+    text = choose_text()
+    tweet(text)
+    toot(text)

Reply via email to