diff --git a/packages/metascraper-instagram/src/index.js b/packages/metascraper-instagram/src/index.js index ad5be32f2..e91f35414 100644 --- a/packages/metascraper-instagram/src/index.js +++ b/packages/metascraper-instagram/src/index.js @@ -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: $ }) => diff --git a/packages/metascraper-instagram/test/index.js b/packages/metascraper-instagram/test/index.js index 39b9da538..e37b5312d 100644 --- a/packages/metascraper-instagram/test/index.js +++ b/packages/metascraper-instagram/test/index.js @@ -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(