merchnow-api-node
Version:
npm module for Merchnow's API
64 lines (63 loc) • 2.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.catalogToObject = void 0;
var node_html_parser_1 = require("node-html-parser");
var getDecorations = function (decorations) {
var contents = [];
if (decorations.length > 0) {
for (var _i = 0, decorations_1 = decorations; _i < decorations_1.length; _i++) {
var decoration = decorations_1[_i];
contents.push(decoration.text);
}
}
return contents;
};
var getDescriptions = function (descriptions) {
var contents = [];
if (descriptions.length > 0) {
for (var _i = 0, descriptions_1 = descriptions; _i < descriptions_1.length; _i++) {
var description = descriptions_1[_i];
for (var _a = 0, _b = description.childNodes; _a < _b.length; _a++) {
var child = _b[_a];
var content = child.text.trim();
if (content.length > 0) {
var tag = child.tagName || "none";
contents.push({ content: content, tag: tag });
}
}
}
}
return contents;
};
var getImages = function (image) {
var url = image.getAttribute("src");
var sm = url.replace("imageproductmd", "imageproductsm");
var md = url;
var lg = url.replace("imageproductmd", "imageproductlg");
var xl = url.replace("imageproductmd", "imageproductxl");
return { sm: sm, md: md, lg: lg, xl: xl };
};
exports.catalogToObject = function (html) {
var parsed = node_html_parser_1.parse(html);
var items = parsed.querySelectorAll(".product-list-item");
var result = [];
for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
var item = items_1[_i];
var image = item.querySelector("img"); // every item has one image, from which we can get all sizes
var name_1 = image.getAttribute("alt"); // every image has a alt attribute
var decorations = getDecorations(item.querySelectorAll(".image-decoration"));
var description = getDescriptions(item.querySelectorAll(".product-list-desc"));
var images = getImages(image);
var href = item.querySelector("a").getAttribute("href");
var url = "https://merchnow.com" + href;
var obj = {
name: name_1,
decorations: decorations,
description: description,
images: images,
url: url
};
result.push(obj);
}
return result;
};