その4~5で作ったスクリプトのソースコードを公開しておきます。
function getLivePage(pageNum) {
var url = "http://ch.nicovideo.jp/mentalist/live?&page=" + String(pageNum);
var response = UrlFetchApp.fetch(url);
return response.getContentText();
}
function subPastParser(htmlText) {
// 一致する文字列が返る
return htmlText.match(/<section class=\"sub past\">([\s\S]*?)<\/section>/m)[1];
}
function liveItemParser(htmlText) {
// 一致する文字列の"配列"が返る
return htmlText.match(/<div class=\"item_right\">[\s\S]*?<\/div>/gm);
}
function getLiveInfo(htmlText) {
var returnObject = {
/*
lv_name: {
title : "オックスフォードからお届け",
date : "2018/08/08",
dayOfWeek : "水",
time : "20:00:00"
},
*/
};
var liveInfoArray = liveItemParser(htmlText);
for(var i = 0; i < liveInfoArray.length; i++){
var liveName = liveInfoArray[i].match(/<a href=\"http:\/\/live.nicovideo.jp\/gate\/([\s\S]*?)\">/)[1];
var title = liveInfoArray[i].match(/<h2 class=\"title\">[\s\S]*?<a href=\"[\s\S]*?\">([\s\S]*?)<\/a>/m)[1];
var dateInfo = liveInfoArray[i].match(/<p class=\"date\">([\s\S]*?)<\/p>/m)[1].trim();
var date = dateInfo.match(/\d{4}\/\d{2}\/\d{2}/)[0];
var dayOfWeek = dateInfo.match(/\(([\s\S])\)/)[1];
var time = dateInfo.match(/\d{2}:\d{2}:\d{2}/)[0];
returnObject[liveName] = {
title : title,
date : date,
dayOfWeek : dayOfWeek,
time : time
};
}
return returnObject;
}
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var headerArray = ["live name", "title", "date", "day of week", "time"];
sheet.clear();
sheet.appendRow(headerArray);
var pageNum = 1;
while (true) {
var text = getLivePage(pageNum);
var subPastText = subPastParser(text);
var liveInfo = getLiveInfo(subPastText);
var nextPage = text.match(/<li class=\"next([\s\S]*?)\">/)[1]; // disabledが入ってたら"disabled"
for(var key in liveInfo){
var writeArray = [];
writeArray.push(key); // live name
writeArray.push(liveInfo[key].title); // title
writeArray.push(liveInfo[key].date); // date
writeArray.push(liveInfo[key].dayOfWeek); // day of week
writeArray.push(liveInfo[key].time); // time
sheet.appendRow(writeArray);
}
if(nextPage !== ""){
break;
}
else {
pageNum++;
}
}
}