Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instagram): handle empty cases #740

Merged
merged 1 commit into from
Jan 12, 2025
Merged
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
8 changes: 4 additions & 4 deletions packages/metascraper-instagram/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ module.exports = () => {
const rules = {
author: ({ htmlDom: $ }) => {
const title = $('meta[property="og:title"]').attr('content')
const value = title.split(' on Instagram')[0]
const value = title?.split(' on Instagram')[0]
return author(value)
},
date: ({ htmlDom: $, url }) => {
const description = getDescription(url, $)
const dateMatch = description.match(/on ([^,]+, \d{4})/)
if (dateMatch === null) return
const dateMatch = description?.match(/on ([^,]+, \d{4})/)
if (dateMatch === null || dateMatch === undefined) return
const dateString = `${dateMatch[1]} GMT`
return date(new Date(dateString))
},
lang: ({ htmlDom: $, url }) => {
const description = getDescription(url, $)
const input = description.split(': ').pop().split(' - ').pop()
const input = description?.split(': ').pop()?.split(' - ').pop()
return detectLang(input)
},
title: ({ htmlDom: $ }) =>
Expand Down
7 changes: 7 additions & 0 deletions packages/metascraper-instagram/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const metascraper = require('metascraper')([
require('metascraper-url')()
])

test('code is resilient', async t => {
const url = 'https://www.instagram.com/p/CPeC-Eenc8l/'
const html = ''
const metadata = await metascraper({ url, html })
t.snapshot(metadata)
})

test('from photo post', async t => {
const url = 'https://www.instagram.com/p/CPeC-Eenc8l/'
const html = await readFile(
Expand Down
Loading