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. In case you want to know more about how I use it for my site have a look at this post, where I write about how I automated my github workflow to create tweets and embed them as comment section.

To create the python twitter automation I followed the steps:

  • Install [python-twitter][1] package.
    This package wraps the twitter rest api and makes it easily callable via python.
  • Create a twitter app and get your keys. Step by step description [here][2]
    • [Create App][3]
    • 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))

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 everytime I commit a new post to my github repository. If you are interested in the complete code, have a look at the repo here. In case you are interested in how I automated my git workflow and generated the tweets have a look at this post.

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