-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
167 lines (150 loc) · 5.92 KB
/
main.js
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
"use strict";
// install dependencies:
//
// npm install
//
// execute:
//
// NB to troubleshoot uncomment $env:DEBUG and set {headless:false,dumpio:true} in main.js.
//
// $env:DEBUG = 'puppeteer:*'
// node main.js
import { program } from "commander";
import puppeteer from "puppeteer";
import fs from "fs";
import path from "path";
function log() {
console.log.apply(console, [new Date().toISOString(), ...arguments]);
}
async function getDrivers(page, product) {
const url = `https://www.dell.com/support/home/en-us/product-support/product/${product}/drivers`;
log(`Loading ${url}...`);
await page.goto(url);
log("Rejecting cookies...");
const rejectCookiesSelector = '[aria-label="dismiss cookie message"]';
try {
await page.waitForSelector(rejectCookiesSelector, {timeout: 5000});
await page.click(rejectCookiesSelector);
} catch {
// ignore. this is expected in countries without cookies consent.
}
log("Selecting the United States/English region...");
const countrySelector = "div.mh-top div.country-selector";
const currentCountry = await page.evaluate((countrySelector) => document.querySelector(countrySelector).innerText.trim(), countrySelector);
if (currentCountry != "US/EN") {
await page.hover(`${countrySelector} a`);
await page.waitForSelector(`${countrySelector} [data-region-id="Americas"]`);
await page.click(`${countrySelector} [data-region-id="Americas"]`);
await page.click(`${countrySelector} a[data-locale="en-us"]`);
await page.waitForNavigation();
}
log("Waiting for the downloads table...");
const downloadsTableSelector = "#downloads-table";
await page.waitForSelector(`${downloadsTableSelector} tr:nth-child(5)`);
await page.waitForSelector(`${downloadsTableSelector} #paginationRow`);
await page.click(`${downloadsTableSelector} #paginationRow`);
log("Getting data from the downloads table...");
return await page.evaluate(async () => {
// e.g. 12 Jan 2023
const dateRe = /0?(?<day>[0-9]+) (?<month>[A-Za-z]+) (?<year>[0-9]+)/;
const dateMonths = {
Jan: 1,
Feb: 2,
Mar: 3,
Apr: 4,
May: 5,
Jun: 6,
Jul: 7,
Aug: 8,
Sep: 9,
Oct: 10,
Nov: 11,
Dec: 12,
}
function parseDate(s) {
const m = dateRe.exec(s);
const day = parseInt(m.groups.day, 10);
const month = dateMonths[m.groups.month];
const year = parseInt(m.groups.year, 10);
return new Date(Date.UTC(year, month - 1, day));
}
var data = [];
const els = document.querySelectorAll("#downloads-table tr.main-row");
for (const el of els) {
const columnEls = el.querySelectorAll("td");
if (columnEls.length < 7) {
continue;
}
const name = columnEls[1].innerText.trim();
const importance = columnEls[2].innerText.trim().toLowerCase();
const category = columnEls[3].innerText.trim();
const date = parseDate(columnEls[4].innerText.trim());
const url = columnEls[5].querySelector("a.btn-download-lg").getAttribute("href").trim().replaceAll(" ", "%20");
const driver = {
name: name,
category: category,
importance: importance,
date: date.toISOString(),
url: url,
};
if (false) {
columnEls[6].querySelector("button").click();
const detailsEl = el.nextSibling && document.evaluate("//a[text()='View full driver details']", el.nextSibling, null, XPathResult.ANY_TYPE, null).iterateNext();
const detailsUrl = detailsEl && detailsEl.getAttribute("href");
columnEls[6].querySelector("button").click();
driver.detailsUrl = detailsUrl;
}
data.push(driver);
}
return data;
});
}
async function main(options) {
var browserConfig = {
args: [
"--start-maximized"
],
headless: "new",
};
if (options.debug) {
browserConfig = {
...browserConfig,
headless: false,
devtools: true,
slowMo: 250,
dumpio: false,
};
}
log("Launching the browser...");
const browser = await puppeteer.launch(browserConfig);
try {
log("Creating a new browser page...");
const page = await browser.newPage();
await page.setViewport({
width: parseInt(options.viewportSize.split('x')[0], 10),
height: parseInt(options.viewportSize.split('x')[1], 10),
deviceScaleFactor: 1,
});
log(`Launched the ${await browser.version()} browser.`);
try {
const product = "optiplex-7060-desktop";
const scrapePath = `data/${product}.json`;
log(`Scraping ${product}...`);
const data = await getDrivers(page, product);
log(`Saving to ${scrapePath}...`);
fs.mkdirSync(path.dirname(scrapePath), {recursive: true});
fs.writeFileSync(scrapePath, JSON.stringify(data, null, 4));
} finally {
log("Taking a screenshot...");
await page.screenshot({ path: options.screenshotPath, fullPage: true });
}
} finally {
await browser.close();
}
}
program
.option('--screenshot-path <path>', 'screenshot output path', 'screenshot.png')
.option('--viewport-size <size>', 'browser viewport size', '1280x720')
.option('--debug', 'run the browser in foreground', false)
.parse(process.argv);
await main(program.opts());