OpenGraph-Net 3.2.1

A simple .net assembly to use to parse Open Graph information from either a URL or an HTML snippet.   You can read more about the Open Graph protocol @ http://ogp.me/.

Install-Package OpenGraph-Net -Version 3.2.1
dotnet add package OpenGraph-Net --version 3.2.1
<PackageReference Include="OpenGraph-Net" Version="3.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpenGraph-Net --version 3.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

OpenGraphNet

AppVeyor
Nuget V
Nuget dl
License
gitter

A simple .net assembly to use to parse Open Graph information from either a URL or an HTML snippet. You can read more about the
Open Graph protocol @ http://ogp.me.

Usage

These are the basic operations of the OpenGraphNet parser.

Parsing from a URL

Synchronosly parse a url:

OpenGraph graph = OpenGraph.ParseUrl("https://open.spotify.com/user/er811nzvdw2cy2qgkrlei9sqe/playlist/2lzTTRqhYS6AkHPIvdX9u3?si=KcZxfwiIR7OBPCzj20utaQ");

Use async/await to parse a url:

OpenGraph graph = await OpenGraph.ParseUrlAsync("https://open.spotify.com/user/er811nzvdw2cy2qgkrlei9sqe/playlist/2lzTTRqhYS6AkHPIvdX9u3?si=KcZxfwiIR7OBPCzj20utaQ");

Acessing Values

Accessing Metadata

Each metadata element is is stored as an array. Additionally, each element's properties are also stored as an array.

<meta property="og:image" content="http://example.com/img1.png">
<meta property="og:image:width" content="30">
<meta property="og:image" content="http://example.com/img2.png">
<meta property="og:image:width" content="60">
<meta property="og:locale" content="en">
<meta property="og:locale:alternate" content="en_US">
<meta property="og:locale:alternate" content="en_GB">

You would access the values from the sample HTML above as:

  • graph.Metadata[&quot;og:image&quot;].First().Value // &quot;http://example.com/img1.png&quot;.
  • graph.Metadata[&quot;og:image&quot;].First().Properties[&quot;width&quot;].Value() // &quot;30&quot;.
  • graph.Metadata[&quot;og:image&quot;][1].Value // &quot;http://example.com/img2.png&quot;.
  • graph.Metadata[&quot;og:image&quot;][1].Properties[&quot;width&quot;].Value() // &quot;30&quot;.
  • graph.Metadata[&quot;og:locale&quot;].Value() // &quot;en&quot;
  • graph.Metadata[&quot;og:locale&quot;].First().Properties[&quot;alternate&quot;][0].Value // &quot;en_US&quot;
  • graph.Metadata[&quot;og:locale&quot;].First().Properties[&quot;alternate&quot;][1].Value // &quot;en_GB&quot;

Basic Metadata

The four required Open Graph properties for all pages are available as direct properties on the OpenGraph object.

  • graph.Type is a shortcut for graph.Metadata[&quot;og:type&quot;].Value()
  • graph.Title is a shortcut for graph.Metadata[&quot;og:title&quot;].Value()
  • graph.Image is a shortcut for graph.Metadata[&quot;og:image&quot;].Value()
    Note: since there can be multiple images, this helper returns the URI of the
    first image. If you want to access images or child properties like og:image:width then you
    should instead use the graph.Metadata dictionary.
  • graph.Url is a shortcut for graph.Metadata[&quot;og:url&quot;].Value()

Creating OpenGraph Data

To create OpenGraph data in memory use the following code:

var graph = OpenGraph.MakeGraph(
    title: "My Title", 
    type: "website", 
    image: "http://example.com/img/img1.png", 
    url: "http://example.com/home", 
    description: "My Description", 
    siteName: "Example.com");
graph.AddMetadata("og", "image", "http://example.com/img/img2.png");
graph.Metadata["og:image"][0].AddProperty("width", "30");
graph.Metadata["og:image"][1].AddProperty("width", "60");
System.Console.Write(graph.ToString());

The previous System.Console.Write(graph.ToString()); will produce the following HTML (formatting added for legibility):

<meta property="og:title" content="My Title">
<meta property="og:type" content="website">
<meta property="og:image" content="http://example.com/img/img1.png">
<meta property="og:image:width" content="30">
<meta property="og:image" content="http://example.com/img/img2.png">
<meta property="og:image:width" content="60">
<meta property="og:url" content="http://example.com/home">
<meta property="og:description" content="My Description">
<meta property="og:site_name" content="Example.com">

Writing out OpenGraph Namespaces

