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

consumer_secret = ""
consumer_key = ""

access_token = ""
access_token_secret = ""

di = {"":" #python", "": " #hackernews #yc", "": " #renewableenergy", "":" #hacks #projects", "": " #semiconductors", "": " #technews #slashdot", "": " #oreillyradar #cs", "":" #finance #economics", "":" #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])


Be sure to make a twitter account first and get on 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