Skip to content

Commit

Permalink
Merge pull request #740 from microlinkhq/instagram
Browse files Browse the repository at this point in the history
fix(instagram): handle empty cases
  • Loading branch information
Kikobeats authored Jan 12, 2025
2 parents f9968be + 5ee96fd commit 522b1bc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
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

0 comments on commit 522b1bc

Please sign in to comment.