jshemas / openGraphScraper

Node.js scraper service for Open Graph Info and More!
MIT License
669 stars 105 forks source link

Something is not right with the types #171

Closed standuprey closed 1 year ago

standuprey commented 1 year ago

Here is my code:

async function openGraphScrapper(url: string) {
  const { result: data } = await ogs({ url })
  console.log(data)
  return {
    imageUrl: data.ogImageURL,
    title: data.ogTitle,
    url: data.ogUrl,
  }
}

When I try with https://seth.blog the console.log returns this

{
  ogType: 'website',
  ogTitle: "Seth's Blog",
  ogDescription: "Seth Godin's Blog on marketing, tribes and respect",
  ogUrl: 'https://seths.blog/',
  ogSiteName: "Seth's Blog",
  ogLocale: 'en_US',
  twitterCard: 'summary_large_image',
  twitterSite: '@ThisIsSethsBlog',
  twitterCreator: '@ThisIsSethsBlog',
  ogImage: {
    height: '628px',
    type: 'jpg',
    url: 'https://149521506.v2.pressablecdn.com/wp-content/themes/godin/img/og-images/seth.jpg',
    width: '1200px'
  },
  favicon: 'https://149521506.v2.pressablecdn.com/wp-content/themes/godin/img/favicons/favicon.ico',
  charset: 'utf8',
  requestUrl: 'https://seth.blog',
  success: true
}

So it seems to me that if I want the image, I need to get it from data.ogImage.url, but that is not part of the return type. Is it missing?

jshemas commented 1 year ago

Hello, I can look into it.

jshemas commented 1 year ago

So I think the issue is because ogImage can be a string, object and array. I'm not sure how to support all three of these datatypes. In the next version of openGraphScraper I'm planning on update all properties to just be one datatype so we don't have issues like this in the future.

jshemas commented 1 year ago

Hello. open-graph-scraper@6.0.0 is now live and now ogImage will always be the ImageObject[] type which will have a url property. Let me know if this fixes your problem.

jshemas commented 1 year ago

Closing this for now. Please reopen the issue if needed.