-
-
Save Lycolia/65b4abe898538b6d395790be6219c0a0 to your computer and use it in GitHub Desktop.
asken.jpのPC画面で日付、体重、体脂肪率をTSV形式で取得
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
/** | |
* ## 使い方 | |
* | |
* 1. あすけん PC サイトにログイン | |
* 2. データを取得したい範囲で beginYear, endYear を設定 | |
* 3. ブラウザのコンソールにこのスクリプトを流す | |
* 4. TSV がコンソールに出力される | |
*/ | |
/** データ取得開始年 */ | |
const beginYear = 2021; | |
/** データ取得終了年 */ | |
const endYear = 2021; | |
/** 結果TSV */ | |
let resultTSV = ''; | |
/** | |
* @param {string} year | |
* @param {string} month | |
*/ | |
const getAskenMonthlyRecord = async (year, month) => { | |
return await ( | |
await fetch( | |
`https://www.asken.jp/my_diary/view_calendar/${year}/${month}/1/0/` | |
) | |
).text(); | |
}; | |
/** | |
* @param {Element} el | |
* @param {string} selectorName | |
*/ | |
const trimElmInnerText = (el, selectorName) => { | |
return el.querySelector(selectorName).innerText.trim(); | |
}; | |
/** | |
* @param {Element} el | |
* @param {string} year | |
* @param {string} month | |
* @param {string} daySl | |
* @param {string} weightSl | |
* @param {string} bodyFatSl | |
*/ | |
const getDayLine = (el, year, month, daySl, weightSl, bodyFatSl) => { | |
const day = getDay(trimElmInnerText(el, daySl)); | |
const ymd = `${year}-${month}-${day}`; | |
const weight = getWeight(trimElmInnerText(el, weightSl)); | |
const bodyFat = getBodyFat(trimElmInnerText(el, bodyFatSl)); | |
return `${ymd}\t${weight}\t${bodyFat}\n`; | |
}; | |
/** | |
* @param {string} text | |
*/ | |
const getDay = (text) => { | |
return text.replace(/\(.\)/, '').padStart(2, '0'); | |
}; | |
/** | |
* @param {string} text | |
*/ | |
const getWeight = (text) => { | |
return text.replace('kg', ''); | |
}; | |
/** | |
* @param {string} text | |
*/ | |
const getBodyFat = (text) => { | |
return text.replace('%', ''); | |
}; | |
for (let year = beginYear; year <= endYear; year++) { | |
for (let month = 1; month < 13; month++) { | |
const monthStr = month.toString().padStart(2, '0'); | |
const data = await getAskenMonthlyRecord(year.toString(), monthStr); | |
const html = document.createElement('div'); | |
html.innerHTML = data; | |
const yearByHtml = trimElmInnerText(html, '#calendar_title').replace( | |
/(\d+)年\d+月/, | |
'$1' | |
); | |
const monthByHtml = trimElmInnerText(html, '#calendar_title').replace( | |
/\d+年(\d+)月/, | |
'$1' | |
); | |
if (monthStr === monthByHtml) { | |
// monthStr !== monthByHtml の時はデータがない | |
resultTSV += [...html.querySelector('#calendar_datas').children] | |
.filter((el) => el.className.match(/calendar_body/) !== null) | |
.reduce( | |
(acc, cur) => | |
`${acc}${getDayLine( | |
cur, | |
year, | |
month, | |
'.val_date', | |
'.val_weight', | |
'.val_bodyfat' | |
)}`, | |
'' | |
); | |
} | |
} | |
} | |
console.log(resultTSV); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment