Skip to content

Instantly share code, notes, and snippets.

@goofansu
Last active Jun 23, 2022
Embed
What would you like to do?

ogp

Mix.install([
  {:kino, "~> 0.6.1"},
  {:ogp, "~> 1.0"}
])
Resolving Hex dependencies...
Dependency resolution completed:
New:
  castore 0.1.17
  finch 0.12.0
  floki 0.32.1
  hpax 0.1.1
  html_entities 0.5.2
  kino 0.6.1
  mime 2.0.2
  mint 1.4.2
  nimble_options 0.4.0
  nimble_pool 0.2.6
  ogp 1.0.2
  table 0.1.1
  telemetry 1.1.0
* Getting kino (Hex package)
* Getting ogp (Hex package)
* Getting finch (Hex package)
* Getting floki (Hex package)
* Getting html_entities (Hex package)
* Getting castore (Hex package)
* Getting mime (Hex package)
* Getting mint (Hex package)
* Getting nimble_options (Hex package)
* Getting nimble_pool (Hex package)
* Getting telemetry (Hex package)
* Getting hpax (Hex package)
* Getting table (Hex package)
You have added/upgraded packages you could sponsor, run `mix hex.sponsor` to learn more
==> nimble_options
Compiling 3 files (.ex)
Generated nimble_options app
==> hpax
Compiling 4 files (.ex)
Generated hpax app
==> nimble_pool
Compiling 2 files (.ex)
Generated nimble_pool app
===> Analyzing applications...
===> Compiling telemetry
==> html_entities
Compiling 2 files (.ex)
Generated html_entities app
==> floki
Compiling 1 file (.xrl)
Compiling 2 files (.erl)
Compiling 27 files (.ex)
Generated floki app
==> table
Compiling 3 files (.ex)
Generated table app
==> kino
Compiling 27 files (.ex)
Generated kino app
==> castore
Compiling 1 file (.ex)
Generated castore app
==> mint
Compiling 1 file (.erl)
Compiling 19 files (.ex)
Generated mint app
==> mime
Compiling 1 file (.ex)
Generated mime app
==> finch
Compiling 13 files (.ex)
Generated finch app
==> ogp
Compiling 3 files (.ex)
Generated ogp app
:ok

Usage

Parse HTML

Basic metadata

# Basic Metadata

basic_metadata = """
<meta property="og:title" content="The Rock" />
<meta property="og:type" content="video.movie" />
<meta property="og:url" content="https://www.imdb.com/title/tt0117500/" />
<meta property="og:image" content="https://ia.media-imdb.com/images/rock.jpg" />
<meta property="og:image" content="https://ia.media-imdb.com/images/rock2.jpg" />
"""

OpenGraph.parse(basic_metadata)
%OpenGraph{
  audio: nil,
  description: nil,
  determiner: nil,
  image: "https://ia.media-imdb.com/images/rock.jpg",
  locale: nil,
  site_name: nil,
  title: "The Rock",
  type: "video.movie",
  url: "https://www.imdb.com/title/tt0117500/",
  video: nil
}

Optional metadata

# Optional Metadata

optional_metadata = """
<meta property="og:audio" content="https://example.com/bond/theme.mp3" />
<meta property="og:description" 
  content="Sean Connery found fame and fortune as the
           suave, sophisticated British agent, James Bond." />
<meta property="og:determiner" content="the" />
<meta property="og:locale" content="en_GB" />
<meta property="og:locale:alternate" content="fr_FR" />
<meta property="og:site_name" content="IMDb" />
<meta property="og:video" content="https://example.com/bond/trailer.swf" />
"""

OpenGraph.parse(optional_metadata)
%OpenGraph{
  audio: "https://example.com/bond/theme.mp3",
  description: "Sean Connery found fame and fortune as the\n           suave, sophisticated British agent, James Bond.",
  determiner: "the",
  image: nil,
  locale: "en_GB",
  site_name: "IMDb",
  title: nil,
  type: nil,
  url: nil,
  video: "https://example.com/bond/trailer.swf"
}

Fetch URL

Input URL

url_input = Kino.Input.text("URL")
url = Kino.Input.read(url_input) |> String.trim("\n")
OpenGraph.fetch!(url)
%OpenGraph{
  audio: nil,
  description: "GitHub is where over 83 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and feat...",
  determiner: nil,
  image: "https://github.githubassets.com/images/modules/site/social-cards/github-social.png",
  locale: nil,
  site_name: "GitHub",
  title: "GitHub: Where the world builds software",
  type: "object",
  url: "https://github.com/",
  video: nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment