An open API service indexing awesome lists of open source software.

https://github.com/ghorsey/OpenGraph-Net

.Net Open Graph Parser written in C#
https://github.com/ghorsey/OpenGraph-Net

c-sharp nuget ogp opengraph opengraph-net parse

Last synced: 23 days ago
JSON representation

.Net Open Graph Parser written in C#

Awesome Lists containing this project

README

        

# OpenGraphNet

[![Build](https://github.com/ghorsey/OpenGraph-Net/actions/workflows/main.yml/badge.svg)](https://github.com/ghorsey/OpenGraph-Net/actions/workflows/main.yml)
[![Nuget V](https://img.shields.io/nuget/v/OpenGraph-Net.svg)](http://www.nuget.org/packages/OpenGraph-Net/)
[![Nuget dl](https://img.shields.io/nuget/dt/OpenGraph-Net.svg)](http://www.nuget.org/packages/OpenGraph-Net/)
[![License](https://img.shields.io/badge/license-MIT-orange.svg)](https://github.com/ghorsey/OpenGraph-Net/blob/main/LICENSE)
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](https://github.com/ghorsey/OpenGraph-Net/blob/main/CODE_OF_CONDUCT.md)
[![gitter](https://badges.gitter.im/webpack/webpack.svg)](https://gitter.im/OpenGraph-Net/OpenGraph-Net)
[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=round-square)](#contributors-)

[![Open in DevPod!](https://devpod.sh/assets/open-in-devpod.svg)](https://devpod.sh/open#https://github.com/ghorsey/OpenGraph-Net)

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 @ .

## Support the library

If you find this library useful, [buy me a coffee](https://www.buymeacoffee.com/ghorsey)!

## Usage

These are the basic operations of the OpenGraphNet parser.

### Parsing from a URL

Use `async/await` to parse a URL:

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

### Accessing Values

#### Accessing Metadata

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

```html

```

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

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

#### 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["og:type"].Value()`
* `graph.Title` is a shortcut for `graph.Metadata["og:title"].Value()`
* `graph.Image` is a shortcut for `graph.Metadata["og:image"].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["og:url"].Value()`

#### Misc

The original URL used to generate the OpenGraph data is available from the `OriginalUrl` property
`graph.OriginalUrl`.

### Creating OpenGraph Data

To create OpenGraph data in memory use the following code:

```csharp
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):

```html

```

### Parsing Namespaces

The component now knows about the 13 namespaces listed below. When parsing a url or a HTML
document, OpenGraph.Net will now read and use those namespaces from either the `` or
`` tags. The parser is now smart enough to include the namespaces when none are
included in those tags by extracting it from the `meta[property]` value directly.

* og:
* Expected fields when validating: `title`, `type`, `image`, `url`
* article:
* book:
* books:
* business
* fitness:
* game:
* music:
* place:
* product:
* profile:
* restaurant:
* video:

*If there are any additional standard/supported namespaces that I am missing, please shoot me
a comment or a pull request with the missing items.*

#### Adding Custom Namespaces

You can now add custom namespaces to the parser. Simply make the following call:

```csharp
NamespaceRegistry.Instance.AddNamespace(
prefix: "gah",
schemaUri: "http://wwww.geoffhorsey.com/ogp/brain#",
requiredElements: new[] { "brain" });
```

Doing the above will allow the parser to understand the following HTML snippet:

```html

```

and the graph:

```csharp
graph.Metadata["gah:brain"].Value() // "http://www.geoffhorsey.com/my-brain"
graph.Metadata["gah:brain"].First().Properties["size"].Value() // "tiny"
```

### 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.

* ``
* ``

**`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

```

Would produce the following:

```html

```

#### `prefix` version in the `` tag

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

```html

```

Would produce the following:

```html

```

### Writing out OpenGraph Metadata to the `head` tag

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

```html
@{
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.ToString()

```

will produce the following HTML:

```html






```

## It's FOSS

So please don't be afraid to [fork me](https://github.com/ghorsey/OpenGraph-Net).

### 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 feature
4. Run all of the tests to ensure everything is still passing.
5. Create a pull request to our `develop` branch.

## Contributors โœจ

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):



Geoff

๐Ÿ’ป ๐Ÿ“– ๐Ÿค” ๐Ÿ“ฆ ๐Ÿ“† โš ๏ธ

Per Osbรคck

๐Ÿ’ป

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!