[GAE][Python] Google Apps Engine から Twitterを更新してみた。
五日目のPython。
Twitterを更新する部分としてModelを書いてみた。
基本的にGoogle Apps Engine Oilから出力したModelに手を入れてみた。
from google.appengine.ext import db from gaeo.model import BaseModel from google.appengine.api import urlfetch from base64 import b64encode import urllib class Twitter(BaseModel): def __init__(self): self.url = 'http://twitter.com/statuses/%s.%s' self.username = "username" self.password = "password" self.credential = 'Basic %s' % b64encode( '%s:%s' % (self.username, self.password)) def update(self, status): url = self.url % ('update', 'xml') form_fields = { "status" : status.encode('utf-8') } result = urlfetch.fetch( url = url, payload = urllib.urlencode(form_fields), method = urlfetch.POST, headers = {'Authorization' : self.credential }, allow_truncated = 'false', follow_redirects = 'true' ) return result.content
はまったところ
- status.encode('utf-8') しないとエラーになる。
- urllibがUnicodeEncodeError: 'ascii' codec can't encode characters とかいう
- credentialの変数を書くところのインデント。インデントが問題になる言語ってどこで途中改行していいのかわかんなくて困る
課題
- usernameとかってドコに持つべき?いまべた書きしてるんだけど。
- 設定ファイル(YAMLとか)をさくっと読めるものがあればそれでも使おうか。