Skip to content

Instantly share code, notes, and snippets.

@Twilyze
Last active January 26, 2020 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Twilyze/2a20f538e1b5818749230090c3bd4de5 to your computer and use it in GitHub Desktop.
Save Twilyze/2a20f538e1b5818749230090c3bd4de5 to your computer and use it in GitHub Desktop.
ニコニコ動画のマイリストから「とりあえずマイリスト」へまとめて移動(登録)させる https://twilyze.hatenablog.jp/entry/nico-mylist-move
(function() {
const startIndex = 0;
const mylistID = location.href.match(/https:\/\/www.nicovideo.jp\/my\/mylist\/#\/(\d+)/)[1];
function main(mylist) {
let i = startIndex;
let addCount = 0;
const len = mylist.mylistitem.length;
const wait = 5e3;
function addDeflist(i) {
const mylistitem = mylist.mylistitem[i];
const item_id = mylistitem.item_id;
const token = NicoAPI.token;
const url = 'https://www.nicovideo.jp/api/deflist/add?item_id=' + item_id + '&token=' + token;
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
const res = JSON.parse(xhr.responseText);
if (res) {
const progress = (i + 1) + '/' + len;
const data = {
'item_id' : item_id,
'videoID' : mylistitem.item_data.video_id,
'title' : mylistitem.item_data.title,
};
if (res.status === 'ok') {
addCount++;
console.log('OK:' + progress, data);
}
else {
console.error('ERROR:' + progress, data, res.error);
if (res.error.code === "EXPIRETOKEN") {
clearInterval(id);
console.info('ページ更新後 startIndex を ' + i + ' にして再度実行してください');
return;
}
}
}
if (i + 1 >= len) {
console.info('%cFinish! 登録:' + addCount + '/' + len, 'color: green; font-size: 16px');
clearInterval(id);
}
}
};
xhr.send(null);
};
console.info('登録件数: ' + len + '件\n処理時間: ' + (len * wait / 6e4).toFixed(1) + '分');
addDeflist(i);
const id = setInterval(function() {
i++;
addDeflist(i);
}, wait);
};
if (!Number.isFinite(parseInt(mylistID, 10)))
console.error('マイリストIDが正しくありません。 mylistID:%d', mylistID);
else {
const xhr = new XMLHttpRequest();
xhr.open('GET', "https://www.nicovideo.jp/api/mylist/list?group_id=" + mylistID, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
const mylist = JSON.parse(xhr.responseText);
if (mylist && mylist.status === 'ok')
main(mylist);
}
};
xhr.send(null);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment