LoginSignup
8
12

More than 5 years have passed since last update.

Node.js で youtube検索を行う

Posted at

Node.jsでyoutube検索をやってみた備忘録です。
Vagrantで仮想環境作って試したので、ついでにそれも載せてあります。
蛇足かもしれませんが。。。

環境構築

Vagrantを利用して、仮想マシン上で開発を行う。

使用マシン: Mac OS X Yosemite (10.10.2)
Vagrant: 1.7.2
VirtualBox: 4.2

$ mkdir develop
$ cd develop
$ mkdir source # ソース置き場
$ vagrant box add chef/centos-7.0
$ vagrant init
$ vim Vagrantfile # 下記の通り編集
$ vagrant up
config.vm.box = "chef/centos-7.0" # box指定
config.vm.network "private_network", ip: "192.168.33.10" # プライベートネットワークの有効化
config.vm.synced_folder "./source", "/var/www/node_youtube" # 共有ディレクトリの指定
$ vagrant ssh # 仮想マシンにログイン


$ sudo yum groupinstall "Development Tools"
$ sudo yum install epel-release
$ sudo yum install nodejs
$ sudo yum install npm

Node.js 環境の準備

開発ディレクトリにnpm用のpackage.jsonを置く
置いたらnpm install でモジュールを導入する

package.json
{
  "dependencies": {
    "express": "*",
    "youtube-node": "*"
  }
}

Youtube APIの利用

  • google developer console でプロジェクトを新規作成
  • サイドバー→APIと認証→API→Youtube Data API→APIを有効にする
  • 認証情報→新しいキーを作成

以上でAPIキーの生成が出来るはず。

node-youtubeの利用

参考: https://www.npmjs.com/package/youtube-node

app.js
(function() {
  var Youtube = require('youtube-node');
  var youtube = new Youtube();

  youtube.setKey('[取得したAPIキー]');
  youtube.search('検索クエリ', [最大取得数], function(error, result) {
    if (error) {
      console.log(error);
      return ;
    }
    console.log(JSON.stringify(result, null, 2));
  });
})();
レスポンス例
{
      "kind": "youtube#searchResult",
      "etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/azXfax9T7cmjrWZy_1JcFELtw2w\"",
      "id": {
        "kind": "youtube#video",
        "videoId": "ud_seyCRXso"
      },
      "snippet": {
        "publishedAt": "2013-04-15T13:00:13.000Z",
        "channelId": "UC8v9MeHJF0jYg3LOTi69HNQ",
        "title": "柳田悠岐 登場曲~行くぜっ!怪盗少女/ももいろクローバー  2013.4.13",
        "description": "狙い撃ち☆ (^^) 2013/4/13 ソフトバンクホークスvs千葉ロッテ 1塁側5x列23xより撮影.",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/ud_seyCRXso/default.jpg"
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/ud_seyCRXso/mqdefault.jpg"
          },
          "high": {
            "url": "https://i.ytimg.com/vi/ud_seyCRXso/hqdefault.jpg"
          }
        },
        "channelTitle": "joyjoytube",
        "liveBroadcastContent": "none"
      }

より詳しい情報を取るには

Youtube.getById(id, callback) を使う
IDは上記の取得例を利用

app.js
youtube.getById('ud_seyCRXso', function(error, result) {
    if (error) {
      console.log(error);
      return ;
    }

    console.log(JSON.stringify(result, null, 2));
});
レスポンス例
{
  "kind": "youtube#videoListResponse",
  "etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/Fpqc9d1G_XWNyEucIX_Pt4kPRwU\"",
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 1
  },
  "items": [
    {
      "kind": "youtube#video",
      "etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/acJgue7t6SoiVwq_1_iNzJieVus\"",
      "id": "ud_seyCRXso",
      "snippet": {
        "publishedAt": "2013-04-15T13:00:13.000Z",
        "channelId": "UC8v9MeHJF0jYg3LOTi69HNQ",
        "title": "柳田悠岐 登場曲~行くぜっ!怪盗少女/ももいろクローバー  2013.4.13",
        "description": "狙い撃ち☆ (^^)\n\n2013/4/13 ソフトバンクホークスvs千葉ロッテ\n1塁側5x列23xより撮影",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/ud_seyCRXso/default.jpg",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/ud_seyCRXso/mqdefault.jpg",
            "width": 320,
            "height": 180
          },
          "high": {
            "url": "https://i.ytimg.com/vi/ud_seyCRXso/hqdefault.jpg",
            "width": 480,
            "height": 360
          },
          "standard": {
            "url": "https://i.ytimg.com/vi/ud_seyCRXso/sddefault.jpg",
            "width": 640,
            "height": 480
          },
          "maxres": {
            "url": "https://i.ytimg.com/vi/ud_seyCRXso/maxresdefault.jpg",
            "width": 1280,
            "height": 720
          }
        },
        "channelTitle": "joyjoytube",
        "categoryId": "17",
        "liveBroadcastContent": "none",
        "localized": {
          "title": "柳田悠岐 登場曲~行くぜっ!怪盗少女/ももいろクローバー  2013.4.13",
          "description": "狙い撃ち☆ (^^)\n\n2013/4/13 ソフトバンクホークスvs千葉ロッテ\n1塁側5x列23xより撮影"
        }
      },
      "contentDetails": {
        "duration": "PT20S",
        "dimension": "2d",
        "definition": "hd",
        "caption": "false",
        "licensedContent": false
      },
      "status": {
        "uploadStatus": "processed",
        "privacyStatus": "public",
        "license": "youtube",
        "embeddable": true,
        "publicStatsViewable": true
      },
      "statistics": {
        "viewCount": "26962",
        "likeCount": "19",
        "dislikeCount": "1",
        "favoriteCount": "0",
        "commentCount": "4"
      }
    }
  ]
}

items.statisticsで評価やお気に入り数が取得出来る

オプションについて

  • Youtube.addParam('パラメータ名', '値') → 検索パラメータの指定
    ex. 再生回数順に並べる
addParamの例(再生回数順に並べる)
youtube.addParam('order','viewCount');

設定可能なその他パラメータについては
https://developers.google.com/youtube/v3/docs/search/list?hl=ja#try-it

感想

割と簡単に出来ましたが、そもそもAPI的に詳細な検索が出来るようにはなってないみたいです。
もうちょっと検索条件指定できれば面白そうなのですが。。。

8
12
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
8
12