Make your own ThienBot (twitter bot)

by thenormalsquid

I wrote a twitter bot in Python to feed me rss syndications of the different news sources I read regularly. I have the script running as a cron job for every 15 minutes. This bot is largely inspired by my friend, Stuart Powers’ bot: @systown. You can follow my bot on twitter: @ThienBot; it posts news about technology, computer science, physics, and finance/economics.

Here’s the source:

import tweepy
import feedparser
import random
#import bitly_api
#import unicodedata
#import sys
#sys.path.append('../')

consumer_secret = ""
consumer_key = ""

access_token = ""
access_token_secret = ""

di = {"http://stackoverflow.com/feeds/tag?tagnames=python":" #python", "http://news.ycombinator.com/rss": " #hackernews #yc", "http://feeds.technologyreview.com/technology_review_energy": " #renewableenergy", "http://feeds2.feedburner.com/hackaday/LgoM":" #hacks #projects", "http://www.eetimes.com/RSS/RSSResult?contentType=1&contentSubtype=SemiNews&classificationGroupId=0": " #semiconductors", "http://rss.slashdot.org/Slashdot/slashdot": " #technews #slashdot", "http://feeds.feedburner.com/oreilly/radar/atom": " #oreillyradar #cs", "http://www.economist.com/feeds/print-sections/79/finance-and-economics.xml":" #finance #economics", "http://phys.org/rss-feed/":" #physics #science"}

def post(msg):
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    status = api.update_status(msg)
    return status

def feed_post(di):
     k = random.choice(di.keys())
     d = feedparser.parse(k)
     e = random.randint(0, len(d.entries)-1)
     return post(d.entries[e].title+"\n"+d.entries[e].link+"\n"+di[k])

feed_post(di)

Be sure to make a twitter account first and get on dev.twitter.com and setup your app. Allow read and write access and generate your auth keys. Basically, just plugin your api keys, add whatever url you want to parse into the dictionary as a key and add the hashtag keyword as the value. The feed_post() function will assign a random url from the dict to a variable, then feedparser will parse the url and place the elements in a dict. We just need to get random entries from that particular syndicate and return the post with the title, link, and hashtag keywords.

About these ads