In the wild web sites seem to add their OpenGraph namespaces in one of 2 ways. They either
write the namespaces in the html as xmlns attributes or within the head tag in the prefix attribute.

  • &lt;html xmlns:og=&quot;http://ogp.me/ns#&quot; xmlns:product=&quot;http://ogp.me/ns/product#&quot;&gt;
  • &lt;head prefix=&quot;og: http://ogp.me/ns# product: http://ogp.me/ns/product#&quot;&gt;

xmlns: version in the html tag

To create the html version in an cshtml page after creating a new graph, use the following code:

<html @graph.HtmlXmlnsValues>

Would produce the following:

<html xmlns:og="http://ogp.me/ns#" xmlns:product="http://ogp.me/ns/product#">

prefix version in the &lt;head&gt; tag

To create the head version in a cshtml page, after create a new graph, use the following code:

<head prefix="@graph.HeadPrefixAttributeValue">

Would produce the following:

<head prefix="og: http://ogp.me/ns# product: http://ogp.me/ns/product#">

Writing out OpenGraph Metadata to the head tag

Below is a complete example to write out a OpenGraph metadata to a page:

@{
    var graph = OpenGraph.MakeGraph(
        title: "My Title", 
        type: "website", 
        image: "http://example.com/img/img1.png", 
        url: "http://example.com/home", 
        description: "My Description", 
        siteName: "Example.com");
}
<html>
<head prefix="@graph.HeadPrefixAttributeValue">
    @graph.ToString()
</head>
<body>
    <!-- Your awesome page! -->
</body>
</html>

will produce the following HTML:

<html>
<head prefix="og: http://ogp.me/ns#">
    <meta property="og:title" content="My Title">
    <meta property="og:type" content="website">
    <meta property="og:image" content="http://example.com/img/img1.png">
    <meta property="og:url" content="http://example.com/home">
    <meta property="og:description" content="My Description">
    <meta property="og:site_name" content="Example.com">
</head>
<body>
    <!-- Your awesome page! -->
</body>
</html>

It's FOSS

So please don't be afraid to fork me.

Contribution Guide

  1. Fork the OpenGraph-Net repository
  2. Create a feature branch for the item you are going to add.
  3. Add your awesome code and your unit tests to cover the new feture
  4. Run all of the tests to ensure everything is still passing.
  5. Create a pull request to our develop branch.

OpenGraphNet

AppVeyor
Nuget V
Nuget dl
License
gitter

A simple .net assembly to use to parse Open Graph information from either a URL or an HTML snippet. You can read more about the
Open Graph protocol @ http://ogp.me.

Usage

These are the basic operations of the OpenGraphNet parser.

Parsing from a URL

Synchronosly parse a url:

OpenGraph graph = OpenGraph.ParseUrl("https://open.spotify.com/user/er811nzvdw2cy2qgkrlei9sqe/playlist/2lzTTRqhYS6AkHPIvdX9u3?si=KcZxfwiIR7OBPCzj20utaQ");

Use async/await to parse a url:

OpenGraph graph = await OpenGraph.ParseUrlAsync("https://open.spotify.com/user/er811nzvdw2cy2qgkrlei9sqe/playlist/2lzTTRqhYS6AkHPIvdX9u3?si=KcZxfwiIR7OBPCzj20utaQ");

Acessing Values

Accessing Metadata

Each metadata element is is stored as an array. Additionally, each element's properties are also stored as an array.

<meta property="og:image" content="http://example.com/img1.png">
<meta property="og:image:width" content="30">
<meta property="og:image" content="http://example.com/img2.png">
<meta property="og:image:width" content="60">
<meta property="og:locale" content="en">
<meta property="og:locale:alternate" content="en_US">
<meta property="og:locale:alternate" content="en_GB">

You would access the values from the sample HTML above as:

  • graph.Metadata[&quot;og:image&quot;].First().Value // &quot;http://example.com/img1.png&quot;.
  • graph.Metadata[&quot;og:image&quot;].First().Properties[&quot;width&quot;].Value() // &quot;30&quot;.
  • graph.Metadata[&quot;og:image&quot;][1].Value // &quot;http://example.com/img2.png&quot;.
  • graph.Metadata[&quot;og:image&quot;][1].Properties[&quot;width&quot;].Value() // &quot;30&quot;.
  • graph.Metadata[&quot;og:locale&quot;].Value() // &quot;en&quot;
  • graph.Metadata[&quot;og:locale&quot;].First().Properties[&quot;alternate&quot;][0].Value // &quot;en_US&quot;
  • graph.Metadata[&quot;og:locale&quot;].First().Properties[&quot;alternate&quot;][1].Value // &quot;en_GB&quot;

Basic Metadata

