-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
238 lines (222 loc) · 7.88 KB
/
index.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"use strict";
const { $jsonld, memoizeOne } = require("@metascraper/helpers");
const { toPriceFormat, getHostname } = require("./helpers");
const jsonLd = memoizeOne(($) => {
const jsonld = JSON.parse($('script[type="application/ld+json"]').html());
return jsonld;
});
const jsonLdGraph = memoizeOne(($) => {
const jsonld = JSON.parse($('script[type="application/ld+json"]').html());
return jsonld && jsonld["@graph"];
});
//@graph { @type=Product }
const jsonLdGraphProduct = memoizeOne(($) => {
const jsonld = jsonLdGraph($);
if (jsonld) {
let products = jsonld.filter((i) => {
return i["@type"] === "Product";
});
return products.length > 0 ? products[0] : null;
}
return;
});
const jsonLdLastBreadcrumb = memoizeOne(($) => {
const jsonld = jsonLdGraph($);
if (jsonld) {
let breadcrumbs = jsonld.filter((i) => {
return i["@type"] === "BreadcrumbList";
});
let breadcrumb = breadcrumbs.length > 0 && breadcrumbs[0];
let items = breadcrumb.itemListElement;
if (items && items.length > 0) {
let item = items[items.length - 1];
return item;
}
return null;
}
return null;
});
/**
* A set of rules we want to declare under the `metascraper-shopping` namespace.
*
**/
module.exports = () => {
const rules = {
brand: [
({ htmlDom: $, url }) => {
let jsonld = jsonLd($);
let brand = jsonld && jsonld.brand;
if (brand && brand.name) {
brand = brand.name;
}
return brand;
},
],
name: [
({ htmlDom: $, url }) => {
let jsonld = jsonLd($);
return jsonld && jsonld.name;
},
({ htmlDom: $, url }) => {
let jsonld = jsonLdLastBreadcrumb($);
return jsonld && jsonld.name;
},
({ htmlDom: $, url }) => {
let jsonld = jsonLdGraphProduct($);
return jsonld && jsonld.name;
},
({ htmlDom: $, url }) => $('[property="og:title"]').attr("content"),
],
url: [
({ htmlDom: $, url }) => {
// dacor.com canonical points to homepage
if (url.includes("dacor.com")) {
return url;
}
if (url.includes("bedrosian")) {
return $('link[rel="canonical"]').attr("href");
}
},
],
image: [
({ htmlDom: $, url }) => $('a[data-fancybox="images"]').attr("href"), //fireclaytile.com
({ htmlDom: $, url }) => $("div#imgTagWrapperId img").attr("src"), //amazon.com
({ htmlDom: $, url }) => {
//arizontile
let relativeImage = $(".main-image-border.js-main-image").attr(
"data-zoom-image"
);
var fullUrl = new URL(url);
if (relativeImage) {
return `${fullUrl.protocol}//${fullUrl.hostname}/${relativeImage}`;
}
},
({ htmlDom: $, url }) => {
//semihandmade shopify
let relativeImage = $("img.lazy.lazyload.img-fluid").attr("data-src");
var fullUrl = new URL(url);
if (relativeImage) {
return `${fullUrl.protocol}${relativeImage}`;
}
},
({ htmlDom: $, url }) =>
$('[property="og:image:secure_url"]').attr("content"),
({ htmlDom: $, url }) => {
let content = $('[property="og:image"]').attr("content");
if (url.includes("rh.com")) {
content = content.replace("$GAL4$", "$np-fullwidth-lg$");
}
return content;
},
({ htmlDom: $, url }) => {
let jsonld = jsonLd($);
let image = jsonld && jsonld.image;
if (image && image["@type"] === "ImageObject") {
image = image.image;
}
if (Array.isArray(image)) {
image = image[0];
}
if (image) return image;
},
({ htmlDom: $, url }) => $('meta.swiftype[name="image"]').attr("content"),
({ htmlDom: $, url }) => {
const image = $('[property="og:image"]').attr("content");
const protocol = new URL(url).protocol;
try {
new URL(image); //catch if not a valid URL and assume it's because of protocol
} catch (e) {
if (image) return `${protocol}${image}`;
}
if (image) return image;
},
],
currency: [
({ htmlDom: $, url }) => {
let jsonld = jsonLdGraphProduct($);
return jsonld && jsonld.offers && jsonld.offers.priceCurrency;
},
({ htmlDom: $, url }) =>
$('[property="og:price:currency"]').attr("content"),
({ htmlDom: $, url }) => $jsonld("offers.0.priceCurrency")($, url),
({ htmlDom: $, url }) => $jsonld("offers.priceCurrency")($, url),
({ htmlDom: $, url }) =>
$("[data-asin-currency-code]").attr("data-asin-currency-code"), //amazon
({ htmlDom: $, url }) =>
$('[property="product:price:currency"]').attr("content"),
({ htmlDom: $, url }) => $("[itemprop=priceCurrency]").attr("content"),
],
condition: [
({ htmlDom: $, url }) => $jsonld("itemCondition")($, url),
({ htmlDom: $, url }) => $jsonld("offers.itemCondition")($, url),
({ htmlDom: $, url }) => $jsonld("offers.0.itemCondition")($, url),
],
sku: [
({ htmlDom: $, url }) => {
let jsonld = jsonLdGraphProduct($);
return jsonld && jsonld.sku;
},
({ htmlDom: $, url }) => $jsonld("sku")($, url),
({ htmlDom: $, url }) => $jsonld("offers.sku")($, url),
({ htmlDom: $, url }) => $jsonld("offers.0.sku")($, url),
({ htmlDom: $, url }) => $("[itemprop=sku]").html(),
],
mpn: [
//mpn=ManufacturProductNumber
({ htmlDom: $, url }) => $jsonld("mpn")($, url),
({ htmlDom: $, url }) => $jsonld("offers.mpn")($, url),
({ htmlDom: $, url }) => $jsonld("offers.0.mpn")($, url),
],
availability: [
({ htmlDom: $, url }) => {
let jsonld = jsonLdGraphProduct($);
return jsonld && jsonld.offers && jsonld.offers.availability;
},
({ htmlDom: $, url }) =>
$('[property="og:availability"]').attr("content"),
({ htmlDom: $, url }) => $jsonld("offers.availability")($, url),
({ htmlDom: $, url }) => $jsonld("offers.0.availability")($, url),
({ htmlDom: $, url }) => $("[itemprop=availability]").attr("href"),
],
price: [
({ htmlDom: $, url }) => {
let jsonld = jsonLdGraphProduct($);
return jsonld && jsonld.offers && toPriceFormat(jsonld.offers.price);
},
({ htmlDom: $, url }) =>
toPriceFormat($('[property="og:price:amount"]').attr("content")),
({ htmlDom: $, url }) =>
toPriceFormat($("[itemprop=price]").attr("content")),
({ htmlDom: $, url }) =>
toPriceFormat($('[property="product:price:amount"]').attr("content")),
({ htmlDom: $, url }) => toPriceFormat($jsonld("price")($, url)),
({ htmlDom: $, url }) => toPriceFormat($jsonld("offers.price")($, url)),
({ htmlDom: $, url }) => toPriceFormat($jsonld("offers.0.price")($, url)),
({ htmlDom: $, url }) => toPriceFormat($jsonld("0.offers.price")($, url)),
({ htmlDom: $, url }) =>
toPriceFormat($jsonld("offers.lowPrice")($, url)),
({ htmlDom: $, url }) =>
toPriceFormat($jsonld("offers.0.lowPrice")($, url)),
({ htmlDom: $, url }) =>
toPriceFormat($jsonld("offers.highPrice")($, url)),
({ htmlDom: $, url }) =>
toPriceFormat($jsonld("offers.0.highPrice")($, url)),
({ htmlDom: $, url }) =>
toPriceFormat($("[data-asin-price]").attr("data-asin-price")), //amazon
({ htmlDom: $, url }) => toPriceFormat($("[itemprop=price]").html()),
({ htmlDom: $, url }) =>
toPriceFormat(
toPriceFormat($("#attach-base-product-price").attr("value"))
),
],
asin: [
//unique amazon identifier
({ htmlDom: $, url }) => $("[data-asin]").attr("data-asin"),
],
hostname: [({ htmlDom: $, url }) => getHostname(url)],
retailer: [
({ htmlDom: $, url }) => $('[property="og:site_name"]').attr("content"),
],
};
return rules;
};