Created
January 3, 2021 07:44
-
-
Save chkk525/9f0c2a903d95c3f3504e935a4051520a to your computer and use it in GitHub Desktop.
くらしTEPCO webからデイリーの消費電力のデータを落としてくるスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const get_login_options = () => { | |
const LOGIN_URL = "https://www.kurashi.tepco.co.jp/kpf-login"; | |
const payload_data = { | |
ACCOUNTUID: 'test@gamil.com', | |
PASSWORD: 'secure-password-1234', | |
HIDEURL: '/pf/ja/pc/mypage/home/index.page?', | |
LOGIN: 'EUAS_LOGIN' | |
}; | |
const post_options = { | |
method: "post", | |
payload: payload_data, | |
followRedirects: false | |
}; | |
let response = UrlFetchApp.fetch(LOGIN_URL, post_options); | |
const cookies1 = response.getAllHeaders()["Set-Cookie"] | |
const STATS_URL = "https://www.kurashi.tepco.co.jp/pf/ja/pc/mypage/learn/comparison.page" | |
response = UrlFetchApp.fetch(LOGIN_URL, post_options); | |
const cookies2 = [...response.getAllHeaders()["Set-Cookie"], ...cookies1] | |
const cookiesStr = cookies2.join('; ') | |
const headers = { | |
"Cookie": cookiesStr | |
} | |
const get_options = { | |
method: "get", | |
headers: headers, | |
followRedirects: true, | |
}; | |
return get_options | |
} | |
const get_daily_data = () => { | |
const get_options = get_login_options() | |
const sheet = SpreadsheetApp.getActive().getSheetByName('daily') | |
let allResult = [] | |
const thisYear = (new Date).getFullYear() | |
for (let y of [...Array(3).keys()].map(d => thisYear - d)) { | |
for (let m of [...Array(12).keys()].map((m) => m + 1)) { | |
let csv_url = `https://www.kurashi.tepco.co.jp/pf/ja/pc/mypage/learn/comparison.page?ReqID=CsvDL&year=${y}&month=${m}`; | |
Logger.log(csv_url) | |
response = UrlFetchApp.fetch(csv_url, get_options); | |
Utilities.sleep(300) | |
let content = response.getContentText("Shift-JIS"); | |
Logger.log(content) | |
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