Skip to content

Commit

Permalink
improving aram helper
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloendoh committed Jun 3, 2024
1 parent 8cf9438 commit 07d648e
Showing 1 changed file with 20 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,21 @@ export class _CacheLolGraphAramStats {
async exec(lolgraphsUrl: string): Promise<MyLolGraphAramStats[]> {
const cached = await this.redis.get(redisKeys.lolgraphsUrl(lolgraphsUrl))
if (cached) {
return JSON.parse(cached)
const parsed = JSON.parse(cached) as {
data: MyLolGraphAramStats[]
expiresAt: number
}
if (parsed.expiresAt > Date.now()) {
this.#scrapeAndSaveData(lolgraphsUrl)
}

return parsed.data
}

return this.#scrapeAndSaveData(lolgraphsUrl)
}

async #scrapeAndSaveData(lolgraphsUrl: string) {
if (myEnvs.IS_DOCKER) {
return []
}
Expand All @@ -35,7 +47,7 @@ export class _CacheLolGraphAramStats {

await page.goto(lolgraphsUrl, { waitUntil: "networkidle2" })

const x = await page.evaluate(() => {
const data = await page.evaluate(() => {
const table = document.querySelector(".summoner_champions_details_table")
const tbody = table?.querySelector("tbody")
const trs = Array.from(tbody?.querySelectorAll("tr") || [])
Expand Down Expand Up @@ -69,42 +81,13 @@ export class _CacheLolGraphAramStats {

await this.redis.set(
redisKeys.lolgraphsUrl(lolgraphsUrl),
JSON.stringify(x),
"EX",
60 * 60 * 24 // 1 day

JSON.stringify({
data,
expiresAt: Date.now() + 60 * 60 * 24,
})
)

return x

// let html = ""
// const cachedHtml = await this.redis.get(
// redisKeys.lolgraphsUrl(lolgraphsUrl)
// )
// if (cachedHtml) {
// html = cachedHtml
// } else {
// let data = await this.axios.get(lolgraphsUrl).catch((err) => {
// console.log(err)
// throw err
// })
// await this.redis.set(
// redisKeys.lolgraphsUrl(lolgraphsUrl),
// data.data,
// "EX",
// 60 * 60 * 24 // 1 day
// )
// }
// const dom = new JSDOM(html).window.document
// const table = dom.querySelector(".summoner_champions_details_table")
// const tbody = table?.querySelector("tbody")
// const trs = Array.from(tbody?.querySelectorAll("tr") || [])
// for (const tr of trs) {
// const championName = tr.querySelector(".name")?.textContent
// const progressBarTxts = tr.querySelectorAll(".progressBarTxt")
// const played = progressBarTxts[0].textContent
// const winRate = progressBarTxts[1].textContent
// console.log(championName, played, winRate)
// }
// console.log(table)
return data
}
}

0 comments on commit 07d648e

Please sign in to comment.