Skip to content

Commit

Permalink
Twitter API scripts v1
Browse files Browse the repository at this point in the history
  • Loading branch information
AJSTO committed Aug 21, 2023
0 parents commit 11b97b4
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Twitter API Scripts

This repository contains simple scripts that allow you to interact with the Twitter API to post text tweets and tweets with images.

## Getting started

### Twitter API Setup
Before you can use these scripts, you need to set up a Twitter Developer account and create an application to obtain API keys and secrets. Follow these steps to get your credentials:

#### Create a Twitter Developer Account:
If you don't already have a Twitter Developer account, go to the Twitter Developer Portal and sign up.

#### Create a New Twitter App:
Once you're logged in, click on the "Projects & Apps" tab.
Click the "Create App" button and fill in the necessary information.
After creating the app, navigate to the "Keys and tokens" tab to find your API keys and secrets.
#### Add Keys to Scripts:
In the script files (tweet_text.py and tweet_image.py), replace the placeholders:
- YOUR_API_KEY,
- YOUR_API_SECRET,
- YOUR_ACCESS_TOKEN,
- YOUR_ACCESS_SECRET

with the actual keys and tokens obtained from the Twitter Developer Portal.

## Installing Dependencies
To run the scripts, you need to have Python and few libraries installed. If you haven't installed it yet, you can do so using the following command:
```bash
pip install tweepy requests_oauthlib requests
```
** To use script only for tweeting text you don't need to install tweepy. Tweepy will be helpful with posting images on twitter.

## Using the Scripts

### Tweeting Text Posts
To tweet a text post using the tweet_text.py script, follow these steps:

Open a terminal and navigate to the project directory.
Run the script with the following command:
```bash
python tweet_text.py "Your text here"
```
Replace "Your text here" with the text you want to tweet.

### Tweeting Posts with Images
To tweet an image along with a text caption using the tweet_image.py script, follow these steps:

Place the image you want to tweet in the images directory.
Open a terminal and navigate to the project directory.
Run the script with the following command:
```bash
python tweet_image.py images/your_image_filename.jpg "Your caption here"
```
Replace "Your caption here" with your desired caption and images/your_image_filename.jpg with the actual path to your image file.
38 changes: 38 additions & 0 deletions tweet_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import requests
import re
import sys
import tweepy
from requests_oauthlib import OAuth1

API_KEY = ""
API_KEY_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
API_URL = "https://api.twitter.com/2/tweets"

def tweet_image(image_path, caption):
tweepy_auth = tweepy.OAuth1UserHandler(
API_KEY, API_KEY_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
)
tweepy_api = tweepy.API(tweepy_auth)
post = tweepy_api.simple_upload(image_path)
text = str(post)
media_id = re.search("media_id=(.+?),", text).group(1)
payload = {
"media": {"media_ids": [media_id]},
"text": caption
}

oauth = OAuth1(API_KEY, API_KEY_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
response = requests.post(API_URL, json=payload, auth=oauth)


if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python tweet_image_script.py <image_path> <caption>")
sys.exit(1)

image_path = sys.argv[1]
caption = sys.argv[2]
tweet_image(image_path, caption)

22 changes: 22 additions & 0 deletions tweet_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import requests
from requests_oauthlib import OAuth1
import sys

API_KEY = ""
API_KEY_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
API_URL = "https://api.twitter.com/2/tweets"


def tweet_text(tweet_output):
oauth = OAuth1(API_KEY, API_KEY_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
response = requests.post(API_URL, json={"text": tweet_output}, auth=oauth)

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python tweet_script.py <tweet_text>")
sys.exit(1)

tweet_output = sys.argv[1]
tweet_text(tweet_output)

0 comments on commit 11b97b4

Please sign in to comment.