update mainly to fix twitter
[pdt.git] / t.py
diff --git a/t.py b/t.py
index de8b9e88c004f3679212ea791aeebdd67510e855..9148987f8422ae377a6034aa69a476b9c2e35705 100755 (executable)
--- a/t.py
+++ b/t.py
@@ -1,59 +1,84 @@
 #!/usr/bin/env python3
+# pdt: post to FSF social media via command line
+# Copyright (C) 2021 Ian Kelling
+# SPDX-License-Identifier: AGPL-3.0-or-later
 
-# based on
-# https://github.com/python-twitter-tools/twitter
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+# Usage: see pdt.sh example.
 
 import sys
-import os
-from twitter import *
-import fileinput
-
-# if needed for development, just hardcode
-#oauth_token = ""
-#oauth_secret = ""
-
-
-first = True
-with fileinput.input(files=(os.environ.get('HOME') + os.sep + '.rainbow_oauth')) as f:
-    # ok, this is dumb, but it works
-    for line in f:
-        if first:
-            oauth_token = line.rstrip()
-            first = False
-        else:
-            oauth_secret = line.rstrip()
-
-CONSUMER_KEY = 'hJHyPhuU7nSVHrKTVsGVDM4Lw'
-CONSUMER_SECRET = '8UCYePqc1y9DY6mg0yQzLoTuq57AIysype2Si63714uACGMCbO'
-
-t = Twitter(auth=OAuth(oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))
-
-tweet_text = input()
-have_image = False
-try:
-    image_path = input()
-    have_image = True
-except:
-    pass
-
-have_alt = False
-try:
-    alt = input()
-    have_alt = True
-except:
-    pass
-
-if have_image:
-    with open(image_path, "rb") as imagefile:
-        imagedata = imagefile.read()
-    t_upload = Twitter(domain='upload.twitter.com',
-        auth=OAuth(oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))
-    id_img = t_upload.media.upload(media=imagedata)["media_id_string"]
-
-if have_alt:
-    t_upload.media.metadata.create(media_id=id_img, text=alt)
-
-if have_image:
-    t.statuses.update(status=tweet_text, media_ids=id_img)
+import tweepy
+import time
+from twitter_keys import *
+
+
+auth = tweepy.OAuth1UserHandler(
+    consumer_key, consumer_secret, access_token, access_token_secret
+)
+
+api = tweepy.API(auth)
+
+client = tweepy.Client(
+    consumer_key=consumer_key, consumer_secret=consumer_secret,
+    access_token=access_token, access_token_secret=access_token_secret
+)
+
+if len(sys.argv) == 2:
+    response = client.delete_tweet(sys.argv[1])
 else:
-    t.statuses.update(status=tweet_text)
+    input_text = input()
+
+    posts=input_text.split("/tnt/")
+    post_count=len(posts)
+
+    tweet_text=posts[0].strip()
+
+    if post_count > 1:
+        tweet_text=tweet_text + f" 1/{post_count}"
+
+    have_image = False
+    try:
+        image_path = input()
+        have_image = True
+    except:
+        pass
+
+    have_alt = False
+    try:
+        alt = input()
+        have_alt = True
+    except:
+        pass
+    if have_image:
+        media = api.media_upload(image_path)
+        media_id = media.media_id_string
+
+    if have_alt:
+        api.create_media_metadata(media_id, alt)
+
+
+    if have_image:
+        response = client.create_tweet(text=tweet_text, media_ids=[media_id])
+    else:
+        #print(tweet_text)
+        response = client.create_tweet(text=tweet_text)
+    print(f"https://nitter.net/user/status/{response.data['id']}")
+    if post_count > 1:
+        for x in range(1, post_count):
+            time.sleep(1)
+            tweet_text=posts[x].strip() + f" {x+1}/{post_count}"
+            response = client.create_tweet(text=tweet_text, in_reply_to_tweet_id=response.data['id'])
+            print(f"{x+1}/{post_count} https://nitter.net/user/status/{response.data['id']}")
+            #print(tweet_text)