-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
80 lines (67 loc) · 2.83 KB
/
main.ts
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
import { chromium, Browser, Page, ElementHandle } from "playwright";
const BASE_URL = "https://www.breakingpoint.gg";
async function navigateToMatches() {
// Launch a Chromium browser instance
const browser: Browser = await chromium.launch({
devtools: true,
headless: false,
});
const page: Page = await browser.newPage();
try {
// Navigate to the URL
await page.goto(`${BASE_URL}/matches`, { waitUntil: "domcontentloaded" });
// Click the "completed" button
await page.getByRole("button", { name: /Completed/ }).click();
console.log('[debug] clicked on the "completed" button');
// Using page.$$ to find all anchor tags starting with '/matches/'
const anchorTags: ElementHandle[] = await page.$$('a[href^="/match/"]');
const hrefs = await Promise.all(
anchorTags.map((tag) => tag.getAttribute("href")),
);
// Some links on the page have a similar URL but link to comments. We ignore those links
// Here's an example of what those ignored links look like:
// https://www.breakingpoint.gg//match/27345?commentsFocused=1
const hrefsForMatches = hrefs.filter(
(href) => href && !href.includes("commentsFocused"),
);
// Iterate over found anchor tags
for (let i = 0; i < hrefsForMatches.length; i++) {
const matchURL = `${BASE_URL}${hrefsForMatches[i]}`;
console.log(`[debug] Navigating to ${matchURL}`);
await page.goto(matchURL, { waitUntil: "domcontentloaded" });
const tabs = await page.getByRole("tab").all();
console.log(`[debug] found ${tabs.length} tabs`);
// For each tab, extract some data;
for (let i = 1; i < tabs.length; i += 1) {
try {
const tab = tabs.at(i);
await tab?.click();
// TODO:
// parse the HTML and extract data from the table
//
//
// const rows = await tab?.getByRole('row').all() || [];
// for (const row of rows) {
// const columns = await row.$$('td');
// const player = await columns[0].innerText();
// const kills = await columns[1].innerText();
// const deaths = await columns[2].innerText();
// const kd = await columns[3].innerText();
// const plusMinus = await columns[4].innerText();
// const damage = await columns[5].innerText();
// console.log(`${player}\t${kills}\t${deaths}\t${kd}\t${plusMinus}\t${damage}`);
// }
} catch (error) {
console.error(`Failed to navigate to tab #${i + 1}/${tabs.length}`);
}
}
}
} catch (error) {
console.error("Error navigating to matches:", error);
} finally {
// Optional: uncomment this line out to leave the browser open after executing
// await browser.close();
}
}
// Execute the function
navigateToMatches();