https://github.com/goofansu/ogp
The Open Graph protocol library in Elixir
https://github.com/goofansu/ogp
elixir metatags opengraph social
Last synced: 9 months ago
JSON representation
The Open Graph protocol library in Elixir
- Host: GitHub
- URL: https://github.com/goofansu/ogp
- Owner: goofansu
- License: mit
- Created: 2021-05-28T10:27:57.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-09-01T14:00:02.000Z (over 1 year ago)
- Last Synced: 2025-05-03T00:42:52.419Z (9 months ago)
- Topics: elixir, metatags, opengraph, social
- Language: Elixir
- Homepage: https://hex.pm/packages/ogp
- Size: 60.5 KB
- Stars: 25
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ogp
The [Open Graph protocol](https://ogp.me/) library in Elixir.
[](https://github.com/goofansu/ogp/actions/workflows/ci.yml)
[](https://coveralls.io/github/goofansu/ogp?branch=main)
[](https://hex.pm/packages/ogp)
## Installation
```elixir
def deps do
[
{:ogp, "~> 1.1.0"}
]
end
```
## Usage
It is recommended to run [ogp.livemd](https://github.com/goofansu/ogp/blob/main/ogp.livemd) in [Livebook](https://github.com/elixir-nx/livebook) for more details.
### Parse HTML
```elixir
iex> html = """
"""
iex> OpenGraph.parse(html)
%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: "https://ia.media-imdb.com/images/rock.jpg",
locale: "en_GB",
site_name: "IMDb",
title: "The Rock",
type: "video.movie",
url: "https://www.imdb.com/title/tt0117500/",
video: "https://example.com/bond/trailer.swf"
}
```
### Fetch URL
```elixir
iex> OpenGraph.fetch!("https://github.com")
%OpenGraph{
audio: nil,
description: "GitHub is where over 65 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
}
```
Redirects are followed automatically by default.
```elixir
iex> OpenGraph.fetch!("https://producthunt.com")
[debug] redirecting to https://www.producthunt.com/
%OpenGraph{
title: " Product Hunt – The best new products in tech. ",
type: "article",
image: "https://ph-static.imgix.net/ph-logo-1.png",
url: "https://www.producthunt.com/",
audio: nil,
description: "Product Hunt is a curation of the best new products, every day. Discover the latest mobile apps, websites, and technology products that everyone's talking about.",
determiner: nil,
locale: "en_US",
site_name: "Product Hunt",
video: nil
}
```
You can control redirects by configuring `req_options`.
- Disable redirects:
```elixir
config :ogp,
req_options: [
redirect: false
]
```
- Set a different `max_redirects` (default is `10`):
```elixir
config :ogp,
req_options: [
max_redirects: 3
]
```
See https://hexdocs.pm/req/Req.html#new/1-options for the full `req` options.