Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | import Logger from "bunyan"; import { DailyUsageModel, FetchSettingModel, MonthlyUsageModel, UsageFetcher, } from "../../domain/Electricity"; import { CommonEnv, Env } from "../../Env"; import { Browser, withBrowser } from "../Browser"; import { BrowserPage } from "../BrowserPage"; import { nthMatch } from "../ScrapeUtils"; export class KireiLifePlusClient implements UsageFetcher { constructor( readonly commonEnv: CommonEnv, readonly loginUrl: string, readonly screenshotDir: string, readonly timeoutMs: number ) {} async fetchMonthly( parentLogger: Logger, setting: FetchSettingModel ): Promise<MonthlyUsageModel[]> { const logger = parentLogger.child({ term: "monthly" }); logger.info("fetch monthly start"); let usages: MonthlyUsageModel[] = []; let screenshotNo: number = 0; try { await withBrowser( this.timeoutMs, async (browser: Browser): Promise<void> => { const page = new BrowserPage( await browser.newPage(logger), this.screenshotDir, "electricity/kireilife/monthly", "electricity-" ); try { // ログインしてトップページに移動 await this.login(logger, setting, page); // 今月の電気料金のページに移動 logger.info("click detail link"); await page.instance .locator( "#headerRyoukin > table.header_ryoukin_tbl.desktop_only > tbody > tr > td:nth-child(2) > a" ) .click(); // 最新のものは取得できなくなったのでコメントアウト // await page.instance.locator("#menu_gaiyou").click(); // 電気料金を取得 // usages.push( // await this.fetchLatestUsage( // logger, // setting, // page, // "latest-usage.png" // ) // ); // 毎月の電気料金の一覧ページに移動 logger.info("click menu_month button"); await page.instance.locator("#menu_month").click(); // 電気料金を取得 usages = usages.concat( await this.fetchUsageHistory( logger, setting, page, "usage-history.png" ) ); } catch (err) { await page.screenshotForError(); throw err; } } ); } finally { logger.info("fetch monthly end"); } return usages; } async fetchDaily( parentLogger: Logger, setting: FetchSettingModel ): Promise<DailyUsageModel[]> { const logger = parentLogger.child({ term: "daily" }); logger.info("fetch daily start"); let usages: DailyUsageModel[] = []; try { await withBrowser( this.timeoutMs, async (browser: Browser): Promise<void> => { const page = new BrowserPage( await browser.newPage(logger), this.screenshotDir, "electricity/kireilife/daily", "electricity-" ); try { // ログインしてトップページに移動 await this.login(logger, setting, page); // 今月の電気料金のページに移動 logger.info("click detail link"); await page.instance .locator( "#headerRyoukin > table.header_ryoukin_tbl.desktop_only > tbody > tr > td:nth-child(2) > a" ) .click(); // 1日ごとの電気料金の一覧ページに移動 logger.info("click menu_day button"); await page.instance.locator("#menu_day").click(); // 電気料金を取得 usages = await this.fetchDailyUsages( logger, setting, page, "daily-usages.png" ); } catch (err) { await page.screenshotForError(); throw err; } } ); } finally { logger.info("fetch daily end"); } return usages; } private async login( logger: Logger, setting: FetchSettingModel, page: BrowserPage ): Promise<void> { const userName = setting.userName; const password = setting.password; // ログインページに移動 logger.info("goto login page"); await page.instance.goto(this.loginUrl); await page.screenshot("login-page.png"); // ログイン logger.info("input id and password"); await page.instance.locator("#k_id").fill(userName); await page.instance.locator("#k_pw").fill(password.value()); await page.screenshot("login-page-with-id-pw.png"); logger.info("click login button"); await page.instance .locator("#loginFormDtl") .locator(".headLoginbtn") .click(); logger.info("wait for loading top page"); await page.instance.locator(".loginName").waitFor(); await page.screenshot("top-page.png"); } private async fetchUsageHistory( logger: Logger, setting: FetchSettingModel, page: BrowserPage, screenshotName: string ): Promise<MonthlyUsageModel[]> { logger.info("wait for loading usage history"); const tables = await page.instance.locator( "#wrapper > div > div > table.desktop_only.table01.mt-1.monthJisekiTbl.conditionBackgroud" ); await tables.waitFor(); await page.screenshot(screenshotName); logger.info("fetch usage history"); const rows = await tables.locator("tbody > tr"); const usages: MonthlyUsageModel[] = []; for (let i = 0; i < (await rows?.count()); i++) { Iif (i == 0) { continue; } const columns = await rows.nth(i).locator("td"); const yearMonth = (await columns.nth(0).innerText()).split("/"); usages.push({ fetchSettingId: setting.id, year: Number(yearMonth[0]), month: Number(yearMonth[1]), dayCount: Number(await columns.nth(5).innerText()), kwh: Number(await columns.nth(2).innerText()), yen: Number((await columns.nth(3).innerText()).replace(",", "")), }); } return usages; } private async fetchLatestUsage( logger: Logger, setting: FetchSettingModel, page: BrowserPage, screenshotName: string ): Promise<MonthlyUsageModel> { logger.info("wait for loading latest usages"); await page.instance.locator("#gaiyouGaisan").waitFor(); await page.screenshot(screenshotName); logger.info("fetch latest usage"); const prevDateLabel = ( await page.instance.locator("#gaiyouJiseki > div.gaiyouTitle").innerText() ) .replace("年", "") .replace("月分の実績", ""); const prevYear = Number(prevDateLabel.substring(0, 4)); const prevMonth = Number(prevDateLabel.substring(4)); const current = new Date(prevYear, prevMonth - 1, 1); current.setMonth(current.getMonth() + 1); const dayCount = Number( await page.instance .locator("#gaiyouGaisan > div:nth-child(4) > span:nth-child(2)") .innerText() ); const kwh = Number( await page.instance .locator("#gaiyouGaisan > div:nth-child(3) > span") .innerText() ); const yen = Number( ( await page.instance .locator("#gaiyouGaisan > div.blockGaiyou.ml10 > span") .innerText() ).replace(",", "") ); return { fetchSettingId: setting.id, year: current.getFullYear(), month: current.getMonth() + 1, dayCount: dayCount, kwh: kwh, yen: yen, }; } private async fetchDailyUsages( logger: Logger, setting: FetchSettingModel, page: BrowserPage, screenshotName: string ): Promise<DailyUsageModel[]> { logger.info("wait for loading daily usages"); const frame = await page.instance.frameLocator("#mieruframe"); await page.screenshot(screenshotName); logger.info("fetch daily usages"); // タイトルから期間の開始年を抽出 const title = await frame.locator("#mi_title").innerText(); const beginYear = Number(nthMatch(title, /([0-9]+)年[0-9]+月[0-9]+日/, 1)); logger.debug("begin year is %d, title[%s]", beginYear, title); const table = await frame.locator("#mi_month_list_table"); const rows = await table.locator("tbody > tr"); const rowCount = await rows.count(); logger.debug("daily usages row count: %d", rowCount); const usages: DailyUsageModel[] = []; let year = beginYear; for (let i = 0; i < rowCount; i++) { Iif (i == 0) { continue; } const columns = await rows.nth(i).locator("td"); Iif ((await columns.count()) == 0) { continue; } const dateStr = await columns.nth(0).innerText(); const month = Number(nthMatch(dateStr, /[0-9]+月/, 0).replace("月", "")); const date = Number(nthMatch(dateStr, /[0-9]+日/, 0).replace("日", "")); const amountStr = (await columns.nth(1).innerText()) .replace("*", "") .trim(); Iif (amountStr == "") { continue; } const amount = Number(amountStr); const usage: DailyUsageModel = { fetchSettingId: setting.id, year: year, month: month, date: date, amount: amount, }; logger.debug("usage: %j", usage); usages.push(usage); Iif (month == 12 && date == 31) { year++; } } return usages; // logger.info("fetch latest usage"); // const prevDateLabel = ( // await page.locator("#gaiyouJiseki > div.gaiyouTitle").innerText() // ) // .replace("年", "") // .replace("月分の実績", ""); // const prevYear = Number(prevDateLabel.substring(0, 4)); // const prevMonth = Number(prevDateLabel.substring(4)); // const current = new Date(prevYear, prevMonth - 1, 1); // current.setMonth(current.getMonth() + 1); // const dayCount = Number( // await page // .locator("#gaiyouGaisan > div:nth-child(4) > span:nth-child(2)") // .innerText() // ); // const kwh = Number( // await page.locator("#gaiyouGaisan > div:nth-child(3) > span").innerText() // ); // const yen = Number( // ( // await page // .locator("#gaiyouGaisan > div.blockGaiyou.ml10 > span") // .innerText() // ).replace(",", "") // ); // return { // year: current.getFullYear(), // month: current.getMonth() + 1, // dayCount: dayCount, // kwh: kwh, // yen: yen, // }; } } |