Help us understand the problem. What is going on with this article?

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

はじめに

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=ニコニコニュース

■ニコニコニュースチャンネルを除外する検索
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で指定する場合は下記のようになる。

http://api.search.nicovideo.jp/api/v2/video/contents/search?fields=contentId,startTime,channelId,title,tags&_sort=startTime&targets=title,tags&_limit=10&q=ニコニコニュース&jsonFilter={"type":"not","filter":{"type":"equal","field":"channelId","value":2525}}

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

以上

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした