pburrows / unfurl.net

A .net metadata scraper that supports X (nee Twitter) Cards, Open Graph, and oEmbed
MIT License
2 stars 0 forks source link

oEmbed Support #1

Closed epicstar closed 1 month ago

epicstar commented 1 month ago

Hello, first of all, thank you for the library.

I was wondering if it was possible to add oEmbed support like unfurl.js? We are actually coming across some URLs that have oEmbed but not opengraph or twitter cards.

For example: https://forms.office.com/r/YLPA60FDtJ

<link title="Microsoft Forms" rel="alternate" href="https://forms.office.com/formapi/api/embed?url=https%3a%2f%2fforms.office.com%2fpages%2fresponsepage.aspx%3fid%3dWKegD09AT0e-zu0p2qxyRJq-C-v7J5RIq7JxzkXdCFRUQzVUOVBCUlNaU1dBRzFNVDdOR0JONENMVi4u%26route%3dshorturl%26mobile%3dfalse%26embed%3dtrue" type="application/json+oembed">

The oEmbed link returns:

{
  "type": "rich",
  "version": "1.0",
  "title": "",
  "author_name": "",
  "provider_name": "Microsoft Forms",
  "provider_url": "forms.office.com",
  "thumbnail_url": "https://forms.office.com/Images/icon-form-logo.png",
  "html": "\u003Ciframe width=\"854px\" height= \"800px\" src=\"https://forms.office.com/pages/responsepage.aspx?id=WKegD09AT0e-zu0p2qxyRJq-C-v7J5RIq7JxzkXdCFRUQzVUOVBCUlNaU1dBRzFNVDdOR0JONENMVi4u&route=shorturl&mobile=false&embed=true\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" style= \"border: none; max-width:100%; max-height:100vh\"allowfullscreen webkitallowfullscreen mozallowfullscreen msallowfullscreen\u003E \u003C/iframe\u003E",
  "width": "854",
  "height": "800"
}

Thank you!

epicstar commented 1 month ago

It is possible I may have time to port the ombed code from unfurl.js to this library.

If I have the time to do this, would you be ok with me creating a PR for this? Thank you!

pburrows commented 1 month ago

Happy to accept a PR for this. Otherwise, I will try and look at it in the next couple days.

pburrows commented 1 month ago

oEmbed support added. Published to NuGet as version 1.1.

epicstar commented 1 month ago

Hello @pburrows, I'm testing the newest version of the library this with this code snippet in my repo at the moment:

var unfurledUrl = await _unfurler.Unfurl("https://forms.office.com/r/YLPA60FDtJ", new UnfurlOptions { LoadOEmbed = true, OEmbedHttpClient = _httpClient }); // my httpClient is injected in DI... it's being used correctly

my unfurledUrl.OEmbed and unfurledUrl.OEmbedLink are null. Is this expected? This seems like a bug to me.

Do note I tried youtube links with the oEmbed option turned on and they work as expected. Thank you!

epicstar commented 1 month ago

Crisis averted. I looked at your unit tests and saw you used my link as one of the test cases. Once I added MaximumRedirects = 2 to the UnfurlOptions, it started working as expected.

MaximumRedirects isn't a required option in https://github.com/jacktuck/unfurl, but I'm good with using this argument. Thank you!

pburrows commented 1 month ago

Yeah, the default number of redirects is 0.