making a bot with GAE and python
introduction
i tried to make a bot with GAE(Google App Engine), python and Linux. i'll make notes of it.
getting a twitter account and setting a twitter application
set a twitter application
after that, set a twitter application at
sign in as the account that you got. in my case i signed in as @forecasthoge.
click the "Your apps" menu and "Register a new app" to add new application.
- notes
- application type : client application
- access type : read & write
after the registration, you need to get "Consumer key", "Consumer secret", "Access token" and "Access token secret".
you can get "Consumer key" and "Consumer secret" at "application detail".
you can get "Access token" and "Access token secret" at "my access token".
they are used by a bot program.
making a twitter client with python
i refered to http://d.hatena.ne.jp/Number6/20100116/1263631863
install tweepy
i used tweepy that is python twitter library.
you can install it via "easy_install". if you haven't installed "easy_install" yet, you need to install it first.
% sudo apt-get install easy_install
after that, you can install "tweepy" with next command.
% sudo easy_install tweepy
do a test with tweepy
i made "try.py" as a test script of tweepy. set your "consumer key", "consumer secret", "access token" and "access token secret".
# try.py import tweepy consumer_key = 'your consumer key' consumer_secret = 'your consumer secret' access_token = 'your access token' access_token_secret = 'your access token secret' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth_handler=auth) api.update_status('test with tweepy')
execute it.
% python try.py
check the twitter. you can see your post like this if you succeeded.
make a bot
make a bot from try.py.
the features of a bot i made are
- i used forecast api of livedoor, "weather hacks".
- it tweets the forecast of tokyo, japan.
- it parses xml with "xml.dom.minidom"
# main.py from xml.dom import minidom import urllib2 import re import tweepy consumer_key = 'your consumer key' consumer_secret = 'your consumer secret' access_token = 'your access token' access_token_secret = 'your access token secret' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth_handler=auth) xdoc = minidom.parse( urllib2.urlopen( 'http://weather.livedoor.com/forecast/webservice/re st/v1?city=63&day=today' ) ) telop = xdoc.getElementsByTagName( 'telop' )[ 0 ].firstChild.data date = xdoc.getElementsByTagName( 'forecastdate' )[ 0 ].firstChild.data description = re.sub( '\.\.\..*$', '...', xdoc.getElementsByTagName( 'description' )[ 0 ]. firstChild.data ) result = telop + ' ' + date + ' ' + description api.update_status(result)
execute it.
% python main.py
check the twitter. you can see the forecast post like this if you succeeded.
set a bot on GAE
after making a bot, you need to set it on GAE. and you need to get GAE to execute your bot periodically with cron.
getting GAE account
i'll skip explaining how to get GAE account because many explanation pages already exist.
download and set SDK
first, you need to download SDK. you can get here
in my case, i downloaded SDK for linux.
secondary, unzip it and set it at /usr/local
% unzip google_appengine_1.4.3.zip % mv google_appengine /usr/local/google_appengine
just in case, check the GAE SDK tool path with which command.
% which appcfg.py
/usr/local/google_appengine/appcfg.py
register new application for a bot
you need to register new application for a bot.
click the "Create Application". "Application Identifier" and "Application Title" are ok what you like. in my case, i entered "forecasthoge" for both item.
set new application
make directory for your bot on linux. the directory name need to be the same as GAE application name you got the above. and place a bot program(main.py) there.
% mkdir ~/work/forecasthoge % cd ~/work/forecasthoge % mv ~/work/main.py .
and you need to create app.yaml and cron.yaml. app.yaml is the configure file of your GAE application. cron.yaml is the configure file of cron.
i'll skip explaining their detail. if you want to know them, check the documents.
- http://code.google.com/intl/en/appengine/docs/python/config/appconfig.html
- http://code.google.com/intl/en/appengine/docs/python/config/cron.html
i made them like these.
app.yaml
application: forecasthoge version: 1 runtime: python api_version: 1 handlers: - url: /.* script: main.py
cron.yaml
cron: - description: cron job name url: /.* schedule: every 1 hours
your bot will be executed every 1 hour.
a duplicated post will be prevented by twitter. so almost fresh forecast will be tweeted soon after the "Weather hacks" updates. this is truly bad tips. you need not to follow this.
and also you need to prepare "tweepy" for GAE.
( there might be the way to use it without deploying? but i'm not sure the way, so i decided to deploy it. )
there are many way to get tweepy. in my case, i created tweepy clone with git. just only the tweepy direcotry in tweepy is necessary, so copy it in GAE application directory.
% cd ~/work % git clone git://github.com/joshthecoder/tweepy.git % cp -r ./tweepy/tweepy ~/work/forecasthoge/
could be ok to use tweepy that be installed by easy_install ( in my case, /usr/local/lib/python2.6/dist-packages/tweepy-1.7.1-py2.6.egg/tweepy ). but i haven't tried to use it, so i'm not sure yet.
now, your GAE application directory has these files.
after creating necessary files, you need to deploy it. execute appcfg.py.
% cd ~/work/forecasthoge
% appcfg.py update .
your e-mail address and password of GAE will be required. enter them.
you can confirm deployed application at https://appengine.google.com/ .
that's all! if you succeed your bot tweets periodically!
other bots i made
other hint
i worried about cron when i made bot with ruby.
perhaps, GAE can resolve this problem. python GAE application with "urllib2" and your bot with ruby or other language that executed via CGI on the other sever.
python GAE application would be like this.
import urllib2 urllib2.urlopen( 'your bot URL' )
and execute it periodically with cron.yaml.
but i haven't tried it yet. so, i'm not sure yet whether it can work.