åãã¦ãTwitter botãªããã®ãä½ã£ã¦ã¿ãã
http://twitter.com/zenra_bot
å
¬å¼ãã¼ã¸ï¼http://zen-ra.appspot.com/
- ãã©ãã¼ããã¨ãã©ãã¼ãè¿ãã¾ã
- ãã©ãã¼ãå¤ãããã¨åæ§ã«ãã©ãã¼ãå¤ãã¾ã
- ãã©ãã¯ã¼ã®çºè¨ãæ¾ã£ã¦ãåæã«å
¨è£¸ã«æ¸ãæãã¾ã
- ä¾ï¼ã@sugyan ãå ¨è£¸ã§è¨ã£ã: ********ã
- éå ¬éã®çºè¨ã¯æ¾ããªãããã«ãã¦ãã¾ãã
- ãã¾ã«ç¬ãè¨ãã¤ã¶ããã¾ã
å®è£
Google App Engine(Python)ã§ä½ãã¾ããã
ã½ã¼ã¹ã³ã¼ãã¯GitHubã«ã¦å
¬éãã¦ãã¾ãã
GitHub - sugyan/Zenra: 全裸にする
åºæ¬çã«cronã§ãã©ãã¯ã¼ã®ãã§ãã¯&æ´æ°ãã¤ã¶ãããè¡ã£ã¦ããã ãã
ç¹ã«å¤é¨ã®ã©ã¤ãã©ãªã使ããã«GAEã®urlfetch APIã§Twitter APIãç´æ¥å©ãããã
ä¸çªè¦å´ããã®ãfollowerã®æ´æ°å¦çã§ã
- èªåããã©ãã¼ãã¦ããã¦ã¼ã¶ãåå¾ãã(friends/ids)
- èªåããã©ãã¼ãã¦ããã¦ã¼ã¶ãåå¾ãã(followers/ids)
- ããããã®å·®åã調ã¹ã¦ã
- ãã©ãã¼ããã¦ãããã©èªåãã¾ã ãã©ãã¼ãã¦ããªãã¦ã¼ã¶ããã©ãã¼(friendships/create)
- ãã©ãã¼å¤ããã¦ããã®ã«èªåãã¾ã ãã©ãã¼ãã¦ããã¦ã¼ã¶ããªã ã¼ã(friendships/destroy)
ã¨ããå¦çããããã¨ã§friendsã¨followerã常ã«çããã§ãããã¨æã£ãã®ã ããã©ãGAEã§ã¯30ç§ä»¥å
ã«ã¬ã¹ãã³ã¹ãè¿ããªãã¨ãããªãã®ã§ãããã®å¦çãä¸çºã§å
¨é¨ãããã¨ããã¨ã¿ã¤ã ãªã¼ãã¼ã«ãªã£ã¦ãã¾ãã®ã§ã¯ãã¨èããã
ã®ã§ã1, 2, ãããããç¬ç«ããå¦çã¨ãã¦è¡ãåå¾çµæãDatastoreã«æ ¼ç´ãã3.1, 3.2ãã¾ãç¬ç«ããå¦çã§ãã®Datastoreã®ãã¼ã¿ãèªã¿åã£ã¦è¡ããã¨ã«ããã
å
·ä½çã«ã¯ãããªã«ã³ã¸ã
#/usr/bin/env python # -*- coding: utf-8 -*- from google.appengine.ext import db class IDS(db.Model): friend = db.BooleanProperty() follower = db.BooleanProperty()
class TwitBot: ... # èªåã®friendsã®ãã¼ã¿ãæ´æ°ãã def friends(self): url = 'http://twitter.com/friends/ids.json' result = urlfetch.fetch( url = url, headers = self.auth_header, ) if result.status_code == 200: keys = ["id:%d" % (id) for id in simplejson.loads(result.content)] # æ¢ã«ç»é²ããã¦ããidãã©ããããã§ãã¯ãã for id in IDS.all().filter('friend =', True): key_name = id.key().name() # ç»é²ããã¦ããã°å¦çã®å¿ è¦ãªã if key_name in keys: keys.remove(key_name) # ãã©ãã¼ãã¦ããçã ã£ãã®ãå¤ãã¦ããå ´å else: id.friend = False id.put() # æ°è¦ã«ãã©ãã¼ãã¹ãidã¨ãã¦ç»é² ids = [] for key in keys: id = IDS.get_by_key_name(key) if id == None: id = IDS(key_name = key, follower = False) id.friend = True ids.append(id) db.put(ids) # èªåã®followersã®ãã¼ã¿ãæ´æ°ãã def followers(self): url = 'http://twitter.com/followers/ids.json' result = urlfetch.fetch( url = url, headers = self.auth_header, ) if result.status_code == 200: keys = ["id:%d" % (id) for id in simplejson.loads(result.content)] # æ¢ã«ç»é²ããã¦ããidãã©ããããã§ãã¯ãã for id in IDS.all().filter('follower =', True): key_name = id.key().name() # ç»é²ããã¦ããã°å¦çã®å¿ è¦ãªã if key_name in keys: keys.remove(key_name) # ãã©ãã¼ããã¦ããçã ã£ãã®ãå¤ããã¦ããå ´å else: id.follower = False id.put() # æ°è¦ã«ãã©ãã¼ãããidã¨ãã¦ç»é² ids = [] for key in keys: id = IDS.get_by_key_name(key) if id == None: id = IDS(key_name = key, friend = False) id.follower = True ids.append(id) db.put(ids) # æ°ãã«ãã©ãã¼ãã def create(self): # ãã©ãã¼ãã¹ãidã®æ½åº query = IDS.all() query.filter('follower =', True) query.filter('friend =', False) id = query.get() if id: # APIã¸ã®éä¿¡ url = 'http://twitter.com/friendships/create/%s.json' % (id.key().name()[3:]) result = urlfetch.fetch( url = url, method = urlfetch.POST, headers = self.auth_header, ) # å é¨ãã¼ã¿ã®æ´æ° id.friend = True id.put() # ãã©ãã¼ãå¤ã def destroy(self): # ãªã ã¼ããã¹ãidã®æ½åº query = IDS.all() query.filter('friend =', True) query.filter('follower =', False) id = query.get() if id: # APIã¸ã®éä¿¡ url = 'http://twitter.com/friendships/destroy/%s.json' % (id.key().name()[3:]) result = urlfetch.fetch( url = url, method = urlfetch.POST, headers = self.auth_header, ) # å é¨ãã¼ã¿ã®æ´æ° id.delete()
ã§ããããã®ã¡ã½ãããããããcronã§5åééãããã§å©ãã¦æ´æ°ããããã«ãã¦ãã¾ãã
(ä¼¼ããããªå¦çã並ãã§ããã®ã§ããã¯ãã¨ã§ãªãã¡ã¯ã¿ãªã³ã°ããã¨ãã¦â¦)
ããã§ãåå¾ä»¶æ°ã1000件ãè¶
ãã¦ãã¾ãã¨(ã¾ãæãå¾ãªãã ãããã©ï½)ãã¾ãåããªããªã£ã¦ãã¾ããããä½ããã£ã¨è¯ãæ¹æ³ãªãããªãã
ãã¨çºè¨ãå
¨è£¸ã«ããå¦çã¯ä»¥åã«æ¸ããYahooã®å½¢æ
ç´ è§£æAPIã使ã£ãæ¹æ³ã§è¡ã£ã¦ãã¾ãã
Pythonから全裸で形態素解析をする - すぎゃーんメモ
ããå°ãèªç¶ãªæ¥æ¬èªã§å
¨è£¸ã«ã§ããããããã®ããããããããæ¹è¯ãã¦ããã¤ããã