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

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

Awesome Lists containing this project

README

          

# ogp

The [Open Graph protocol](https://ogp.me/) library in Elixir.

[![CI](https://github.com/goofansu/ogp/actions/workflows/ci.yml/badge.svg)](https://github.com/goofansu/ogp/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/goofansu/ogp/badge.svg?branch=main)](https://coveralls.io/github/goofansu/ogp?branch=main)
[![Version](https://img.shields.io/hexpm/v/ogp.svg)](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.