Python Mini Series: How to create twitter tweets from python

In order to automatically create Twitter tweets for new blog post entries on my side, I created a python script to handle this.
The script reads the metadata of the post and looks for the tweet-related context and creates a tweet. After the tweet is created the metadata is updated with the tweet id.

This is really easy to automate with python. Find below the details on how to do it.

To create the python twitter automation I followed the steps:

  • Install python-twitter package.

    This package wraps the twitter rest API and makes it easily callable via python.
  • Create a Twitter app and get your keys.
    • Create App
    • Set the callback url to http:127.0.0.1:8080. This should work for most cases.
  • Read data you want to pass to the API in your script
  • Call api

Let's go through the code:

  1. Import the twitter API wrapper

    import twitter
  2. set the keys you got during the creation of the Twitter app. The API requires the keys in order to identify you.
try:
consumerKey = <Your key that you got after creating the twitter app>
consumerSecret = <Your key that you got after creating the twitter app>
tokenKey = <Your key that you got after creating the twitter app>
access_token_secret = <Your key that you got after creating the twitter app>
  1. create an instance of the api
api = twitter.Api(consumer_key=consumerKey,
                  consumer_secret=consumerSecret,
                  access_token_key=tokenKey,
                  access_token_secret=tokenSecret)
  1. Create a tweet
#create a tweet / twitter status
twitterPost = api.PostUpdate(
    status = "Yay this is an automated tweet. #python"
)
  1. work with the result. In my case, I print it and store the ID.
print(twitterPost)
tweetId = str(twitterPost.id)
except Exception as e:
print(str(e))

The full code example below:

import twitter
try:
consumerKey = <Your key that you got after creating the twitter app>
consumerSecret = <Your key that you got after creating the twitter app>
tokenKey = <Your key that you got after creating the twitter app>
access_token_secret = <Your key that you got after creating the twitter app>
    api = twitter.Api(consumer_key=consumerKey,
                      consumer_secret=consumerSecret,
                      access_token_key=tokenKey,
                      access_token_secret=tokenSecret)

    #create a tweet / twitter status

    twitterPost = api.PostUpdate(
        status = "Yay this is an automated tweet. #python"
    )
    print(twitterPost)
    tweetId = str(twitterPost.id)
except Exception as e:
    print(str(e))

Just creating a post is usually not very useful. After all, you can do this with the app as well. However, with the API you can now automate tweet creation.
I used it on my blog to automatically create a new tweet every time I commit a new post to my GitHub repository.
If you are interested in the complete code, have a look at the repo here.

Let me know if you have any cool ideas on how to automate Twitter!

Jan 2nd 22


No one left a comment yet. Be the first!


Leave a comment

We'll never share your email with anyone else.
>>>
Slide to submit