Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions packages/metascraper-x/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ const getAuthorName = memoizeOne(ogTitle =>
ogTitle?.split(' on X:')[0].split(' (@')[0].trim()
)

const STATUS_ID_REGEX = /^\/[^/]+\/status\/(\d+)/

const NON_MEDIA_IMAGE_REGEX = /\/profile_images\/|\/rweb\/ssr\/default\//

const getPostCardUrl = url => {
const statusId = new URL(url).pathname.match(STATUS_ID_REGEX)?.[1]
return statusId && `https://jf.x.com/images/post/${statusId}.png`
}

const getShareImage = ($, url) => {
const ogImage = $meta('og:image')($)
const postCardUrl = getPostCardUrl(url)
if (!postCardUrl) return ogImage
return ogImage && !NON_MEDIA_IMAGE_REGEX.test(ogImage) ? ogImage : postCardUrl
}

module.exports = ({ resolveUrl = url => url } = {}) => {
const rules = {
author: [toAuthor($ => getAuthorName($meta('og:title')($)))],
Expand Down Expand Up @@ -82,15 +98,15 @@ module.exports = ({ resolveUrl = url => url } = {}) => {
})
],
image: [
toImage($ => {
toImage(($, url) => {
const imageUrl =
$jsonld('mainEntity.image.contentUrl')($) ||
$('video').attr('poster') ||
/* embedded image from a post */
$('[data-testid="tweetPhoto"] img').attr('src') ||
/* embebbed image from a link */
$('[data-testid="card.layoutLarge.media"] img').attr('src') ||
$meta('og:image')($)
getShareImage($, url)

return imageUrl?.endsWith('_200x200.jpg')
? imageUrl.replace('_200x200.jpg', '_400x400.jpg')
Expand Down
32 changes: 32 additions & 0 deletions packages/metascraper-x/test/fixtures/post-syndication.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<head>
<meta charset="utf-8">
<title>Kiko Beats on X</title>
<meta property="og:type" content="article">
<meta property="og:site_name" content="X">
<meta property="og:url" content="https://x.com/Kikobeats/status/2110627638049910905">
<link rel="canonical" href="https://x.com/Kikobeats/status/2110627638049910905">
<meta property="og:title" content="Kiko Beats">
<meta property="og:description" content="Añado algunos consejos:

- La empresa es tuya y tu tiempo también. Automatiza sin volverte loco. Muchas tareas de las asesorías son rudimentarias y, una vez entiendes el proceso, puedes automatizarlas.

- Delega los cimientos: constitución, alta en CIRCE, pacto de socios,">
<meta name="author" content="Kiko Beats">
<meta property="article:published_time" content="2026-10-15T07:03:37.000Z">
<meta property="og:locale" content="es">
</head>
<body>
<article>
<p>Añado algunos consejos:</p>
<p>- La empresa es tuya y tu tiempo también. Automatiza sin volverte loco. Muchas tareas de las asesorías son rudimentarias y, una vez entiendes el proceso, puedes automatizarlas.</p>
<p>- Delega los cimientos: constitución, alta en CIRCE, pacto de socios,</p>
<blockquote>
<p><strong>Emilio Nicolás</strong> (@emailnicolas)</p>
<p>Me preguntan mucho esto.</p>
<p>Aquí lo dejo: consejos tontos operativos si vas a empezar una startup en España:</p>
<p>- Creala con Circe, trámite tener la empresa registrada en 48h, amén de tu autónomo societario y otras muchas zarandajas burocráticas<br>
- Hazte Revolut Business como banco</p>
</blockquote>
</article>

</body>
10 changes: 10 additions & 0 deletions packages/metascraper-x/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,13 @@ test('from a post with a name containing "on X"', async t => {
const metadata = await metascraper({ url, html })
t.snapshot(metadata)
})

test('from a post without media uses the post card', async t => {
const url = 'https://x.com/Kikobeats/status/2110627638049910905'
const html = await readFile(
resolve(__dirname, 'fixtures/post-syndication.html')
)
const metascraper = createMetascraper()
const metadata = await metascraper({ url, html })
t.is(metadata.image, 'https://jf.x.com/images/post/2110627638049910905.png')
})
6 changes: 3 additions & 3 deletions packages/metascraper-x/test/snapshots/index.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Generated by [AVA](https://avajs.dev).
author: 'Donald J. Trump',
date: '2020-01-30T15:39:43.000Z',
description: '“Schiff blasted for not focusing on California homeless.” @foxandfriends His District is in terrible shape. He is a corrupt pol who only dreams of the Impeachment Hoax. In my opinion he is mentally deranged!',
image: 'https://abs.twimg.com/rweb/ssr/default/v2/og/image.png',
image: 'https://jf.x.com/images/post/1222907250383245320.png',
lang: 'en',
publisher: 'X',
title: 'Donald J. Trump (@realDonaldTrump) on X',
Expand All @@ -124,7 +124,7 @@ Generated by [AVA](https://avajs.dev).
author: 'Donald J. Trump',
date: null,
description: '“Schiff blasted for not focusing on California homeless.” @foxandfriends His District is in terrible shape. He is a corrupt pol who only dreams of the Impeachment Hoax. In my opinion he is mentally deranged!',
image: 'https://pbs.twimg.com/profile_images/874276197357596672/kUuht00m_400x400.jpg',
image: 'https://jf.x.com/images/post/1222907250383245320.png',
lang: 'en',
publisher: 'X',
title: 'Donald J. Trump (@realDonaldTrump) on X',
Expand Down Expand Up @@ -252,7 +252,7 @@ Generated by [AVA](https://avajs.dev).
author: 'Life on X',
date: '2020-01-30T15:39:43.000Z',
description: 'This is a tweet about life on X',
image: 'https://abs.twimg.com/rweb/ssr/default/v2/og/image.png',
image: 'https://jf.x.com/images/post/1234567890.png',
lang: 'en',
publisher: 'X',
title: 'Life on X (@elonx) on X',
Expand Down
Binary file modified packages/metascraper-x/test/snapshots/index.js.snap
Binary file not shown.
Loading