The four required Open Graph properties for all pages are available as direct properties on the OpenGraph object.

  • graph.Type is a shortcut for graph.Metadata[&quot;og:type&quot;].Value()
  • graph.Title is a shortcut for graph.Metadata[&quot;og:title&quot;].Value()
  • graph.Image is a shortcut for graph.Metadata[&quot;og:image&quot;].Value()
    Note: since there can be multiple images, this helper returns the URI of the
    first image. If you want to access images or child properties like og:image:width then you
    should instead use the graph.Metadata dictionary.
  • graph.Url is a shortcut for graph.Metadata[&quot;og:url&quot;].Value()

Creating OpenGraph Data

To create OpenGraph data in memory use the following code:

var graph = OpenGraph.MakeGraph(
    title: "My Title", 
    type: "website", 
    image: "http://example.com/img/img1.png", 
    url: "http://example.com/home", 
    description: "My Description", 
    siteName: "Example.com");
graph.AddMetadata("og", "image", "http://example.com/img/img2.png");
graph.Metadata["og:image"][0].AddProperty("width", "30");
graph.Metadata["og:image"][1].AddProperty("width", "60");
System.Console.Write(graph.ToString());

The previous System.Console.Write(graph.ToString()); will produce the following HTML (formatting added for legibility):

<meta property="og:title" content="My Title">
<meta property="og:type" content="website">
<meta property="og:image" content="http://example.com/img/img1.png">
<meta property="og:image:width" content="30">
<meta property="og:image" content="http://example.com/img/img2.png">
<meta property="og:image:width" content="60">
<meta property="og:url" content="http://example.com/home">
<meta property="og:description" content="My Description">
<meta property="og:site_name" content="Example.com">

Writing out OpenGraph Namespaces

In the wild web sites seem to add their OpenGraph namespaces in one of 2 ways. They either
write the namespaces in the html as xmlns attributes or within the head tag in the prefix attribute.

  • &lt;html xmlns:og=&quot;http://ogp.me/ns#&quot; xmlns:product=&quot;http://ogp.me/ns/product#&quot;&gt;
  • &lt;head prefix=&quot;og: http://ogp.me/ns# product: http://ogp.me/ns/product#&quot;&gt;

xmlns: version in the html tag

To create the html version in an cshtml page after creating a new graph, use the following code:

<html @graph.HtmlXmlnsValues>

Would produce the following:

<html xmlns:og="http://ogp.me/ns#" xmlns:product="http://ogp.me/ns/product#">

prefix version in the &lt;head&gt; tag

To create the head version in a cshtml page, after create a new graph, use the following code:

<head prefix="@graph.HeadPrefixAttributeValue">

Would produce the following:

<head prefix="og: http://ogp.me/ns# product: http://ogp.me/ns/product#">

Writing out OpenGraph Metadata to the head tag

Below is a complete example to write out a OpenGraph metadata to a page:

@{
    var graph = OpenGraph.MakeGraph(
        title: "My Title", 
        type: "website", 
        image: "http://example.com/img/img1.png", 
        url: "http://example.com/home", 
        description: "My Description", 
        siteName: "Example.com");
}
<html>
<head prefix="@graph.HeadPrefixAttributeValue">
    @graph.ToString()
</head>
<body>
    <!-- Your awesome page! -->
</body>
</html>

will produce the following HTML:

<html>
<head prefix="og: http://ogp.me/ns#">
    <meta property="og:title" content="My Title">
    <meta property="og:type" content="website">
    <meta property="og:image" content="http://example.com/img/img1.png">
    <meta property="og:url" content="http://example.com/home">
    <meta property="og:description" content="My Description">
    <meta property="og:site_name" content="Example.com">
</head>
<body>
    <!-- Your awesome page! -->
</body>
</html>

It's FOSS

So please don't be afraid to fork me.

Contribution Guide

  1. Fork the OpenGraph-Net repository
  2. Create a feature branch for the item you are going to add.
  3. Add your awesome code and your unit tests to cover the new feture
  4. Run all of the tests to ensure everything is still passing.
  5. Create a pull request to our develop branch.

Release Notes

Add support for netstandard 2.1 and make fxcop a private reference

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.2.1 184 2/7/2020
3.2.0 4,327 6/15/2019
3.1.2 1,413 3/16/2019
3.1.1 295 2/28/2019
3.1.0 660 1/18/2019
3.0.0 3,275 12/5/2018
2.2.0 4,026 5/10/2018
2.0.0 541 5/2/2018
1.3.4 10,409 8/17/2016
1.3.3 404 8/16/2016
1.3.2 473 8/15/2016
1.3.1 473 8/15/2016
1.3.0.1 1,322 6/30/2016
1.2.0.1 5,908 4/2/2013