Migrate project to TypeScript and Playwright scraping

This commit is contained in:
2026-08-05 13:47:45 +02:00
parent 8ecbc6655f
commit 639d9bec75
30 changed files with 3222 additions and 1625 deletions
+53
View File
@@ -0,0 +1,53 @@
// @ts-nocheck
import { chromium } from "playwright";
const visibleBrowsers = new Set();
export async function openInBrowser(url) {
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
await page.goto(url, { waitUntil: "domcontentloaded", timeout: 30_000 });
visibleBrowsers.add(browser);
return { browser, page };
}
export async function searchInBrowser({ url, headless = true, waitAfterLoadMs = 3000 }) {
const browser = await chromium.launch({ headless });
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
try {
const response = await page.goto(url, {
waitUntil: "domcontentloaded",
timeout: 30_000,
});
await page.waitForTimeout(Math.max(250, Number(waitAfterLoadMs) || 3000));
const [currentUrl, title, pageSource] = await Promise.all([
page.url(),
page.title(),
page.content(),
]);
const cleanText = await page.locator("body").innerText().catch(() => "");
const failed = /something has gone wrong|reference id|access denied/i.test(cleanText);
return {
ok: Boolean(response?.ok()) && !failed,
status: response?.status() || 0,
url: currentUrl,
title,
text: cleanText.slice(0, 1200),
pageSource,
};
} finally {
await browser.close();
}
}
export async function scrapePage(url, options = {}) {
return searchInBrowser({ url, headless: true, waitAfterLoadMs: 750, ...options });
}
export async function closeVisibleBrowsers() {
await Promise.all([...visibleBrowsers].map((browser) => browser.close().catch(() => {})));
visibleBrowsers.clear();
}
@@ -1,5 +1,6 @@
import { executeEndpointWrite, queryEndpoint } from "../endpoint/endpoint-client.js";
import { openInFirefox, searchInFirefox } from "./firefox-adapter.js";
// @ts-nocheck
import { executeEndpointWrite, queryEndpoint } from "../endpoint/endpoint-client.ts";
import { openInBrowser, scrapePage, searchInBrowser } from "./browser-adapter.ts";
import {
prepareManufacturerCatalogSqls,
mappingSourcesSql,
@@ -8,7 +9,7 @@ import {
productCombinationsByReferenceSql,
productToDoByManufacturerSql,
suggestedProductsSql,
} from "../sql/catalog-maker.js";
} from "../sql/catalog-maker.ts";
export async function loadManufacturerCatalogState(config, { manufacturerId, position = 1 }) {
const refreshResults = [];
@@ -181,9 +182,9 @@ export async function findProductSources(
const candidate = item.candidates?.[0];
if (candidate?.url) {
try {
openInFirefox(candidate.url);
await openInBrowser(candidate.url);
} catch {
// Picture extraction can continue even if visible Firefox cannot start.
// Picture extraction can continue even if the visible browser cannot start.
}
}
}
@@ -249,13 +250,13 @@ async function findSourceCandidates(source, product, { includePictures = false }
query: "",
searchUrl: lastSearchUrl,
status: "error",
error: "EAN is required for Idealo Firefox search.",
error: "EAN is required for Idealo Playwright search.",
candidates: [],
};
}
try {
const result = await searchInFirefox({
const result = await searchInBrowser({
url: attempt.url,
headless: source.browserHeadless,
waitAfterLoadMs: source.waitAfterLoadMs,
@@ -283,14 +284,14 @@ async function findSourceCandidates(source, product, { includePictures = false }
query: attempt.query,
searchUrl: result.url || attempt.url,
status: "error",
error: "Idealo returned an error page in Firefox.",
error: "Idealo returned an error page in Playwright.",
candidates: buildManualSearchCandidates(searchAttempts, product),
};
} catch (error) {
try {
openInFirefox(attempt.url);
await openInBrowser(attempt.url);
} catch {
// The manual candidate below remains available even if Firefox cannot start.
// The manual candidate below remains available even if the browser cannot start.
}
return {
@@ -298,7 +299,7 @@ async function findSourceCandidates(source, product, { includePictures = false }
query: attempt.query,
searchUrl: attempt.url,
status: "manual-search",
error: `Firefox automation unavailable: ${error.message || "search failed"}`,
error: `Playwright automation unavailable: ${error.message || "search failed"}`,
candidates: buildManualSearchCandidates(searchAttempts, product),
};
}
@@ -307,19 +308,12 @@ async function findSourceCandidates(source, product, { includePictures = false }
for (const attempt of searchAttempts) {
lastSearchUrl = attempt.url;
try {
const response = await fetch(attempt.url, {
method: attempt.method || "GET",
redirect: "follow",
headers: {
"User-Agent": "CatalogMaker/0.1",
...(attempt.body ? { "Content-Type": "application/x-www-form-urlencoded" } : {}),
},
body: attempt.body,
signal: AbortSignal.timeout(9000),
const response = await scrapePage(attempt.url, {
waitAfterLoadMs: source.waitAfterLoadMs,
});
const html = await response.text();
const html = response.pageSource;
const candidates = [
...extractResponseUrlCandidate(response.url || attempt.url, html, product),
...extractResponseUrlCandidate(response.url || attempt.url, html, product),
...extractDirectCandidates(html, source, product),
].slice(0, 3);
@@ -604,12 +598,8 @@ async function verifyHudyVariant(url, product) {
if (visited.has(page.url)) continue;
visited.add(page.url);
const response = await fetch(page.url, {
headers: { "User-Agent": "CatalogMaker/0.1" },
redirect: "follow",
signal: AbortSignal.timeout(9000),
});
const html = await response.text();
const response = await scrapePage(page.url, { waitAfterLoadMs: 750 });
const html = response.pageSource;
lastStatus = response.status;
const pageEans = extractPageEans(html);
const matchedEan = pageEans.find((value) => wantedEans.has(value)) || "";
@@ -712,12 +702,8 @@ function extractPictureUrls(html, pageUrl) {
async function extractPicturesFromPage(url) {
try {
const response = await fetch(url, {
headers: { "User-Agent": "CatalogMaker/0.1" },
redirect: "follow",
signal: AbortSignal.timeout(9000),
});
return extractHudyGalleryPictureUrls(await response.text(), response.url || url);
const response = await scrapePage(url, { waitAfterLoadMs: 750 });
return extractHudyGalleryPictureUrls(response.pageSource, response.url || url);
} catch {
return [];
}
@@ -878,22 +864,7 @@ async function checkMappingSourceUrls(items) {
async function checkUrl(url) {
try {
const target = normalizeSourceUrl(url);
let response = await fetch(target, {
method: "HEAD",
redirect: "follow",
signal: AbortSignal.timeout(6000),
});
if (!response.ok) {
response = await fetch(target, {
method: "GET",
redirect: "follow",
headers: {
"User-Agent": "CatalogMaker/0.1",
},
signal: AbortSignal.timeout(6000),
});
}
const response = await scrapePage(target, { waitAfterLoadMs: 250 });
return {
health: response.ok ? "ok" : "warning",
-82
View File
@@ -1,82 +0,0 @@
import { spawn } from "node:child_process";
import { Builder } from "selenium-webdriver";
import firefox from "selenium-webdriver/firefox.js";
import { start } from "geckodriver";
const FIREFOX_PATHS = [
"C:\\Program Files\\Mozilla Firefox\\firefox.exe",
"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
];
export function openInFirefox(url) {
const firefoxPath = FIREFOX_PATHS.find((candidate) => candidate);
if (!firefoxPath) throw new Error("Firefox executable was not found.");
const process = spawn(firefoxPath, [url], {
detached: true,
stdio: "ignore",
windowsHide: true,
});
process.unref();
}
export async function searchInFirefox({ url, headless = false, waitAfterLoadMs = 3000 }) {
const firefoxPath = FIREFOX_PATHS.find((candidate) => candidate);
const port = 4444;
const geckodriver = await start({ port, binary: firefoxPath, log: "error" });
let driver;
try {
await waitForWebDriver(port);
const options = new firefox.Options();
if (firefoxPath) options.setBinary(firefoxPath);
if (headless) options.addArguments("-headless");
driver = await new Builder()
.forBrowser("firefox")
.setFirefoxOptions(options)
.usingServer(`http://127.0.0.1:${port}`)
.build();
await driver.get(url);
await driver.sleep(Math.max(1000, Number(waitAfterLoadMs) || 3000));
const [currentUrl, title, pageSource] = await Promise.all([
driver.getCurrentUrl(),
driver.getTitle(),
driver.getPageSource(),
]);
const cleanText = stripHtml(pageSource).slice(0, 1200);
const failed = /something has gone wrong|reference id/i.test(cleanText);
return { ok: !failed, url: currentUrl, title, text: cleanText, pageSource };
} finally {
if (driver) await driver.quit().catch(() => {});
geckodriver.kill();
}
}
async function waitForWebDriver(port) {
const deadline = Date.now() + 10000;
while (Date.now() < deadline) {
try {
const response = await fetch(`http://127.0.0.1:${port}/status`);
if (response.ok) return;
} catch {
// Geckodriver needs a short moment to open its local port.
}
await new Promise((resolve) => setTimeout(resolve, 150));
}
throw new Error("Firefox WebDriver did not start.");
}
function stripHtml(value) {
return String(value ?? "")
.replace(/<script[\s\S]*?<\/script>/gi, " ")
.replace(/<style[\s\S]*?<\/style>/gi, " ")
.replace(/<[^>]+>/g, " ")
.replace(/&nbsp;/gi, " ")
.replace(/&amp;/gi, "&")
.replace(/\s+/g, " ")
.trim();
}
@@ -1,5 +1,6 @@
import { queryEndpoint } from "../endpoint/endpoint-client.js";
import { catalogMakerSql } from "../sql/catalog-maker.js";
// @ts-nocheck
import { queryEndpoint } from "../endpoint/endpoint-client.ts";
import { catalogMakerSql } from "../sql/catalog-maker.ts";
export async function listLanguages(config) {
const result = await queryEndpoint(config, catalogMakerSql.languages);
@@ -1,5 +1,6 @@
import { queryEndpoint } from "../endpoint/endpoint-client.js";
import { catalogMakerSql } from "../sql/catalog-maker.js";
// @ts-nocheck
import { queryEndpoint } from "../endpoint/endpoint-client.ts";
import { catalogMakerSql } from "../sql/catalog-maker.ts";
export async function listManufacturers(config) {
const result = await queryEndpoint(config, catalogMakerSql.manufacturers);