tweepyã§Streaming API
id:nekomusha6:20110121:1295606221 ã§ã©ã¤ãã©ãªãtweepyã«æ±ºã¾ã£ãã®ã§ã次ã¯ã¢ããªã±ã¼ã·ã§ã³ã®ä½æã«ãªãã¾ãã
ãé¡
tweepyã使ã£ã¦ãã©ãã¼ãã¦ãã人ã®ã¤ã¶ãããStreaming APIã使ã£ã¦æ¾ã
ã¯ããã«
twitterã®APIã«ã¯ãå ¬å¼*1 *2ã«ããã¨ç¾å¨ä»¥ä¸ã®3種é¡ãåãæ¹ã«ãã£ã¦ã¯5種é¡ãããã¾ãã
(ãããã®Web Serviceã®Pythonã©ããã¼ã§ãã)tweepyã§å¯¾å¿ãã¦ããã®ã¯ã(1)ã(3-1)ã¾ã§ã§ãããä»å使ç¨ããã®ã¯(1)ã¨(3-1)ã ãã§ãã
ããã§ã¯æ¤è¨
Streaming APIのサンプルãè¦ãã¨ãã©ãããã¦ã¼ã¶ã¼åã¨ãã¹ã¯ã¼ããåããã°(Basicèªè¨¼ã§)ãªããã§ããããªæ°ããã¾ããã§ããã£ã¦ã¿ãã¨...
åãã¾ãããfilterã®ã¦ã¼ã¶ã¼æå®(folow)ãã§ãã¾ãããããæ£ç¢ºã«ã¯ä¾ãã°ç§ã®ã¦ã¼ã¶ã¼ãæå®ããã®ã ã£ãã@ã®å¾ãã«æ¸ãnekomushaã§ããã®ãã¨æã£ã¦ããã®ã§ãããã¦ã¼ã¶ã¼ID(230193917)ãå¿ è¦ã§ãããããã§ã¯
- ã©ãããã°ã¦ã¼ã¶ã¼åããã¦ã¼ã¶ã¼IDãå¾ãããã®ãï¼
- ããã«ã©ãããã°ãã©ãã¼ãã¦ãã人ã®ä¸è¦§ãå¾ãããã®ãï¼
ããã(3-1)Streaming APIã«ã¯ã©ã¡ãã®æ©è½ãç¨æããã¦ãã¾ããããããã¯(1)REST APIã®1ã¤ã§ãã以ä¸ã®APIã§ã¾ã¨ãã¦ç¨æããã¦ãã¾ãã
"GET friends/ids"
http://dev.twitter.com/doc/get/friends/ids
ããã¯REST APIãã¤èªè¨¼ä¸è¦ãªAPIãªã®ã§ãä¾ãã°ãã©ã¦ã¶ãã
http://api.twitter.com/1/friends/ids.json?screen_name=nekomusha
ã¨ããã ãã§ç§ã®ãã©ãã¼ãã¦ãã人ã®IDä¸è¦§ãJSONå½¢å¼ã§åãã¦ãã¾ãã¾ãã
ãã¡ããtweepyã«ããã®APIã使ãé¢æ°ãç¨æããã¦ãã¾ãã
API.friends_ids(id/screen_name/user_id[, cursor])(http://joshthecoder.github.com/tweepy/docs/api.html#API.friends_ids)
ããã ãæ å ±ãæãã°ååä½ãããã§ãã
å®è£
ã»ã¼ãµã³ãã«ã®ã¾ã¾ã§ãã...
#!/usr/bin/env python from textwrap import TextWrapper import tweepy username = "ã¦ã¼ã¶ã¼å" password = "ãã¹ã¯ã¼ã" class StreamWatcherListener(tweepy.StreamListener): status_wrapper = TextWrapper(width=60, initial_indent=' ', subsequent_indent=' ') def on_status(self, status): try: print self.status_wrapper.fill(status.text) print '\n %s %s via %s\n' % (status.author.screen_name, status.created_at, status.source) except: # Catch any unicode errors while printing to console # and just ignore them to avoid breaking application. pass def on_error(self, status_code): print 'An error has occured! Status code = %s' % status_code return True # keep stream alive def on_timeout(self): print 'Snoozing Zzzzzz' follow_list = tweepy.api.friends_ids(screen_name=username) if len(follow_list) > 5000: print "too many firends." else: stream = tweepy.Stream(username, password, StreamWatcherListener(200), timeout=None) stream.filter(follow_list)
ãªãããã©ãã¼ãã¦ãã人ã5000人ãè¶ ãããã¨ã©ã¼ã«ãã¦ããã®ã¯ã
"Streaming API: Methods"
http://dev.twitter.com/pages/streaming_api_methods#statuses-filter
ã«ãããããã©ã«ãã ã¨ãã®è¾ºãä¸éã ããã§ãããã以ä¸å¢ããããå ´åã¯ãUser Streamã®åºçªãªã®ã§ãããã試ãã¦ãããfollowããããããããªã©ã®ã¤ãã³ãã¾ã§æ¾ã£ã¦ãããããã§ã(ããå¿ãã¾ãããStreaming APIã«ããã ãã§ããã£ãºãã«æ¾ããæ å ±ãå¢ãã¾ã)ã
ãã¾ã
ä»åã¯èªè¨¼æ¹æ³ã¯Basicèªè¨¼ã«ãªã£ã¦ãã¾ãããHTTPãªã®ã§ãã¹ã¯ã¼ããå¹³æã§éããããã¨ã«ãªãã¾ãããã¹ã¯ã¼ãã¯æ¨ä»ãããããªã¨ããã§åãã®ã使ããã¦ãããããã¡ãªã®ã§ã(OAuthã«æ¯ã¹ã¦)æ¼ããã¨ãã®ãã¡ã¼ã¸ãæ ¼æ®µã«å¤§ããããã¾ãããããã¾ããããã£ããStreaming APIã«å¯¾å¿ãã¦ããOAuthã§ã»ã¼åããã¨ãã§ããREST APIã®æ¹ãå®å ¨ã§è¯ãã¨ããçµè«ã«ãªã£ã¦ãã¾ãã¾ã(å®ã¯Streaming APIã¯Basicèªè¨¼/OAuth両対å¿ãã¦ã¾ããtweepyãBasicèªè¨¼ãã対å¿ãã¦ãªãã ãã§ã)ãããã§ãã¾ãã¯æå·åéä¿¡ã§ããªãã調ã¹ã¦ã¿ã¾ãããçµæ...
æå·åéä¿¡ã¯ã§ãã¾ããã§ãããtwitterã¯SSL(HTTPS)ã«å¯¾å¿ãã¦ãã¾ãããtweepyã®Streaming APIã§ã¯ã§ããªãããã§ãããããtweepyã1è¡ãããã°HTTPSã§éä¿¡ã§ããããã«ãªãã¾ã(HTTPã§ã¯éä¿¡ã§ããªããªãã¾ãã)ã
/usr/share/pyshared/tweepy/streaming.py: 196
conn = httplib.HTTPConnection(self.host)
ããã
/usr/share/pyshared/tweepy/streaming.py: 196
conn = httplib.HTTPSConnection(self.host)
ããããã ãï¼