Skip to content

Instantly share code, notes, and snippets.

@ayakix
Last active August 29, 2015 14:16
Show Gist options
  • Save ayakix/80270b84a0f65773b09b to your computer and use it in GitHub Desktop.
Save ayakix/80270b84a0f65773b09b to your computer and use it in GitHub Desktop.
ミスドが100円セール中かどうかをチェックするAPI
function doGet(e) {
var json = {"isSale": false};
var response;
try {
response = UrlFetchApp.fetch("http://www.misterdonut.jp/sale/index.html");
} catch(err) {
return createOutput(json);
}
var regexp = /(\d+)年(\d+)月(\d+)日.*(\d+)月(\d+)日/;
var match = regexp.exec(response.getContentText("Shift_JIS"));
if (match == null && match.length != 6) {
return createOutput(json);
}
var sDate = new Date(match[1] + "/" + match[2] + "/" + match[3]);
var eDate = new Date(match[1] + "/" + match[4] + "/" + match[5] + " 23:59:59");
var nDate = new Date();
if (sDate.getTime() <= nDate.getTime() && nDate.getTime() <= eDate.getTime()) {
json = {
"isSale": true,
"sDate" : formatDate(sDate),
"eDate" : formatDate(eDate)
};
}
return createOutput(json);
}
function createOutput(json) {
return ContentService.createTextOutput(JSON.stringify(json)).setMimeType(ContentService.MimeType.JSON);
}
function formatDate(date) {
toDouble = function(d) { return ("0"+d).slice(-2); };
return date.getFullYear() + "/" +
(date.getMonth() + 1) + "/" +
date.getDate() + " " +
toDouble(date.getHours()) + ":" +
toDouble(date.getMinutes()) + ":" +
toDouble(date.getSeconds());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment