All Projects → ghorsey → OpenGraph-Net

ghorsey / OpenGraph-Net

Licence: MIT License
.Net Open Graph Parser written in C#

Programming Languages

HTML
75241 projects
C#
18002 projects

Projects that are alternatives of or similar to OpenGraph-Net

LeagueReplayParser
C# library which can read some data from a .rofl file, and start a replay in the client. (no longer actively maintained)
Stars: ✭ 20 (-81.98%)
Mutual labels:  parse, nuget
Angourimath
Open-source symbolic algebra library for C# and F#. One of the most powerful in .NET
Stars: ✭ 266 (+139.64%)
Mutual labels:  parse, nuget
WPFBOGP
WordPress Open Graph plugin development
Stars: ✭ 13 (-88.29%)
Mutual labels:  opengraph, ogp
lastfm
Portable .Net library for Last.fm
Stars: ✭ 87 (-21.62%)
Mutual labels:  nuget
MediatR.Extensions.Autofac.DependencyInjection
Autofac plug-in for MediatR.
Stars: ✭ 30 (-72.97%)
Mutual labels:  nuget
AspSqliteCache
An ASP.NET Core IDistributedCache provider backed by SQLite
Stars: ✭ 39 (-64.86%)
Mutual labels:  nuget
Apos.Gui
UI library for MonoGame.
Stars: ✭ 77 (-30.63%)
Mutual labels:  nuget
expresol
Library for executing customizable script-languages in python
Stars: ✭ 11 (-90.09%)
Mutual labels:  parse
parse-torrent-file
DEPRECATED: Parse a .torrent file and return an object of keys/values
Stars: ✭ 62 (-44.14%)
Mutual labels:  parse
resourcemanagement-client
Lithnet FIM/MIM Service .NET Client Library
Stars: ✭ 17 (-84.68%)
Mutual labels:  nuget
ButtonCirclePlugin
Circle Buttons with icon for your Xamarin.Forms Applications
Stars: ✭ 96 (-13.51%)
Mutual labels:  nuget
crawler CIA CREST
R-crawler for CIA website (CREST)
Stars: ✭ 15 (-86.49%)
Mutual labels:  parse
ManagedShell
A library for creating Windows shell replacements using .NET.
Stars: ✭ 134 (+20.72%)
Mutual labels:  nuget
HardwareInformation
.NET Standard Cross-Platform Hardware Information Gatherer
Stars: ✭ 37 (-66.67%)
Mutual labels:  nuget
MIST
Implements change notification for properties (ie: INotifyPropertyChanged) using IL weaving and a custom Visual Studio build task.
Stars: ✭ 51 (-54.05%)
Mutual labels:  nuget
cmd-ts
💻 A type-driven command line argument parser
Stars: ✭ 92 (-17.12%)
Mutual labels:  parse
html-dom-parser
📝 HTML to DOM parser.
Stars: ✭ 56 (-49.55%)
Mutual labels:  parse
EasyFileTransfer
An easy way to transfer file with any size on network with tcp protocol.
Stars: ✭ 30 (-72.97%)
Mutual labels:  nuget
UrlBase64
A standards-compliant implementation of web/url-safe base64 encoding and decoding for .NET targets
Stars: ✭ 25 (-77.48%)
Mutual labels:  nuget
WcfSoapLogger
Capture SOAP request and response in C# WCF
Stars: ✭ 26 (-76.58%)
Mutual labels:  nuget

OpenGraphNet

Build Nuget V Nuget dl License

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

Synchronously 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");

Accessing Values

Accessing Metadata

Each metadata element 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["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:

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">

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 <html> or <head> 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.

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:

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:

<meta property="gah:brain" content="http://www.geoffhorsey.com/my-brain">
<meta property="gah:brain:size" content="tiny">

and the graph:

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.

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

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 <head> 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 feature
  4. Run all of the tests to ensure everything is still passing.
  5. Create a pull request to our develop branch.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].