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()
Bookmark and Share

Technorati Tags: , , , , , , , , , ,

Stumble it!


RSS feed | Trackback URI

15 Comments »

Comment by gimpy21
2009-07-01 12:42:38

Down voted due to full page pop up ad.

This comment was originally posted on Reddit

 
Comment by f4nt
2009-07-01 14:24:34

Same here. Talk about intrusive.

This comment was originally posted on Reddit

 
Comment by Alejo
2009-07-02 17:05:01

Thank you very much for sharing this script!
works like a charm =)

Greetings from Chile!

Comment by Matt Warren
2009-07-02 17:21:21

Thanks for giving it a try.
I’m glad it worked well for you.

 
 
Comment by adamzap
2009-07-04 12:12:41

Cool stuff, but reading this code shows me how much of a PEP8 purist I’ve become.

This comment was originally posted on Reddit

 
Comment by kortina
2009-07-08 09:08:50

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.

 
Comment by gba Subscribed to comments via email
2009-07-12 17:20:25

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

 
2009-07-14 10:20:00

[...] 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 [...]

 
Comment by mgoerz
2009-08-09 20:41:22

Modified #twitter #bot from http://bit.ly/sxmlK to http://bit.ly/XXcZ5 for auto-tweeting my public snipt.net #snippets

This comment was originally posted on Twitter

 
Comment by Gregory Saxton Subscribed to comments via email
2009-08-17 13:49:21

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?

 
Comment by Matt Warren
2009-08-17 14:22:37

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.

Comment by Gregory Saxton Subscribed to comments via email
2009-08-17 14:29:27

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.

 
 
Comment by Matt Warren
2009-08-17 14:51:41

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.

Comment by Gregory Saxton Subscribed to comments via email
2009-08-17 14:55:58

Thanks, Matt!

 
 
Comment by zhup
2009-09-02 02:18:40

[links] RSS Twitter Bot in Python http://bit.ly/1MxQ0O

This comment was originally posted on Twitter

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped=""> in your comment.



Additional comments powered by BackType