minor: docs
[pdt.git] / t.py
1 #!/usr/bin/env python3
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
5
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.
20
21 """ for sys.argv """
22 import sys
23 import time
24 import tweepy
25 from twitter_keys import *
26
27 # for debugging:
28 # import logging
29 # logging.basicConfig(level=logging.DEBUG)
30
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 ##
40 auth = tweepy.OAuth1UserHandler(
41 consumer_key, consumer_secret, access_token, access_token_secret
42 )
43 api = tweepy.API(auth)
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 )
48 ## End setup api ##
49
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)
63 sys.exit(0)
64
65 #### For the rest of the script:: no action specified in cli arguments,
66 #### post based on input from STDIN
67
68 input_text = input()
69 posts=input_text.split("/tnt/")
70 post_count=len(posts)
71
72 tweet_text=posts[0].strip()
73
74 if post_count > 1:
75 tweet_text=tweet_text + f" 1/{post_count}"
76
77 have_image = True
78 try:
79 image_path = input()
80 except EOFError:
81 have_image = False
82
83 have_alt = True
84 try:
85 alt = input()
86 except EOFError:
87 have_alt = False
88
89 if have_image:
90 media = api.media_upload(image_path)
91 media_id = media.media_id_string
92
93 if have_alt:
94 api.create_media_metadata(media_id, alt)
95
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:
102 print(response.data['id'])
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'])
108 if verbose:
109 print(response.data['id'])