Python to Twitter
This post assumes you know how to install Python and submodules for Python.
I have been working on a script that will allow me to post a link to Twitter when I write a blog post here.
I have used IFTTT for things like this in the past, but I have never been happy with that. Technically IFTTT works but I have had issues, double posts, posting the wrong thing, etc. So I wanted to figure out away to do this myself.
And of course I turned to Python for this.
I started by trying to find a module that would allow me to easily post to Twitter. I found a module called Twython, it was fairly easy to set up: Something like this:
You can create an ‘app’ and create the needed credentials by going to https://apps.twitter.com
Next I wanted to use my bit.ly account to create a ‘short link’(this is in air quotes because my short link is www2.ki6bjv.com/xxxxxxxx because I haven’t figured out a shorter url that I am happy with yet. 😏)
Awesome thing about Bit.ly is that they have an API, man I love API’s. So I didn’t need to go find a module for Python I already had one that i used before that is awesome……Requests. To implement requests you do something like this:
Finally I needed to be able to read and parse the RSS feed from the blog so that I could get the latest post so that we can shorten the link and and post it to Twitter.
I needed another Python module for this and when i wen searching I found Feedparser. Again the implementation goes like this:
So when I finally put all this together we get something like this:
Couple of things are missing before I would consider this completely ready for production. For one there is no erroring handling at all. I should also be keeping track of the title of each blog post to make sure I am not spamming twitter with the same blog post over and over again.
those are going to be my steps…..