Skip to content

Instantly share code, notes, and snippets.

@kiuchikeisuke
Created May 18, 2017 06:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiuchikeisuke/fcf4c7015cf3bc4cfbc2c0c084e93016 to your computer and use it in GitHub Desktop.
Save kiuchikeisuke/fcf4c7015cf3bc4cfbc2c0c084e93016 to your computer and use it in GitHub Desktop.
バス通知くん
Const = {
LINEToken: "XXXXXXXXXXXXXXXXXXXXXXXXXXX",
spreadSheetId: "YYYYYYYYYYYYYYYYYYYYYYYYY",
weekDayIndex: 0,
satDayIndex: 1,
sunDayIndex: 2
}
function doPost(e) {
var terminal = e.parameters["terminal"][0];
var now = new Date();
var msg = generateResentBusMsg(terminal, now);
notify2Line(msg);
}
function generateResentBusMsg(terminal, now) {
var nowHours = now.getHours();
var nowMinutes = now.getMinutes();
var day = now.getDay();
var data = getTerminalData(terminal);
var index;
var terminalName = terminal == "sibuya" ? "渋谷" : "東京";
if (day == 0) {
index = Const.sunDayIndex;
} else if (day == 6) {
index = Const.satDayIndex;
} else {
index = Const.weekDayIndex;
}
var recent = "なし";
var next = "なし";
for(var i = 0; i < data[index].length; i++) {
var time = data[index][i];
var timeHours = time.getHours();
var timeMinutes = time.getMinutes();
if (recent != "なし") {
next = formatTime2Str(timeHours, timeMinutes);
break;
}
if ( nowHours == timeHours ) {
if ( nowMinutes < timeMinutes ) {
recent = formatTime2Str(timeHours, timeMinutes);
}
} else if ( nowHours < timeHours) {
recent = formatTime2Str(timeHours, timeMinutes);
}
}
return terminalName + "\n直近[" + recent + "]\n次発[" + next + "]";
}
function formatTime2Str(hours, minutes) {
return ("0" + hours).slice(-2) + ":" + ("0" + minutes).slice(-2);
}
function getTerminalData(terminal) {
var sheet = getSheet(terminal);
return sheet.getDataRange().getValues();
}
function getSheet(terminal) {
if (getSheet.instance) { return getSheet.instance; }
var sheet = SpreadsheetApp.openById(Const.spreadSheetId).getSheetByName(terminal);
return sheet;
}
function notify2Line(msg) {
var options =
{
"method" : "post",
"payload" : "message=" + msg,
"headers" : {"Authorization" : "Bearer "+ Const.LINEToken}
};
UrlFetchApp.fetch("https://notify-api.line.me/api/notify",options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment