Thursday, January 27, 2011

KPF Software Twitter Bot

I am working on an app that basically shows content from a website, in a native app. To do this, I wrote a python script that scrapes the page for content, and then inserts records into the database. I didn't want to mess with a sms gateway or email server to notify me when it's found changes in the original page, so I decided to make the script post to twitter when it updates.

Following this page with few modifications, I was able to get this working with Python 3 and tweepy1.4.

This is what the code to get the pin looks like, two things that changed in python from 2 to 3 is 'raw_input' becomes 'input' and print behaves like a regular function requiring parentheses.

# python code
import tweepy

CONSUMER_KEY = 'key here'
CONSUMER_SECRET = 'secret here'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth_url = auth.get_authorization_url()
print('Please authorize: ' + auth_url)
verifier = input('PIN: ').strip()
auth.get_access_token(verifier)
print("ACCESS_KEY = '%s'" % auth.access_token.key)
print("ACCESS_SECRET = '%s'" % auth.access_token.secret)

From here you should have no problem following Jeff Millers blog post.

@kpfsoftware is where the bot posts, hopefully i'll have the app in the market in the next few days.

And there you have it, a twitter bot using Python 3.

No comments:

Post a Comment