RSS Twitter Bot in Python
I was a little bored today and decided to write up a simple script that pushes RSS feed information out to Twitter and manages to keep track of the history so that tweets are not sent out more than once.
It was actually a very trivial little script to write but it could actually be useful for something that I’m working on in the future.
The script makes use of an Sqlite database to store history and bit.ly for shortening URLs. I’ve made heavy use of some really nice open source libraries to make for a very short and sweet little script.
Grab the necessary python libraries:
python-twitter
python-bitly
feedparser
You’ll need to sign up for free accounts at Twitter and Bit.ly to use this script.
Hopefully someone out there can take this code example to do something really cool with Twitter and Python.
Update: I’ve added some bit.ly link tracking output to this script. After it twitters the RSS feed it will print out the click count information for every bit.ly link.
from time import strftime import sqlite3 import twitter #http://code.google.com/p/python-twitter/ import bitly #http://code.google.com/p/python-bitly/ import feedparser #available at feedparser.org DATABASE = "tweets.sqlite" BITLY_LOGIN = "bitlyUsername" BITLY_API_KEY = "api key" TWITTER_USER = "username" TWITTER_PASSWORD = "password" def print_stats(): conn = sqlite3.connect(DATABASE) conn.row_factory = sqlite3.Row c = conn.cursor() b = bitly.Api(login=BITLY_LOGIN,apikey=BITLY_API_KEY) c.execute('SELECT title, url, short_url from RSSContent') all_links = c.fetchall() for row in all_links: short_url = row['short_url'] if short_url is None: short_url = b.shorten(row['url']) c.execute('UPDATE RSSContent SET `short_url`=? WHERE `url`=?',(short_url,row['url'])) stats = b.stats(short_url) print "%s - User clicks %s, total clicks: %s" % (row['title'], stats.user_clicks,stats.total_clicks) conn.commit() def tweet_rss(url): conn = sqlite3.connect(DATABASE) conn.row_factory = sqlite3.Row c = conn.cursor() #create the table if it doesn't exist c.execute('CREATE TABLE IF NOT EXISTS RSSContent (`url`, `title`, `dateAdded`, `content`, `short_url`)') api = twitter.Api(username=TWITTER_USER, password=TWITTER_PASSWORD) b = bitly.Api(login=BITLY_LOGIN,apikey=BITLY_API_KEY) d = feedparser.parse(url) for entry in d.entries: #check for duplicates c.execute('select * from RSSContent where url=?', (entry.link,)) if not c.fetchall(): tweet_text = "%s - %s" % (entry.title, entry.summary) shortened_link = b.shorten(entry.link) t = (entry.link, entry.title, strftime("%Y-%m-%d %H:%M:%S", entry.updated_parsed), entry.summary, shortened_link) c.execute('insert into RSSContent (`url`, `title`,`dateAdded`, `content`, `short_url`) values (?,?,?,?,?)', t) print "%s.. %s" % (tweet_text[:115], shortened_link) api.PostUpdate("%s.. %s" % (tweet_text[:115], shortened_link)) conn.commit() if __name__ == '__main__': tweet_rss('http://www.halotis.com/feed/') print_stats()


Thank you very much for sharing this script!
works like a charm =)
Greetings from Chile!
Thanks for giving it a try.
I’m glad it worked well for you.
Solid work! I ended up here after googling looking for a python script to email me all my @ replies, but this is cool too. Love the bit.ly integration.
oh man sweet. i was about 80% into implementing a similar script, and the only part i had left was the queueing system. i was dreading writing my own system, so i googled ‘twitter post queue’ and found your post. thanks!
i’m going to try to integrate your queues into my software: http://code.google.com/p/chpcad/source/browse/bin/chpcad.py
[...] you’d like to setup a twitter bot using Python, Halotis.com has a great tutorial that uses a sqlite backend to keep track of what it’s messaged. For a [...]
Hi,
This looks like a great script–thanks for sharing! I am a Python newbie, but managed to get a couple of your other Python scripts running. I’m using TextWrangler on my Mac to run the scripts, and first tried running them in Python 3,but just switched to 2.6.2, which seemed to do the trick. However, I’m having a bit of trouble with this one; here are the errors I’m getting:
/Users/gdsaxton/Downloads/RSS_Twitter-feed.py:76: Traceback (most recent call last): tweet_rss(‘http://twitter.com/QualityStocks‘)
/Users/gdsaxton/Downloads/RSS_Twitter-feed.py:52: AttributeError: ‘module’ object has no attribute ‘Api’
Any advice?
hmm.. looks like it is probably the twitter library. There’s a couple of different ones out there. It’s possible that I have used a different library than you have. try downloading the one from http://code.google.com/p/python-twitter/ and putting the twitter.py file in the same directory as your RSS_Twitter-feed.py script.
Thanks–that did the trick! This may have been a newbie mistake, but I just placed the RSS_Twitter-feed.py script in my Downloads directory and was running it from there; as soon as I copied and pasted twitter.py into the same directory the whole thing worked.
That leads me to a follow-up question, if you don’t mind. :) What is a better place to put the scripts I’ll be executing, like this one? Do I put them in the same place as my python path?
Thanks in advance for your help.
there’s no hard rules for how to organize these files. But personally I do all my development in my Dropbox directory. I’m constantly switching between 3 or 4 different computers and I like for the scripts I write to work on all of them so I put all my code and all 3rd party libraries in there as well. Then I don’t have to install each library on all the computers separately. As long as I have the same version of Python installed everything just tends to work.
A more advanced option is to use virtualenv to create separate locations for code. I haven’t used it but it will allow you to more easily create work spaces for projects for different versions of Python, with different versions of libraries available.
Thanks, Matt!
Using your example, I’ve written a python script to grab your Google Buzz feed (as detailed in the Buzz API), and automatically post your Buzz-es to Twitter. It includes a link back to the original Buzz URL (shortened with Bit.ly) It also uses a local sqlite database to store previous posts, and print bit.ly statistics for your published links.
Google Buzz -> Twitter Export