minor: docs
[pdt.git] / t.py
CommitLineData
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 22import sys
03c21027 23import time
026344f1 24import tweepy
03c21027
IK
25from 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 ##
33verbose = False
34if 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
40auth = tweepy.OAuth1UserHandler(
41 consumer_key, consumer_secret, access_token, access_token_secret
42)
03c21027 43api = tweepy.API(auth)
03c21027
IK
44client = 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
51if 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
68input_text = input()
69posts=input_text.split("/tnt/")
70post_count=len(posts)
03c21027 71
9dcc7882 72tweet_text=posts[0].strip()
03c21027 73
9dcc7882
IK
74if post_count > 1:
75 tweet_text=tweet_text + f" 1/{post_count}"
03c21027 76
9dcc7882
IK
77have_image = True
78try:
79 image_path = input()
80except EOFError:
81 have_image = False
026344f1 82
9dcc7882
IK
83have_alt = True
84try:
85 alt = input()
86except EOFError:
87 have_alt = False
03c21027 88
9dcc7882
IK
89if have_image:
90 media = api.media_upload(image_path)
91 media_id = media.media_id_string
03c21027 92
9dcc7882
IK
93if have_alt:
94 api.create_media_metadata(media_id, alt)
03c21027 95
9dcc7882
IK
96
97if have_image:
98 response = client.create_tweet(text=tweet_text, media_ids=[media_id])
99else:
100 response = client.create_tweet(text=tweet_text)
101 if verbose:
c8c18954 102 print(response.data['id'])
9dcc7882
IK
103if 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'])