Skip to content

Instantly share code, notes, and snippets.

@goerz
Created August 10, 2009 01:38
Show Gist options
  • Save goerz/164973 to your computer and use it in GitHub Desktop.
Save goerz/164973 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from time import strftime
import sqlite3
import sys
import twitter #http://code.google.com/p/python-twitter/
import feedparser #available at feedparser.org
DATABASE = "/Users/goerz/.twitter_bot/tweets.sqlite"
TWITTER_USER = "username"
TWITTER_PASSWORD = "secret"
def print_stats():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
c = conn.cursor()
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 = row['url']
c.execute('UPDATE RSSContent SET `short_url`=? WHERE `url`=?',(short_url,row['url']))
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)
d = feedparser.parse(url)
for entry in d.entries:
#check for private
if "?key=" in entry.link:
continue
#check for duplicates
c.execute('select * from RSSContent where url=?', (entry.link,))
if not c.fetchall():
tweet_text = entry.title
shortened_link = 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)
if len(tweet_text) + len(shortened_link) > 137:
tweet_text = tweet_text[:137-len(shortened_link)]
print "%s.. %s" % (tweet_text, shortened_link)
api.PostUpdate("%s.. %s" % (tweet_text, shortened_link))
conn.commit()
if __name__ == '__main__':
tweet_rss(sys.argv[1])
print_stats()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment