You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.3 KiB
JavaScript
87 lines
2.3 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("assert");
|
|
const cheerio = require("cheerio");
|
|
const syncpipe = require("syncpipe");
|
|
const url = require("url");
|
|
|
|
const assureResponse = require("../../shared/assure-response");
|
|
|
|
function firstMatch(options) {
|
|
for (let option of options) {
|
|
if (option != null && option !== "") {
|
|
return option;
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = function tmeScrapeProduct({ session }) {
|
|
return async function ({ data, createAlias, updateData, expireDependents }) {
|
|
let response = await session.get(data.url);
|
|
assureResponse(response);
|
|
|
|
let $ = cheerio.load(response.body);
|
|
|
|
// FIXME: This is currently broken!
|
|
let allMetaHeaders = syncpipe($("h2.o-semantic-only-header").toArray(), [
|
|
(_) => _.map((header) => {
|
|
let $header = $(header);
|
|
|
|
return [
|
|
$header.find(".name").text().trim(),
|
|
$header.find(".value").text().trim()
|
|
];
|
|
}),
|
|
(_) => Object.fromEntries(_)
|
|
]);
|
|
|
|
let descriptionElement = $(".c-pip__description > h2").eq(0);
|
|
|
|
let itemData = {
|
|
productID: $("h2.c-pip__symbol--tme .c-pip__symbol-value").eq(0).text().trim(),
|
|
manufacturer: $("a.pip__product-header-title").eq(0).text().trim(),
|
|
model: firstMatch([
|
|
$("h2.c-pip__symbol--producer .c-pip__symbol-value").eq(0).text().trim(), // Manufacturer part number
|
|
$("h2.c-pip__symbol--tme .c-pip__symbol-value").eq(0).text().trim(), // TME Symbol
|
|
]),
|
|
description: (descriptionElement.children().length === 0) // This skips meta fields if there is no description element
|
|
? descriptionElement.text().trim()
|
|
: null,
|
|
documents: $("div.c-pip__document > a").toArray()
|
|
.map((link) => {
|
|
let relativeLink = $(link).attr("href");
|
|
|
|
if (relativeLink != null) {
|
|
return {
|
|
description: $(link).text().trim(),
|
|
url: url.resolve(
|
|
data.url,
|
|
relativeLink
|
|
)
|
|
};
|
|
} else {
|
|
// Probably a video popup
|
|
return null;
|
|
}
|
|
})
|
|
.filter((item) => {
|
|
return item != null;
|
|
}),
|
|
// TODO: Scrape prices
|
|
};
|
|
|
|
assert(itemData.productID != null);
|
|
assert(itemData.manufacturer != null);
|
|
assert(itemData.model != null);
|
|
|
|
createAlias({ from: `tme:product:${itemData.productID}` });
|
|
|
|
updateData((oldData) => ({
|
|
... oldData,
|
|
itemData: itemData
|
|
}));
|
|
|
|
expireDependents();
|
|
};
|
|
};
|