May 17, 2011

How to make Twitter Bot by Python (Part 1) python-twitter




This is my personal note to know hot to make my Twitter Bot.
First of all, you and I should use python-twitter library. It's easy to use for python user.

Important points for recently Twitter Bot development are below.
- New account for the bot
- Authentication by OAuth. Basic authentication is no longer available



1. Install python-twitter



I hope you already have linux machine which includes python environment. Then, you access the python-twitter library site and read instruction. You have to add a few dependencies before installing python-twitter.

- simplejson
- httplib2
- python-oauth2

I guess you need to get easy_install in order to install them correctly. It is included in setuptools python package.



2. Register Twitter Application




Next, you have to register application and obtain some keys on the Twitter Developers Site.
You can do it by tracing the guide. You will get Consumer Key and Consumer Secret when you finish registering your application on the web site. You can remain the keys anytime on the site.

And then, you have to obtain your Access Key and Access Secret for OAuth on Twitter Application.
It seems that you need to request including Consumer Key and Secret. This python script can help you.

from oauth2 import Client, Token, Consumer 

consumer_key = ''
consumer_secret = ''


def split_parameter(parameters):
    result_list = [tuple(parameter.split('='))
                   for parameter in parameters.split('&')]
    return dict(result_list)

consumer = Consumer(consumer_key, consumer_secret)
client = Client(consumer, None)
result = client.request('http://api.twitter.com/oauth/request_token',
                        'GET')
request_token_map = split_parameter(result[1])
request_token = Token(request_token_map['oauth_token'],
                      request_token_map['oauth_token_secret'])

print 'Please access "http://api.twitter.com/oauth/authorize?oauth_token='+request_token.key+'".'
pin = raw_input('PIN:')
request_token.set_verifier(pin)

client.token = request_token
result = client.request('http://api.twitter.com/oauth/access_token',
                        'POST')

access_token_map = split_parameter(result[1])
print result[1]
print 'User key: '+access_token_map['oauth_token']
print 'User secret: '+access_token_map['oauth_token_secret']
raw_input('Push any key to quit.')
This code by this site. Thanks!



3. Write a sample code




You must get 4 keys. I provide a sample code for test Twitter Application. It will get your friends and followers. Set your keys in this code and run it.


import twitter

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''

def print_users(users):
  for user in users:
    print "[%20s]: %s" % (user.screen_name,user.name)
    api = twitter.Api(
      consumer_key = CONSUMER_KEY,
      consumer_secret = CONSUMER_SECRET,
      access_token_key = ACCESS_KEY,
      access_token_secret = ACCESS_SECRET)

friends = api.GetFriends()
followers = api.GetFollowers()
print_users(friends)
print_users(followers)




References




Twitter From the Command Line in Python Using OAuth
最短 Twitter bot OAuth化対応 (Python編)
python で twitter メモ