5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ニコニコ動画のスナップショット検索APIを使ってサムネイルのリストを作る

Last updated at Posted at 2015-12-16

ニコニコ動画のAPIのレスポンスからサムネイルのURLを抜き出して、
画像を並べたHTMLファイルを作るシェルスクリプトを書いてみました。

APIのガイド

http://search.nicovideo.jp/docs/api/snapshot.html
実行例のAcceptの前に「-H」が足りない気が。

コード

niconico.sh
# !/bin/bash

post='{"query":"初音ミク","service":["video"],"search":["title","description","tags"],"join":["cmsid","title","view_counter", "thumbnail_url"],"filters":[{"type":"range","field":"view_counter","to":1000000}],"from":0,"size":10,"sort_by":"view_counter","issuer":"yourservice/applicationname"}'
url="http://api.search.nicovideo.jp/api/snapshot/"
date=`date '+%Y%m%d%H%M'`
file="$date.html"

images=(`curl -s -H "Accept: application/json" -H "Content-type: application/json" -X POST -d "$post" $url | egrep -o "thumbnail_url\":\"[^\"]+" | egrep -o "[^\"]+$" | sed -e 's/\\\\\//\\//g'`)

for image in "${images[@]}"; do
    echo "<img src=\"$image\">" >> $file
done

やっていること

  • date=date '+%Y%m%d%H%M'
  • 実行時間を取得(例:201512162200)
  • images=(``)
  • ``内の実行結果を配列に入れる
  • curl
  • 「初音ミク」のキーワードでヒットする動画のうち1,000,000再生以下のものを10件取得
  • 「-s」プログレスメータやエラーメッセージを省略する
  • 「-H」ヘッダを指定する
  • 「-X」リクエストメソッドを指定
  • 「-d」指定したデータをPOSTリクエストで送る
  • egrep -o "thumbnail_url":"[^\"]+"
  • thumbnail_urlの箇所のみを抜き出す
  • 「-o」正規表現にマッチした箇所のみを抜き出す
  • egrep -o "[^\"]+$"
  • thumbnail_urlのURLの部分のみを抜き出す
  • sed -e 's/\\//\//g'
  • thumbnail_urlのURLの「/」が「/」となっているため、「/」に変換する
  • for image in "${images[@]}"; do ~ done
  • imagesの配列一つ一つを取得し、imgタグの形式で出力ファイルに吐き出す

結果

201512162200.html
<img src="http://tn-skr1.smilevideo.jp/smile?i=1563848">
<img src="http://tn-skr1.smilevideo.jp/smile?i=7103480">
<img src="http://tn-skr3.smilevideo.jp/smile?i=1384758">
<img src="http://tn-skr1.smilevideo.jp/smile?i=17375356">
<img src="http://tn-skr3.smilevideo.jp/smile?i=13560654">
<img src="http://tn-skr1.smilevideo.jp/smile?i=9382588">
<img src="http://tn-skr4.smilevideo.jp/smile?i=17211747">
<img src="http://tn-skr2.smilevideo.jp/smile?i=2700265">
<img src="http://tn-skr3.smilevideo.jp/smile?i=6971638">
<img src="http://tn-skr1.smilevideo.jp/smile?i=8424952">
5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?