TwitterããTwitterAPIã使ããã«ã¹ã¯ã¬ã¤ãã³ã°ãã¦ã¿ã
èªåã¯ã¾ã Twitterã®ã¢ã«ã¦ã³ããæã£ã¦ã¾ããããTwitterããæ
å ±ãåãåºãã¦ã¿ãããªã£ãã®ã§åæ代çãªæ¹æ³ã§ãã£ã¦ã¿ãã
Google App Engine(Python)ã使ã£ã¦ã¿ã¾ããã
html解æã¯BeautifulSoupã¨ããã©ã¤ãã©ãªã使ãã¨ä¾¿å©ãªã®ã§ãããè²ã
ã¨é¢åãªã®ã§ä»åã¯æ£è¦è¡¨ç¾ã§ç¡çããæ½åºãã¾ããã
ãããããã¯Javascriptã ãã§ãåºæ¥ããã§ãããGoogleã®æãä½ãè³æºãæå¹æ´»ç¨ããã¦ãããã¨ãããã¨ã§ã»ã»ã»ã
ãµã³ãã«URL
http://voidy21.appspot.com/twit_statusã以ä¸ã«ããã¾ãï¼
使ãæ¹
GETã¡ã½ããã§
- callback : ã³ã¼ã«ããã¯é¢æ°å
- twitter_id : twitterã®ã¢ã«ã¦ã³ãå
- status_num: çºè¨ã®ã¹ãã¼ã¿ã¹id
ãæå®ãã
ä¾ãã°http://twitter.com/zenra_bot/status/3605531004ãªã
http://voidy21.appspot.com/twit_status?callback=hogehoge&twitter_id=zenra_bot&status_num=3605531004
ã¨æå®ãã¾ãã
çµæã¯JSON(P)ã§è¿ã£ã¦ãã¾ãã
- date : ã¢ãã¦ããªæ¥ä»
- entry : çºè¨
- image : ãããã¡ã¤ã«ç»åã®URL
ä½ã«ä½¿ããã®ãï¼
ãããªã«ä½¿ãéã¯ç¡ãã§ãï¼(ï¼¾oï¼¾)ï¼¼
èªåã¯Ajaxç¨(JSONP)ã«ä½¿ãã¾ãã
ä¸ã®ç»åã®ãããªæãã§ï¼æ²ç¤ºæ¿ã«Twitterã®ã¢ãã¬ã¹ãè²¼ãããå ´åï¼éåæã§Twitterããæ
å ±ãåå¾ã§ãã¾ãï¼
ãã¨å°å³ã«çããã®ãããã¡ã¤ã«ç»åã®ãã¡ã¤ã«åãåå¾ã§ããç¹ãæãã¬å©ç¹ã»ã»ã»ï¼
ãµã¼ãå´ã®ã½ã¼ã¹(Google App Engine / Python)
# -*- coding: utf-8 -*- import wsgiref.handlers import cgi import re import urllib2 from django.utils import simplejson from google.appengine.ext import webapp from google.appengine.api import urlfetch class GetTwitterStatus(webapp.RequestHandler): def get(self): self.response.headers['Content-Type'] = 'application/json' self.response.headers['charset'] = 'UTF-8' callback = cgi.escape(self.request.get('callback')) twitter_id = cgi.escape(self.request.get('twitter_id')) status_num = cgi.escape(self.request.get('status_num')) url = "http://twitter.com/%s/status/%s" % (twitter_id, status_num) html = urlfetch.fetch(url).content entry = self.getTwitterEntry(html, twitter_id) self.response.out.write("%s(%s)" % (callback, simplejson.dumps(entry, ensure_ascii=False))) def getTwitterEntry(self, html, twitter_id): parser_entry = re.compile("""<span class="entry-content">(.*?)</span>""",re.S) #20090831追å :æ¹è¡ãæ£è¦è¡¨ç¾ã§ãæååã¨ã¿ãªãããã«ããªãã¨ãã¡ parser_date = re.compile("""<span class="published">(.*?)</span>""") parser_image = re.compile("""<a href="http://twitter.com/%s" class=".*" hreflang=".*">""" """<img alt=".*" .* src="(.*)" style=".*" .*></a>""" % twitter_id ) entry = self.encodeURI(parser_entry.search(html).group(1)) date = self.encodeURI(parser_date.search(html).group(1)) image = self.encodeURI(parser_image.search(html).group(1)) return { "entry":entry, "date":date, "image":image, } def encodeURI(self, url_string): #Javascriptã®decodeURIã¨å¯¾å¿ãåããå½¢ return urllib2.quote(url_string, safe='!#$&\'()*+,-./:;=?@_~') application = webapp.WSGIApplication([ ('/twit_status', GetTwitterStatus), ], debug=False) def main(): wsgiref.handlers.CGIHandler().run(application) if __name__ == '__main__': main()
ããã£ããã¨
- non-greedy æå®åã¨ããã®ããã£ã¦ã*?ã¨ãæ¸ãã¨æçã®æ£è¦è¡¨ç¾ã ãã«ãããåºæ¥ãããã«ãªãããã
- _multiprocessingãç¡ãã¨ãè¨ããã¦åããªãã£ãã®ã§http://d.hatena.ne.jp/tmatsuu/20090818/1250606215ãåèã«(SDK)/google/appengine/tools/dev_appserver.pyã«_multiprocessingã追å ããã¨åãããã«ãªãã¾ãããæè¬ã§ãï¼
- Twitterã¢ã«ã¦ã³ãã¯æã£ã¦ããæ¹ãããã·ã¥ã¢ããããããã¨æãã¾ãï¼