metascraper-x 5.49.9 → 5.49.10

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 +33 -12
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.9",
5
+ "version": "5.49.10",
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": "47690490a2eb1f6e3bcd47559fefeaa298c45eaa"
42
+ "gitHead": "49b73e47537a43054208b9c6d80b906fa3890395"
43
43
  }
package/src/index.js CHANGED
@@ -25,30 +25,51 @@ const test = memoizeOne(url =>
25
25
  ['twitter.com', 'x.com'].includes(parseUrl(url).domain)
26
26
  )
27
27
 
28
+ const getAuthorName = memoizeOne(ogTitle =>
29
+ ogTitle?.split(' on X:')[0].split(' (@')[0].trim()
30
+ )
31
+
28
32
  module.exports = ({ resolveUrl = url => url } = {}) => {
29
33
  const rules = {
30
34
  author: [
31
- toAuthor($ => {
32
- const author = $('meta[property="og:title"]').attr('content')
33
- return author?.includes(' on X') ? author.split(' on X')[0] : author
34
- })
35
- ],
36
- title: [
37
- toTitle(
38
- ($, url) => `@${new URL(url).pathname.toString().split('/')[1]} on X`
35
+ toAuthor($ =>
36
+ getAuthorName($('meta[property="og:title"]').attr('content'))
39
37
  )
40
38
  ],
41
- url: [
42
- toUrl($ =>
43
- $('link[rel="canonical"]').attr('href')?.replace('twitter.com', 'x.com')
44
- )
39
+ title: [
40
+ toTitle(($, url) => {
41
+ const username = new URL(url).pathname.split('/')[1]
42
+ const authorName = getAuthorName(
43
+ $('meta[property="og:title"]').attr('content')
44
+ )
45
+ return authorName
46
+ ? `${authorName} (@${username}) on X`
47
+ : `@${username} on X`
48
+ })
45
49
  ],
50
+ url: [toUrl($ => $('link[rel="canonical"]').attr('href'))],
46
51
  description: [
47
52
  toDescription(async $ => {
48
53
  let description =
49
54
  $jsonld('mainEntity.description')($) ||
50
55
  $('meta[property="og:description"]').attr('content')
51
56
 
57
+ if (!description) {
58
+ const ogTitle = $('meta[property="og:title"]').attr('content')
59
+ if (ogTitle?.includes(' on X: "')) {
60
+ description = ogTitle.split(' on X: "')[1].split('" / X')[0]
61
+ const urls = getUrls(description)
62
+ if (urls.length > 1) {
63
+ const lastUrl = urls[urls.length - 1]
64
+ if (description.endsWith(lastUrl)) {
65
+ description = description.replace(lastUrl, '').trim()
66
+ }
67
+ }
68
+ }
69
+ }
70
+
71
+ if (!description) return
72
+
52
73
  const urls = getUrls(description)
53
74
  const resolvedUrls = await Promise.all(urls.map(resolveUrl))
54
75