metascraper-instagram 5.58.1 → 5.58.2

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 +23 -0
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper-instagram",
3
3
  "description": "Metascraper rules tailored for Instagram pages — richer metadata than generic HTML parsers.",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-instagram",
5
- "version": "5.58.1",
5
+ "version": "5.58.2",
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": "ba231caba2d7afe37e55921e88a77b02dafcdcf4"
49
+ "gitHead": "3914e46522974b0c551d33f964f0e5840c99b288"
50
50
  }
package/src/index.js CHANGED
@@ -4,6 +4,8 @@ const {
4
4
  $meta,
5
5
  author,
6
6
  date,
7
+ getHtml,
8
+ image,
7
9
  logo,
8
10
  memoizeOne,
9
11
  parseUrl,
@@ -11,8 +13,22 @@ const {
11
13
  toRule
12
14
  } = require('@metascraper/helpers')
13
15
 
16
+ const toImage = toRule(image)
14
17
  const toLogo = toRule(logo)
15
18
 
19
+ const POST_PATH = /\/(?:p|reels?|tv|stories)\//
20
+
21
+ /** First `image_versions2` URL — Instagram lists the largest candidate first. */
22
+ const firstImageVersion = html => {
23
+ const raw = html.match(
24
+ /"image_versions2":\{"candidates":\[\{[^[]*?"url":"([^"]+)"/
25
+ )?.[1]
26
+ if (!raw) return
27
+ try {
28
+ return JSON.parse(`"${raw}"`)
29
+ } catch {}
30
+ }
31
+
16
32
  const test = memoizeOne(
17
33
  url => parseUrl(url).domainWithoutSuffix === 'instagram'
18
34
  )
@@ -37,6 +53,13 @@ module.exports = () => {
37
53
  return date(new Date(dateString))
38
54
  },
39
55
  title: ({ htmlDom: $ }) => title($meta('twitter:title')($)),
56
+ // og:image is a signed 640 crop. Posts embed a larger feed candidate.
57
+ image: [
58
+ toImage(($, url) => {
59
+ if (!POST_PATH.test(new URL(url).pathname)) return
60
+ return firstImageVersion(getHtml($))
61
+ })
62
+ ],
40
63
  // rel=icon claims 192x192 but the file is 32x32. The 180 apple-touch is real.
41
64
  logo: [
42
65
  toLogo($ =>