LoginSignup
4
5

More than 3 years have passed since last update.

【ニコニコ動画】niconicoコンテンツ検索APIで様々なフィルター条件でコンテンツの検索を行う

Last updated at Posted at 2019-10-17

はじめに

niconicoコンテンツ検索APIで様々なフィルター条件を利用してニコニコ動画のコンテンツ検索を行った際のメモ。

プログラム

jsonFilterを利用した検索(Python)

nico_jsonFilter.py
import json
import urllib.request

url_format = 'http://api.search.nicovideo.jp/api/v2/%s/contents/search'
url_live = url_format % 'live'
url_video = url_format % 'video'

jsonFilter = """{
  "type": "or",
  "filters": [
    {
      "type": "range",
      "field": "startTime",
      "from": "2017-07-07T00:00:00+09:00",
      "to": "2017-07-08T00:00:00+09:00",
      "include_upper": false
    },
    {
      "type": "range",
      "field": "startTime",
      "from": "2016-07-07T00:00:00+09:00",
      "to": "2016-07-08T00:00:00+09:00",
      "include_upper": false
    }
  ]
}"""

params = {
    'q':'初音ミク',
    'targets':'title,tags',
    'fields':'contentId,title,startTime,channelId',
    '_sort':'startTime',
    '_context':'nico_jsonFilter',
    '_limit':3,
    'jsonFilter':jsonFilter
}

req = urllib.request.Request('{}?{}'.format(url_video, urllib.parse.urlencode(params)))
res = json.loads(urllib.request.urlopen(req).read())['data']

print(json.dumps(res, ensure_ascii=False, indent=4))
$ python nico_jsonFilter.py

[
    {
        "title": "【初音ミク】死に至る病【オリジナル】",
        "contentId": "sm31529802",
        "startTime": "2017-07-07T23:57:42+09:00",
        "channelId": null
    },
    {
        "title": "【初音ミク】CHiCO with HoneyWorks/プライド革命【カバー】",
        "contentId": "sm31529674",
        "startTime": "2017-07-07T23:50:13+09:00",
        "channelId": null
    },
    {
        "title": "右肩の蝶 EXTREME perfect 達成率兼スコアタ 【Project DIVA Arcade】",
        "contentId": "sm31529638",
        "startTime": "2017-07-07T23:27:32+09:00",
        "channelId": null
    }
]

filtersクエリパラメータのNOT検索

たとえば、channelId が 2525 の動画を除外した結果を得る場合は、下記のように指定を行う。

filters[-channelId][0]=2525

channelId が 2525 は「ニコニコニュースチャンネル」である。下記にAPIのリクエスト例を示す。

■ニコニコニュースチャンネルを除外しない検索

■ニコニコニュースチャンネルを除外する検索
http://api.search.nicovideo.jp/api/v2/video/contents/search?fields=contentId,startTime,channelId,title,tags&_sort=startTime&targets=title,tags&_limit=10&q=ニコニコニュース&filters[-channelId][0]=2525

jsonFilterで指定する場合は下記のようになる。

※本「filtersクエリパラメータのNOT検索」の内容はドワンゴにAPI仕様を問い合わせた際の回答を載せている。公式APIガイドにも同内容を記載もしてもらうように要望したので、そちらに記載された場合はここから消すかも。

以上

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5