Commit | Line | Data |
---|---|---|
f9ace606 | 1 | #!/usr/bin/env python3 |
03c21027 IK |
2 | # pdt: post to FSF social media via command line |
3 | # Copyright (C) 2021 Ian Kelling | |
4 | # SPDX-License-Identifier: AGPL-3.0-or-later | |
f9ace606 | 5 | |
03c21027 IK |
6 | # This program is free software: you can redistribute it and/or modify |
7 | # it under the terms of the GNU Affero General Public License as published by | |
8 | # the Free Software Foundation, either version 3 of the License, or | |
9 | # (at your option) any later version. | |
10 | ||
11 | # This program is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU Affero General Public License for more details. | |
15 | ||
16 | # You should have received a copy of the GNU Affero General Public License | |
17 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | |
18 | ||
19 | # Usage: see pdt.sh example. | |
f9ace606 | 20 | |
9dcc7882 | 21 | """ for sys.argv """ |
f9ace606 | 22 | import sys |
03c21027 | 23 | import time |
026344f1 | 24 | import tweepy |
03c21027 IK |
25 | from twitter_keys import * |
26 | ||
026344f1 IK |
27 | # for debugging: |
28 | # import logging | |
29 | # logging.basicConfig(level=logging.DEBUG) | |
03c21027 | 30 | |
9dcc7882 IK |
31 | |
32 | ## begin cli option parsing ## | |
33 | verbose = False | |
34 | if len(sys.argv) == 2 and sys.argv[1] == "-v": | |
35 | verbose = True | |
36 | ## end cli option parsing ## | |
37 | ||
38 | ||
39 | ## Begin setup api ## | |
03c21027 IK |
40 | auth = tweepy.OAuth1UserHandler( |
41 | consumer_key, consumer_secret, access_token, access_token_secret | |
42 | ) | |
03c21027 | 43 | api = tweepy.API(auth) |
03c21027 IK |
44 | client = tweepy.Client( |
45 | consumer_key=consumer_key, consumer_secret=consumer_secret, | |
46 | access_token=access_token, access_token_secret=access_token_secret | |
47 | ) | |
9dcc7882 | 48 | ## End setup api ## |
026344f1 | 49 | |
026344f1 IK |
50 | |
51 | if len(sys.argv) == 3: | |
52 | if sys.argv[1] == '--delete': | |
53 | client.delete_tweet(sys.argv[2]) | |
54 | elif sys.argv[1] == '--pin': | |
55 | api.pin_tweet(sys.argv[2]) | |
56 | elif sys.argv[1] == '--unpin': | |
57 | api.unpin_tweet(sys.argv[2]) | |
58 | elif sys.argv[1] == '--banner': | |
59 | api.update_profile_banner(sys.argv[2]) | |
60 | else: | |
61 | print("t.py: error: unexpected argument. exiting without doing anything") | |
62 | sys.exit(1) | |
9dcc7882 | 63 | sys.exit(0) |
03c21027 | 64 | |
9dcc7882 IK |
65 | #### For the rest of the script:: no action specified in cli arguments, |
66 | #### post based on input from STDIN | |
03c21027 | 67 | |
9dcc7882 IK |
68 | input_text = input() |
69 | posts=input_text.split("/tnt/") | |
70 | post_count=len(posts) | |
03c21027 | 71 | |
9dcc7882 | 72 | tweet_text=posts[0].strip() |
03c21027 | 73 | |
9dcc7882 IK |
74 | if post_count > 1: |
75 | tweet_text=tweet_text + f" 1/{post_count}" | |
03c21027 | 76 | |
9dcc7882 IK |
77 | have_image = True |
78 | try: | |
79 | image_path = input() | |
80 | except EOFError: | |
81 | have_image = False | |
026344f1 | 82 | |
9dcc7882 IK |
83 | have_alt = True |
84 | try: | |
85 | alt = input() | |
86 | except EOFError: | |
87 | have_alt = False | |
03c21027 | 88 | |
9dcc7882 IK |
89 | if have_image: |
90 | media = api.media_upload(image_path) | |
91 | media_id = media.media_id_string | |
03c21027 | 92 | |
9dcc7882 IK |
93 | if have_alt: |
94 | api.create_media_metadata(media_id, alt) | |
03c21027 | 95 | |
9dcc7882 IK |
96 | |
97 | if have_image: | |
98 | response = client.create_tweet(text=tweet_text, media_ids=[media_id]) | |
99 | else: | |
100 | response = client.create_tweet(text=tweet_text) | |
101 | if verbose: | |
c8c18954 | 102 | print(response.data['id']) |
9dcc7882 IK |
103 | if post_count > 1: |
104 | for x in range(1, post_count): | |
105 | time.sleep(1) | |
106 | tweet_text=posts[x].strip() + f" {x+1}/{post_count}" | |
107 | response = client.create_tweet(text=tweet_text, in_reply_to_tweet_id=response.data['id']) | |
026344f1 | 108 | if verbose: |
c8c18954 | 109 | print(response.data['id']) |