metascraper-x 5.49.5 → 5.49.8

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 +3 -3
  2. package/src/index.js +41 -5
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.5",
5
+ "version": "5.49.8",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "author": {
@@ -24,7 +24,7 @@
24
24
  "spotify"
25
25
  ],
26
26
  "dependencies": {
27
- "@metascraper/helpers": "5.49.5"
27
+ "@metascraper/helpers": "5.49.7"
28
28
  },
29
29
  "devDependencies": {
30
30
  "ava": "5"
@@ -39,5 +39,5 @@
39
39
  "test": "NODE_PATH=.. TZ=UTC ava --timeout 15s"
40
40
  },
41
41
  "license": "MIT",
42
- "gitHead": "260a219e626c1ed61fdbd5ebf13db4a21d1fefb2"
42
+ "gitHead": "327e130cb3825bf1e05220f19d3ffecb2e7178ed"
43
43
  }
package/src/index.js CHANGED
@@ -1,21 +1,24 @@
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 =>
@@ -42,7 +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')
48
+ let description =
49
+ $jsonld('mainEntity.description')($) ||
50
+ $('meta[property="og:description"]').attr('content')
51
+
46
52
  if (!resolveUrls) return description
47
53
  const urls = getUrls(description)
48
54
  const resolvedUrls = await Promise.all(urls.map(resolveUrl))
@@ -57,13 +63,43 @@ module.exports = ({ resolveUrls = false, resolveUrl = url => url } = {}) => {
57
63
  ],
58
64
  image: [
59
65
  toImage($ => {
60
- let imageUrl = $('meta[property="og:image"]').attr('content')
66
+ let imageUrl =
67
+ $jsonld('mainEntity.image.contentUrl')($) ||
68
+ $('meta[property="og:image"]').attr('content')
69
+
61
70
  if (imageUrl?.endsWith('_200x200.jpg')) {
62
71
  imageUrl = imageUrl.replace('_200x200.jpg', '_400x400.jpg')
63
72
  }
64
73
  return imageUrl
65
74
  })
66
75
  ],
76
+ date: [
77
+ toDate(($, url) => {
78
+ const { pathname } = new URL(url)
79
+ const parts = pathname.split('/')
80
+ const username = parts[1].toLowerCase()
81
+ const isStatus = parts[2] === 'status'
82
+
83
+ if (isStatus) {
84
+ const statusId = parts[3]
85
+ return (
86
+ $(`a[href*="/status/${statusId}"] time`).attr('datetime') ||
87
+ $('article time').attr('datetime') ||
88
+ $('time').attr('datetime')
89
+ )
90
+ }
91
+
92
+ return $(`a[href*="/${username}/status/" i] time`)
93
+ .get()
94
+ .reduce((acc, el) => {
95
+ const datetime = $(el).attr('datetime')
96
+ if (datetime && (!acc || datetime > acc)) {
97
+ acc = datetime
98
+ }
99
+ return acc
100
+ }, undefined)
101
+ })
102
+ ],
67
103
  publisher: () => 'X'
68
104
  }
69
105