Skip to content

Instantly share code, notes, and snippets.

@gc373
Last active June 30, 2017 15:12
Show Gist options
  • Select an option

  • Save gc373/b612fe77a72a691d9a11c87d99f428fc to your computer and use it in GitHub Desktop.

Select an option

Save gc373/b612fe77a72a691d9a11c87d99f428fc to your computer and use it in GitHub Desktop.
Youtubeのチャンネル/ユーザーに新規動画が追加されたら自分のプレイリストに追加するやつ
/*
* Check Youtube channel.
* When upload a video to target channel, add to my playlist.
*
* 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 = process.env.REFRESH_TOKEN;
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
const REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
const MY_PLAYLISTID = process.env.MY_PLAYLISTID;
const TARGET_PLAYLISTID = process.env.TARGET_PLAYLISTID;
*/
const REFRESH_TOKEN = "";
const CLIENT_ID = "";
const CLIENT_SECRET = "";
const REDIRECT_URI = "";
const MY_PLAYLISTID = "";
const TARGET_PLAYLISTID = "";
let last_id;
let nextpage_token;
let token_timestamp;
init();
function init() {
nextpage_token = null;
chkToken();
setTimeout(init, 1000 * 60);
}
function chkToken() {
if (!token_timestamp || !nextpage_token && (new Date() - token_timestamp) / (1000 * 60 * 60) > 1) {
Youtube.authenticate({
type: "oauth",
refresh_token: REFRESH_TOKEN,
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
redirect_uri: REDIRECT_URI
}, () => {
token_timestamp = new Date();
console.log("token is now refresh", token_timestamp.toLocaleString());
});
}
if (!last_id) {
getLastId();
} else {
getTargetPlaylist();
}
}
function getLastId(nextpage_token) {
Youtube.playlistItems.list({
"part": "snippet",
"maxResults": 50,
"playlistId": MY_PLAYLISTID,
"pageToken": nextpage_token
}, (err, data) => {
if (err) {
console.log(err)
} else if (data.nextPageToken) {
nextpage_token = data.nextPageToken;
getLastId(nextpage_token);
} else {
last_id = data.items[data.items.length - 1].snippet.resourceId.videoId;
getTargetPlaylist();
}
});
}
function getTargetPlaylist() {
Youtube.playlistItems.list({
"part": "snippet",
"order": "date",
"maxResults": 5,
"playlistId": TARGET_PLAYLISTID
}, (err, data) => {
if (err) {
console.log(err)
} else if (last_id == data.items[0].snippet.resourceId.videoId) {
console.log("no update [", (new Date()).toLocaleString(), "]\r\nID:", last_id);
} else {
let update_flg = false;
let items = data.items.reverse();
for (let i = 0, max = items.length; i < max; i++) {
let item_id = items[i].snippet.resourceId.videoId;
if (update_flg) {
console.log("########### New Video! ###########\r\n# add to playlist -> ", item_id, "\r\n##################################");
Youtube.playlistItems.insert({
resource: {
snippet: {
"playlistId": MY_PLAYLISTID,
"resourceId": {
"kind": "youtube#video",
"videoId": item_id
}
}
},
part: "snippet"
}, (err, data) => {
if (err) {
console.log(err)
return;
} else {
console.log(data)
}
});
last_id = item_id;
}
if (item_id == last_id) {
update_flg = true
}
}
}
})
}
@gc373
Copy link
Author

gc373 commented Jun 15, 2017

20170615
Fix bug(expired token)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment