Last active
June 17, 2017 01:42
-
-
Save gc373/e64a761f3f02c9ee7fdee670a7f4a2d0 to your computer and use it in GitHub Desktop.
dotstokyoの動画一覧を取得するやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Youtubeのチャンネル/ユーザーの動画を自分の再生リストに追加するやつ(制御版) | |
| * YouTube Data API [Your Key] | |
| * https://console.developers.google.com/apis/library | |
| * | |
| * Copyright © 2017 gc373 | |
| * This work is free. You can redistribute it and/or modify it under the | |
| * terms of the Do What The Fuck You Want To Public License, Version 2, | |
| * as published by Sam Hocevar. See http://www.wtfpl.net/ for more details. | |
| * http://www.wtfpl.net/ | |
| */ | |
| const Youtube = require("youtube-api"); | |
| const REFRESH_TOKEN = ""; | |
| const CLIENT_ID = ""; | |
| const CLIENT_SECRET = ""; | |
| const REDIRECT_URI = ""; | |
| const TARGET_PLAYLISTID = "UU1K7BU8V68WMYcn-ei34oyQ"; | |
| let videoIds = []; | |
| let nextpage_token = null; | |
| let my_playlistId = null; | |
| let count = 0; | |
| getAPI(); | |
| /* | |
| * APIを叩く(最大取得件数 50件) | |
| */ | |
| function getAPI(nextpage_token) { | |
| Youtube.authenticate({ | |
| type: "oauth", | |
| refresh_token: REFRESH_TOKEN, | |
| client_id: CLIENT_ID, | |
| client_secret: CLIENT_SECRET, | |
| redirect_uri: REDIRECT_URI | |
| }); | |
| Youtube.playlistItems.list({ | |
| "part": "snippet", | |
| "order": "date", | |
| "maxResults": 50, | |
| "playlistId": TARGET_PLAYLISTID, | |
| "pageToken": nextpage_token | |
| }, function (err, data) { | |
| filter(err, data); | |
| }); | |
| } | |
| /* | |
| * 追加する動画のフィルターと後続処理の分岐 | |
| * data.nextPageTokenがある場合には全件取得するために再帰 | |
| */ | |
| function filter(err, data) { | |
| if (err) { console.log(err); return } | |
| let root = data.items; | |
| root.forEach(function (i) { | |
| if (i.snippet.title.match(/*.*/)) { // you should be add filter. | |
| videoIds.push(i.snippet.resourceId.videoId) | |
| } | |
| }) | |
| if (data.nextPageToken) { | |
| nextpage_token = data.nextPageToken; | |
| getAPI(nextpage_token); | |
| } else { | |
| createPlaylist(); | |
| } | |
| } | |
| /* | |
| * 再生リストの作成 | |
| * 作成できたら動画の追加へ | |
| */ | |
| function createPlaylist() { | |
| videoIds.reverse(); | |
| Youtube.playlists.insert({ | |
| resource: { | |
| snippet: { | |
| title: "dots.tokyo" | |
| } | |
| }, | |
| part: "snippet" | |
| }, function (err, data) { | |
| if (err) { console.log(err); return } else { | |
| my_playlistId = data.id; | |
| insertPlaylist(); | |
| } | |
| }) | |
| } | |
| /* | |
| * 動画の追加 | |
| */ | |
| function insertPlaylist() { | |
| console.log("now insert :" + videoIds[count] + " to " + my_playlistId); | |
| Youtube.playlistItems.insert({ | |
| resource: { | |
| snippet: { | |
| "playlistId": my_playlistId, | |
| "resourceId": { | |
| "kind": "youtube#video", | |
| "videoId": videoIds[count++] | |
| } | |
| } | |
| }, | |
| part: "snippet" | |
| }, function (err, data) { | |
| if (err) { | |
| console.log(err) | |
| } else if (count < videoIds.length) { | |
| insertPlaylist(); | |
| } else { | |
| console.log("done!"); | |
| } | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment