shopify-product-scraper
Version:
A lightweight JavaScript library for scraping products from Shopify stores. This package provides a simple and efficient way to extract product data from Shopify websites.
95 lines (91 loc) • 2.82 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/index.ts
var index_exports = {};
__export(index_exports, {
scrape: () => scrape
});
module.exports = __toCommonJS(index_exports);
// src/url.ts
var shopifyIndicators = [
/shopify\.com/i,
/cdn\.shopify\.com/i,
/Shopify\.Buy\.SDK/i,
/var Shopify =/i,
/{{ '.*' | asset_url }}/i
];
function isValidURL(url) {
return __async(this, null, function* () {
try {
const html = yield fetch(url).then((res) => res.text());
return shopifyIndicators.some((pattern) => pattern.test(html));
} catch (e) {
return false;
}
});
}
// src/index.ts
function scrape(_0) {
return __async(this, arguments, function* (url, { limit = Infinity, onProgress } = {}) {
var _a;
const isValid = yield isValidURL(url);
if (!isValid) return null;
const data = [];
let page = 0;
while (data.length < limit) {
const pageLimit = Math.min(250, limit - data.length);
const requestedData = yield fetch(
`${url}/products.json/?limit=${pageLimit}&page=${page}`
).then((res) => res.json());
if (!((_a = requestedData == null ? void 0 : requestedData.products) == null ? void 0 : _a.length)) break;
data.push(...requestedData.products);
if (onProgress)
onProgress({
progress: data.length / limit,
products: data
});
page++;
}
return data;
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
scrape
});