metascraper-x 5.49.7 → 5.49.9

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.
package/README.md CHANGED
@@ -20,17 +20,11 @@ $ npm install metascraper-x --save
20
20
 
21
21
  #### options
22
22
 
23
- ##### resolveUrls
24
-
25
- Type: `boolean`
26
-
27
- Set to `true` if you want to resolve `t.co` URLs into the final URL.
28
-
29
23
  ##### resolveUrl
30
24
 
31
25
  Type: `function`
32
26
 
33
- The function implementation that resolves `t.co` into a the final URL.
27
+ The function implementation that resolves URLs (e.g., `t.co`) into the final URL.
34
28
 
35
29
  ## License
36
30
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper-x",
3
3
  "description": "Metascraper integration with x.com",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-x",
5
- "version": "5.49.7",
5
+ "version": "5.49.9",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "author": {
@@ -39,5 +39,5 @@
39
39
  "test": "NODE_PATH=.. TZ=UTC ava --timeout 15s"
40
40
  },
41
41
  "license": "MIT",
42
- "gitHead": "4920125394e77b42fcc0636e03fb066b7e1f81ed"
42
+ "gitHead": "47690490a2eb1f6e3bcd47559fefeaa298c45eaa"
43
43
  }
package/src/index.js CHANGED
@@ -1,28 +1,31 @@
1
1
  'use strict'
2
2
 
3
3
  const {
4
- getUrls,
4
+ $jsonld,
5
5
  author,
6
+ date,
7
+ description,
8
+ getUrls,
6
9
  image,
7
10
  memoizeOne,
8
11
  parseUrl,
9
12
  title,
10
13
  toRule,
11
- description,
12
14
  url
13
15
  } = require('@metascraper/helpers')
14
16
 
17
+ const toDescription = toRule(description)
15
18
  const toAuthor = toRule(author)
16
19
  const toImage = toRule(image)
17
20
  const toTitle = toRule(title)
18
- const toDescription = toRule(description)
21
+ const toDate = toRule(date)
19
22
  const toUrl = toRule(url)
20
23
 
21
24
  const test = memoizeOne(url =>
22
25
  ['twitter.com', 'x.com'].includes(parseUrl(url).domain)
23
26
  )
24
27
 
25
- module.exports = ({ resolveUrls = false, resolveUrl = url => url } = {}) => {
28
+ module.exports = ({ resolveUrl = url => url } = {}) => {
26
29
  const rules = {
27
30
  author: [
28
31
  toAuthor($ => {
@@ -42,8 +45,10 @@ module.exports = ({ resolveUrls = false, resolveUrl = url => url } = {}) => {
42
45
  ],
43
46
  description: [
44
47
  toDescription(async $ => {
45
- let description = $('meta[property="og:description"]').attr('content')
46
- if (!resolveUrls) return description
48
+ let description =
49
+ $jsonld('mainEntity.description')($) ||
50
+ $('meta[property="og:description"]').attr('content')
51
+
47
52
  const urls = getUrls(description)
48
53
  const resolvedUrls = await Promise.all(urls.map(resolveUrl))
49
54
 
@@ -57,13 +62,43 @@ module.exports = ({ resolveUrls = false, resolveUrl = url => url } = {}) => {
57
62
  ],
58
63
  image: [
59
64
  toImage($ => {
60
- let imageUrl = $('meta[property="og:image"]').attr('content')
65
+ let imageUrl =
66
+ $jsonld('mainEntity.image.contentUrl')($) ||
67
+ $('meta[property="og:image"]').attr('content')
68
+
61
69
  if (imageUrl?.endsWith('_200x200.jpg')) {
62
70
  imageUrl = imageUrl.replace('_200x200.jpg', '_400x400.jpg')
63
71
  }
64
72
  return imageUrl
65
73
  })
66
74
  ],
75
+ date: [
76
+ toDate(($, url) => {
77
+ const { pathname } = new URL(url)
78
+ const parts = pathname.split('/')
79
+ const username = parts[1].toLowerCase()
80
+ const isStatus = parts[2] === 'status'
81
+
82
+ if (isStatus) {
83
+ const statusId = parts[3]
84
+ return (
85
+ $(`a[href*="/status/${statusId}"] time`).attr('datetime') ||
86
+ $('article time').attr('datetime') ||
87
+ $('time').attr('datetime')
88
+ )
89
+ }
90
+
91
+ return $(`a[href*="/${username}/status/" i] time`)
92
+ .get()
93
+ .reduce((acc, el) => {
94
+ const datetime = $(el).attr('datetime')
95
+ if (datetime && (!acc || datetime > acc)) {
96
+ acc = datetime
97
+ }
98
+ return acc
99
+ }, undefined)
100
+ })
101
+ ],
67
102
  publisher: () => 'X'
68
103
  }
69
104