ä¸äººã¢ããã³ãã«ã¬ã³ãã¼ã®ä¸æ¥ç®ã§ã(é
10æã«ãã³ãã³åç»ãããã¹ãããã·ã§ããæ¤ç´¢APIãã¨ããã®ãåºã¦ããã®ã§è§¦ã£ã¦ã¿ã¾ãã
æ¯æ¥æ5æã®æç¹ã®ãã¼ã¿ã®ã¹ãããã·ã§ããããæ¤ç´¢ã§ããAPIã§ãã³ãã³åç»ã®ã³ã³ãã³ãã解æããç®çã§æ¤ç´¢/åå¾ããéã«å©ç¨ã§ãã¾ã
ã¨ã®ãã¨ã§ã
ニコニコ動画 『スナップショット検索API』 ガイド
APIã¯http://api.search.nicovideo.jp/api/snapshot/ã«ä»¥ä¸ã®ãããªJSONãPOSTããæ¹å¼ã«ãªã£ã¦ãã¾ã(åã
ãã£ã¼ã«ãã®è©³ç´°ã¯ドキュメントãåç
§)
{ "query" : æ¤ç´¢ãã¼ã¯ã¼ã "service" : æ¤ç´¢å¯¾è±¡ãµã¼ãã¹ãªã¹ã, "search" : æ¤ç´¢å¯¾è±¡ãã£ã¼ã«ããªã¹ã, "join" : åå¾å¯¾è±¡ãã£ã¼ã«ããªã¹ã, "filters" : ãã£ã«ã¿æå®ãªã¹ã(ãªãã·ã§ã³), "sort_by" : 並ã¹æ¿ããã£ã¼ã«ãå(ãªãã·ã§ã³), "order" : 並ã¹æ¿ãé åº "desc" ããã㯠"asc"(ãªãã·ã§ã³ãããã©ã«ã: "desc"), "from" : N (æ°å¤æå®ããªãã·ã§ã³ãããã©ã«ã: 0), "size" : M (æ°å¤æå®ããªãã·ã§ã³ãããã©ã«ã: 10, æ大: 100), "issuer" : ãµã¼ãã¹/ã¢ããªã±ã¼ã·ã§ã³å (æ大: 40æå) }ニコニコ動画 『スナップショット検索API』 ガイド
1åã®ãªã¯ã¨ã¹ãã§æ大100件ã¾ã§ã®åç»ã®ã¿ã¤ãã«ã説ææãã¿ã°ãæ稿æ¥æãåçæ°ãæéãªã©ã®æ
å ±ãå¾ããã¾ã
æ稿æ¥æãªã©ã§ãã£ã«ã¿ããããææ°ã³ã¡ã³ãã®æéãªã©ã§ã½ã¼ãããããã§ãã¾ã
çµæã¯1è¡1åã®JSONãè¤æ°è¡ã¨ããå½¢å¼ã§è¿ã£ã¦ãã¾ã(åç»ãã¨ã«1è¡ã§ã¯ãªãã®ã§æ³¨æ)
{"dqnid":"e542b31f-036d-49fb-a0b5-2925b6cb0b6c","type":"stats","values":[{"_rowid":0,"service":"video","total":10660}]} {"dqnid":"e542b31f-036d-49fb-a0b5-2925b6cb0b6c","endofstream":true,"type":"stats"} {"dqnid":"e542b31f-036d-49fb-a0b5-2925b6cb0b6c","type":"hits","values":[{"_rowid":0,"cmsid":"sm16539814","title":"ãåé³ãã¯ã»å·¡é³ã«ã«ããªã³ã¡ãããªãï¼ãé¡é³çèªç¥2011ã","view_counter":1983881},{"_rowid":1,"cmsid":"sm2972481","title":"ãåé³ãã¯ããã«ã³è¡é²æ² - ãªã¯ã¿ï¼¼(^o^)ï¼ãã¢ã¬ã³ã¸ã","view_counter":1363194},{"_rowid":2,"cmsid":"sm17239967","title":"[åé³ãã¯]ãparanoiaã[ãªãªã¸ãã«]","view_counter":920589}]} {"dqnid":"e542b31f-036d-49fb-a0b5-2925b6cb0b6c","endofstream":true,"type":"hits"}ニコニコ動画 『スナップショット検索API』 ガイド
type==hitsã§endofstreamã§ãªãJSONã®valuesã®é¨åã«æ¤ç´¢çµæãå ¥ã£ã¦ãã¾ã
ã©ããã¼ãä½ã£ã¦ã¿ã
ã¡ãã£ã¨ãããã¥ããã®ã§Pythonã§ç°¡åãªã©ããã¼ãä½ã£ã¦ã¿ã¾ãã
ãã¾ã«APIã失æãã¦ã503ãè¿ã£ã¦ããã®ã§ãªãã©ã¤ãã§ããããã«ãã¦ãã¾ã
HTTPãªã¯ã¨ã¹ãã®é¨åã«Requestsã¨ããã©ã¤ãã©ãªã使ç¨ãã¦ãã¾ã(Pythonæ¨æºã®ãã®ãã使ããããã§ã)
å ¥ãã¦ããªããã°ã以ä¸ã®ã³ã¼ãã使ãåã«pip install requestsãã¦ãã ãã
# -*- coding: utf-8 -*- import requests import json import time import collections class NiconicoSnapshotAPIWrapper(object): endpoint = 'http://api.search.nicovideo.jp/api/snapshot/' endpoint_last_modified = 'http://api.search.nicovideo.jp/api/snapshot/version' Result = collections.namedtuple('NiconicoSnapshotAPIResult', 'total hits errid') default_result = Result(None, None, None) def __init__(self, issuer, **default_parameter): self.default_parameter = { 'service': ['video'], 'search': ['title', 'description', 'tags'], 'join': 'cmsid title description tags start_time thumbnail_url view_counter comment_counter mylist_counter last_res_body length_seconds'.split() } self.default_parameter.update(default_parameter) self.default_parameter['issuer'] = issuer def getLastModified(self): return requests.get(self.endpoint_last_modified).json()['last_modified'] def makeFilterEqual(self, field, value): ret = {} ret['type'] = 'equal' ret['field'] = field ret['value'] = value return ret def makeFilterRange(self, field, start = None, end = None, include_lower = True, include_upper = True): ret = {} ret['type'] = 'range' ret['field'] = field if start is not None: ret['from'] = start if end is not None: ret['to'] = end ret['include_lower'] = include_lower ret['include_upper'] = include_upper return ret def query(self, query, retry = 1, wait = 1, **kwargs): parameter = self._makeQueryParameter(query, kwargs) for i in xrange(retry): r = requests.post(u'http://api.search.nicovideo.jp/api/snapshot/', json=parameter) if r.status_code == requests.codes.ok: break else: time.sleep(wait) if r.status_code != requests.codes.ok: print r return self._parseResponse(r.text) def _makeQueryParameter(self, query, kwargs): parameter = self.default_parameter.copy() parameter['query'] = query parameter.update(kwargs) return parameter def _parseResponse(self, response): ret = {} for i, line in enumerate(response.splitlines()): data = json.loads(line) if 'endofstream' in data: continue if 'errid' in data: return self.default_result._replace(errid = data['errid']) if 'type' not in data: return self.default_result._replace(errid = -1) if data['type'] == 'stats': ret['total'] = data['values'][0]['total'] if data['type'] == 'hits': ret['hits'] = data['values'] return self.default_result._replace(**ret)
ã¡ãªã¿ã«Requestsã§ã¯ã¬ã¹ãã³ã¹ãJSONã®å ´å.json()ã§JSONã«å¤æãããã®ãå¾ãããã®ã§ããããã®APIã®å ´åã¯1è¡ã«1ã¤ã®JSONã並ãã ç¶æ
ã«ãªã£ã¦ããã®ã§ãè¤æ°ã®JSONããã³ã¼ããããã¨ãã¦ãã¾ã "ValueError: Extra data: " ããå§ã¾ãã¨ã©ã¼ãåºã¦ãã¾ãå¤æã§ãã¾ãã(Pythonã®JSONã®ã©ã¤ãã©ãªã®ä»æ§ã£ã½ã)
ãªã®ã§ä¸è¡ãã¤JSONã«å¤æãã¦ãã¾ã
使ç¨ä¾
ã©ããã¼ã®queryé¢æ°ã¯ãæ¤ç´¢ã¯ã¼ããå«ãåç»æ°(total)ãæ¤ç´¢ã«å¼ã£ããã£ãåç»ã®JSONããã³ã¼ãããè¾æ¸ã®list(hits)ããã¨ã¯ã¨ã©ã¼ID(errid)ãããã°è¿ãã¾ã
# -*- coding: utf-8 -*- from NiconicoSnapshotAPIWrapper import * if __name__ == '__main__': #issuer(ã¢ããªå)ãæå®ãã¦ãã ãã api = NiconicoSnapshotAPIWrapper('NiconicoSnapshotAPIWrapper')# #åç´ãªä½¿ãæ¹(æ¤ç´¢çµæã®å 1件ã ãåå¾) result = api.query(u'æ±æ¹', size = 1) print u'件æ°: {}, çµæ: {}'.format(result.total, result.hits) #件æ°ã ãåå¾ print api.query(u'æ±æ¹', size = 0).total #5åã¾ã§ãªãã©ã¤ãã(ãªãã©ã¤ã®éã«ã¯1ç§éãã) api.query(u'æ±æ¹', retry = 5, wait = 1, size = 0).total #ãã©ã¡ã¼ã¿ãæå®ãã print api.query(u'æ±æ¹', size = 1, sort_by = 'mylist_counter').hits[0]['last_res_body'] #ãã£ã«ã¿ã®ãã©ã¡ã¼ã¿ã¯JSONãä¸ããªãã¨ãããªã print api.query('æ±æ¹', size = 0, filters=[{'type': 'range', 'field': 'start_time', 'from': '2014-01-01 00:00:00'}]) #ãã£ã«ã¿ã®æå®ã®ãã«ãã¼é¢æ° print api.query('æ±æ¹', size = 0, filters=[api.makeFilterRange('start_time', '2014-01-01 00:00:00')]) #ãã¼ã¿ã®æ´æ°æ¥æã®åå¾(AM5:00ã®æç¹ã®ã¹ãããã·ã§ããããã®æ¥ã®ä½æã«åãæ¿ããã) print api.getLastModified()
æ±æ¹ãã£ã©ã®åç»æ°ä¸ä½ã調ã¹ã¦ã¿ã
ä»å¹´æ稿ãããæ±æ¹ãã£ã©ã®åç»æ°ãä¸ä½10ãã£ã©ã調ã¹ã¦ã¿ã¾ãã
# -*- coding: utf-8 -*- from NiconicoSnapshotAPIWrapper import * if __name__ == '__main__': api = NiconicoSnapshotAPIWrapper('NiconicoSnapshotAPIWrapper')# count = collections.Counter() data = u"""åéºé夢 é§é¨éçæ² ã«ã¼ã㢠大å¦ç²¾ ãã«ã ç´ ç¾é´ å°æªé ããã¥ãªã¼ã»ãã¼ã¬ã㸠åå å¤å²å¤ ã¬ããªã¢ã»ã¹ã«ã¼ã¬ãã ãã©ã³ãã¼ã«ã»ã¹ã«ã¼ã¬ãã ã¬ãã£ã»ãã¯ã¤ãããã¯ æ© ã¢ãªã¹ã»ãã¼ã¬ããã¤ã ãªãªã¼ãã¯ã¤ã ã«ããµã»ããªãºã ãªãã¼ ã¡ã«ã©ã³ã»ããªãºã ãªãã¼ ãªãªã«ã»ããªãºã ãªãã¼ ééå¦å¤¢ 西è¡å¯ºå¹½ã å å «é²è å «é²ç´« ä¼å¹èé¦ ãªã°ã«ï½¥ãã¤ããã° ãã¹ãã£ã¢ã»ãã¼ã¬ã©ã¤ ä¸ç½æ²¢æ §é³ å 幡ã¦ã é´ä»ã»åªæè¯é¢ã»ã¤ãã å «ææ°¸ç³ è¬è±å±±è¼å¤ è¤åå¦¹ç´ ã¡ãã£ã¹ã³ã»ã¡ã©ã³ã³ãªã¼ 風è¦å¹½é¦ å°éå¡å°çº åå£æ 姫ã»ã¤ãã¶ãã㥠å°å½ä¸¸æ ç§éè ç§ç©£å éµå±±é æ²³åã«ã¨ã ç¬èµ°æ¤ æ±é¢¨è°·æ©è å «åç¥å¥å æ´©ç¢è«è¨ªå æ°¸æ±è¡£ç æ¯é£åå± å¤©å ãã¹ã¡ é»è°·ã¤ãã¡ æ°´æ©ãã«ã¹ã£ æçåå å¤æå°ãã¨ã ç«çç«ç éç路空 å¤æå°ããã ããºã¼ãªã³ å¤ã è¯å°å é²å± ä¸è¼ª é²å±± æç´æ°´è å¯ ä¸¸æ èç½è® å°ç£ã¬ã å§«æµ·æ£ ã¯ã㦠幽谷é¿å å®®å¤è³é¦ éé娥 èæå± èªå¤ ç©é¨å¸é½ è±è¡è³ç¥å äºã岩ããã¾ã¦ 秦ããã ãããã姫 赤è®å¥ ä»æ³å½±ç¼ ä¹åä¹å¼ã ä¹åä¹å «æ© 鬼人æ£éª å°åéå¦ä¸¸ å å·é·é¼ 綿æè±å§« 綿æä¾å§« ã¬ã¤ã»ã³ ãµãã¼ãã«ã¯ ã«ããã£ã¤ã«ã ã¹ã¿ã¼ãµãã¡ã¤ã¢ è¨æ¨è¯æ æ¬å± å°é´ 森è¿éä¹å© ç¨ç°é¿æ± å®ä½è¦è®å ãã¨ãªããªã¼ã»ãã¼ã³""".split() for word in data: time.sleep(1) count[word] = api.query(word, retry = 3, size = 0, filters=[api.makeFilterRange('start_time', '2014-01-01 00:00:00', '2014-12-31 23:59:59')]).total for word, freq in count.most_common(10): print word, freq
ãã«ããå¦ã«å¤ããªãã¾ããããåå ã¯ãããããã¾ãã
å®ã¯ãã£ã©åã ãã®æ¤ç´¢ã«ã¯åé¡ããã£ã¦ããã®æ¤ç´¢ã¯ã¼ããå«ããè¦ã¦ããã ããªã®ã§ãç¹ã«ãæ©ãã¯æ±æ¹ã¨é¢ä¿ãªãåç»ãå¤ãã¯ãã§ã
é ä½ | ãã£ã©å | åç»æ° |
---|---|---|
1 | ãã«ã | 3403 |
2 | åéºé夢 | 1392 |
3 | æ© | 1236 |
4 | å°æªé | 1087 |
5 | åå å¤å²å¤ | 1008 |
6 | é§é¨éçæ² | 975 |
7 | ã«ã¼ã㢠| 911 |
8 | ã¬ããªã¢ã»ã¹ã«ã¼ã¬ãã | 749 |
9 | ãã©ã³ãã¼ã«ã»ã¹ã«ã¼ã¬ãã | 682 |
10 | æ±é¢¨è°·æ©è | 602 |
ãæ±æ¹ãã¿ã°ã¨ãã£ã©åã®ã¿ã°ã両æ¹å«ãåç»ã®ä»¶æ°ã調ã¹ã¦ã¿ãã¨ããã«ããæ©ã®æ°ãå°ãªããªãã¾ãã
æ±æ¹ã¡ã¤ã³ãããªãåç»ã«ãã«ãã¯ããããåºã¦ããã®ããä½ã被ã£ã¦ããåèªãããã®ããªããªãã§ãããâ¦â¦ï¼(ãã³ãã³åç»ã«é¢ãããã¡ã¤ã³ç¥èã足ããªã
é ä½ | ãã£ã©å | åç»æ° |
---|---|---|
1 | åéºé夢 | 1190 |
2 | ãã«ã | 1157 |
3 | åå å¤å²å¤ | 881 |
4 | é§é¨éçæ² | 857 |
5 | ã«ã¼ã㢠| 717 |
6 | ã¬ããªã¢ã»ã¹ã«ã¼ã¬ãã | 698 |
7 | ãã©ã³ãã¼ã«ã»ã¹ã«ã¼ã¬ãã | 584 |
8 | æ±é¢¨è°·æ©è | 549 |
9 | å «é²ç´« | 544 |
10 | å°æªé | 536 |
ã¾ã¨ã
ãã³ãã³åç» ãã¹ãããã·ã§ããæ¤ç´¢APIãã¨ããé¢ç½ãããªãã®ãåºã¦ããã®ã§è§¦ã£ã¦ã¿ãã
åç»æ°ã ãè¦ã¦ãã¨ç®å½ã¦ã®ãã®ã§ãªããã®ãå¼ã£ããã£ã¦ããã¨ãããã®ã§ãæ¤ç´¢ã¯ã¼ãã®é¸ã³æ¹ã«ã¯æ³¨æã
â¦â¦pixivããããæ©ãAPIåºãã¦ãããªãã§ãããã¼ï¼