File size: 2,528 Bytes
6370672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import argparse
import json
import logging
import os

import tweepy
from dotenv import load_dotenv
from tweet_counter import count_tweet

load_dotenv()


CONSUMER_KEY = os.environ["CONSUMER_KEY"]
CONSUMER_SECRET = os.environ["CONSUMER_SECRET"]
ACCESS_KEY = os.environ["ACCESS_KEY"]
ACCESS_SECRET = os.environ["ACCESS_SECRET"]


# Authenticate to Twitter
client = tweepy.Client(
    consumer_key=CONSUMER_KEY,
    consumer_secret=CONSUMER_SECRET,
    access_token=ACCESS_KEY,
    access_token_secret=ACCESS_SECRET,
)
auth = tweepy.OAuth1UserHandler(
    CONSUMER_KEY,
    CONSUMER_SECRET,
    ACCESS_KEY,
    ACCESS_SECRET,
)

# Create API object
api = tweepy.API(auth, wait_on_rate_limit=True)


logging.basicConfig(handlers=[logging.StreamHandler()], level=logging.INFO)
logger = logging.getLogger(__name__)


def tweet_thread(thread_data, base_path):
    for index, tweet in enumerate(thread_data, start=1):
        tweet_length = count_tweet(tweet["content"])
        if tweet_length > 280:
            raise ValueError(
                f"Tweet number {index} exceeds 280 characters by {tweet_length - 280}. Content: {tweet['content']}"
            )

    # Posting the thread
    previous_tweet_id = None
    for tweet_data in thread_data:
        if "media" in tweet_data and tweet_data["media"]:
            media_ids = [
                api.media_upload(os.path.join(base_path, media["path"])).media_id
                for media in tweet_data["media"]
            ]
        else:
            media_ids = None

        # Post tweet
        if previous_tweet_id is None:
            # First tweet of the thread
            tweet = client.create_tweet(text=tweet_data["content"], media_ids=media_ids)
        else:
            # Subsequent tweets in the thread
            tweet = client.create_tweet(
                text=tweet_data["content"],
                in_reply_to_tweet_id=previous_tweet_id,
                media_ids=media_ids,
            )

        previous_tweet_id = tweet.data["id"]
        logger.info(f"Tweeted: {tweet_data['content']}")

    logger.info("Thread posted!")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Tweet a thread from a json file.")
    parser.add_argument(
        "file", type=str, help="Path to the json file containing the thread data."
    )
    args = parser.parse_args()

    with open(args.file, "r") as f:
        thread_data = json.load(f)

    base_path = os.path.dirname(os.path.abspath(args.file))
    tweet_thread(thread_data, base_path)