Skip to content

Instantly share code, notes, and snippets.

@chkk525
Created January 3, 2021 07:48
Show Gist options
  • Save chkk525/565fde89b85dab02900f25270a4e8193 to your computer and use it in GitHub Desktop.
Save chkk525/565fde89b85dab02900f25270a4e8193 to your computer and use it in GitHub Desktop.
くらしTEPCO webからhourlyのデータを過去Nヶ月分とってくるスクリプト。なぜかうまくいかない。(csvが空....)
LAST_N_MONTH = 1
const days = () => {
const result = []
for (let m of [...Array(LAST_N_MONTH).keys()]) {
const today = new Date()
const dt = new Date(today.setMonth(today.getMonth() - m))
const lastDay = new Date(dt.getFullYear(), dt.getMonth() + 1, 0).getDate()
for (let d of [...Array(lastDay).keys()].map(d => d + 1).sort((a, b) => b - a)) {
result.push({
"year": dt.getFullYear(),
"month": dt.getMonth() + 1,
"day": d,
})
}
}
return result
}
const get_hourly_data = () => {
const get_options = get_login_options()
const sheet = SpreadsheetApp.getActive().getSheetByName('hourly')
let allResult = []
for (let dt of days()) {
let csv_url = `https://www.kurashi.tepco.co.jp/pf/ja/pc/mypage/learn/comparison.page?ReqID=CsvDL&year=${dt.year}&month=${dt.month}&day=${dt.day}`;
const response = UrlFetchApp.fetch(csv_url, get_options);
let content = response.getContentText("Shift-JIS");
if (content.length) {
let result = Utilities.parseCsv(content).slice(1)
allResult = [...allResult, ...result]
}
}
sheet.getRange(1, 1, allResult.length, allResult[0].length).setValues(allResult)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment