metascraper-amazon 5.58.13 → 5.59.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +69 -10
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper-amazon",
3
3
  "description": "Metascraper rules tailored for Amazon pages — richer metadata than generic HTML parsers.",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-amazon",
5
- "version": "5.58.13",
5
+ "version": "5.59.1",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "author": {
@@ -46,5 +46,5 @@
46
46
  "test": "NODE_PATH=.. TZ=UTC ava --timeout 15s"
47
47
  },
48
48
  "license": "MIT",
49
- "gitHead": "62841e86839aa2a9ae0dee7316c2b01982b9836d"
49
+ "gitHead": "15c5505d0af79d05d1732193b7fa45e743159c30"
50
50
  }
package/src/index.js CHANGED
@@ -3,6 +3,7 @@
3
3
  const {
4
4
  $filter,
5
5
  author,
6
+ isUrl,
6
7
  lang,
7
8
  memoizeOne,
8
9
  parseUrl,
@@ -11,13 +12,63 @@ const {
11
12
  url
12
13
  } = require('@metascraper/helpers')
13
14
 
14
- const isAmazonDomain = domain => {
15
- if (!domain) return false
16
- if (domain === 'a.co') return true
17
- return /^(?:amazon|amzn)\./.test(domain)
15
+ const AMAZON_SUFFIXES = [
16
+ 'ae',
17
+ 'ca',
18
+ 'cn',
19
+ 'co.jp',
20
+ 'co.uk',
21
+ 'co.za',
22
+ 'com',
23
+ 'com.au',
24
+ 'com.be',
25
+ 'com.br',
26
+ 'com.mx',
27
+ 'com.tr',
28
+ 'de',
29
+ 'eg',
30
+ 'es',
31
+ 'fr',
32
+ 'ie',
33
+ 'in',
34
+ 'it',
35
+ 'nl',
36
+ 'pl',
37
+ 'sa',
38
+ 'se',
39
+ 'sg'
40
+ ]
41
+
42
+ const AMZN_SUFFIXES = ['asia', 'com', 'eu', 'in', 'to']
43
+
44
+ const AMAZON_DOMAINS = new Set([
45
+ 'a.co',
46
+ ...AMAZON_SUFFIXES.map(suffix => `amazon.${suffix}`),
47
+ ...AMZN_SUFFIXES.map(suffix => `amzn.${suffix}`)
48
+ ])
49
+
50
+ const IMAGE_SELECTORS = [
51
+ '#landingImage',
52
+ '#imgBlkFront',
53
+ '#imgTagWrapperId img',
54
+ '.a-dynamic-image'
55
+ ]
56
+
57
+ const IMAGE_ATTRIBUTES = ['data-old-hires', 'src']
58
+
59
+ const lastLabels = (labels, count) => labels.slice(-count).join('.')
60
+
61
+ const isAmazonUrl = url => {
62
+ const { hostname } = parseUrl(url)
63
+ if (!hostname) return false
64
+ const labels = hostname.split('.')
65
+ return (
66
+ AMAZON_DOMAINS.has(lastLabels(labels, 2)) ||
67
+ AMAZON_DOMAINS.has(lastLabels(labels, 3))
68
+ )
18
69
  }
19
70
 
20
- const test = memoizeOne(url => isAmazonDomain(parseUrl(url).domain))
71
+ const test = memoizeOne(isAmazonUrl)
21
72
 
22
73
  const SUFFIX_LANGUAGES = {
23
74
  ca: 'en',
@@ -35,11 +86,22 @@ const SUFFIX_LANGUAGES = {
35
86
 
36
87
  const getDomainLanguage = url => SUFFIX_LANGUAGES[parseUrl(url).publicSuffix]
37
88
 
38
- const toUrl = toRule(url)
39
89
  const toAuthor = toRule(author)
40
90
  const toTitle = toRule(title, { removeSeparator: false })
41
91
  const toLang = toRule(lang)
42
92
 
93
+ const linkedUrl = (value, opts) => {
94
+ const absolute = url(value, opts)
95
+ return isUrl(absolute) ? absolute : undefined
96
+ }
97
+
98
+ const toLinkedUrl = toRule(linkedUrl)
99
+
100
+ const imageRules = () =>
101
+ IMAGE_SELECTORS.flatMap(selector =>
102
+ IMAGE_ATTRIBUTES.map(name => toLinkedUrl($ => $(selector).attr(name)))
103
+ )
104
+
43
105
  module.exports = () => {
44
106
  const rules = {
45
107
  lang: [toLang(($, url) => getDomainLanguage(url))],
@@ -55,10 +117,7 @@ module.exports = () => {
55
117
  toTitle($ => $filter($, $('#item_name')))
56
118
  ],
57
119
  publisher: () => 'Amazon',
58
- image: [
59
- toUrl($ => $('.a-dynamic-image').attr('data-old-hires')),
60
- toUrl($ => $('.a-dynamic-image').attr('src'))
61
- ]
120
+ image: imageRules()
62
121
  }
63
122
 
64
123
  rules.test = ({ url }) => test(url)