Skip to content

Instantly share code, notes, and snippets.

@freddiefujiwara
Last active July 20, 2021 06:17
Show Gist options
  • Save freddiefujiwara/ba4180f49d84cea1eaafe1ad309bd024 to your computer and use it in GitHub Desktop.
Save freddiefujiwara/ba4180f49d84cea1eaafe1ad309bd024 to your computer and use it in GitHub Desktop.
5931bus.com crawler
const sheet = SpreadsheetApp.getActiveSheet();
function doGet(e) {
refresh();
const values = sheet.getDataRange().getValues();
const header = [];
values.forEach(h => {
header.push(h[0]);
});
const target = {
"川口": 1,
"蕨": 2,
"草加": 3,
"イオンモール川口": 4
};
const results = {};
Object.keys(target).forEach(k => {
const result = {}
values.forEach((v,i) => {
if(typeof v[target[k]] == "object"){
const t = new Date(new Date(v[target[k]]).toLocaleString("en-US", {timeZone: "Asia/Tokyo"}));
const h = t.getHours();
const m = t.getMinutes();
result[header[i]] = `${h}:${m > 9 ? m : "0" + m}`;
return;
}
result[header[i]] = v[target[k]];
});
results[result["destination"]] = result;
});
const output = ContentService.createTextOutput();
if (e.parameter.callback === undefined) {
output.setMimeType(ContentService.MimeType.JSON);
output.setContent(JSON.stringify(results));
} else {
output.setMimeType(ContentService.MimeType.JAVASCRIPT);
output.setContent(e.parameter.callback + "&&" + e.parameter.callback + "(" + JSON.stringify(results) + ");");
}
return output;
}
function refresh() {
const kawaguchi = sheet.getRange("B2");
kawaguchi.setFormula(kawaguchi.getFormula().replace(/\?[^"]*"/, '?VID=lsc&EID=nt&ASMK=539&DSMK=2604&refresh=' + Date.now() + '"'));
const warabi = sheet.getRange("C2");
warabi.setFormula(warabi.getFormula().replace(/\?[^"]*"/, '?VID=lsc&EID=nt&ASMK=599&DSMK=2604&refresh=' + Date.now() + '"'));
const soka = sheet.getRange("D2");
soka.setFormula(soka.getFormula().replace(/\?[^"]*"/, '?VID=lsc&EID=nt&ASMK=569&DSMK=2615&refresh=' + Date.now() + '"'));
}
{
"timeZone": "America/New_York",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"webapp": {
"executeAs": "USER_DEPLOYING",
"access": "ANYONE_ANONYMOUS"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment