-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from CaptainFact/staging
Release 0.9.1
- Loading branch information
Showing
56 changed files
with
526 additions
and
509 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ until_fail.sh | |
# Secrets | ||
apps/*/priv/secrets/* | ||
!apps/*/priv/secrets/.keep | ||
**/*.secret.exs | ||
|
||
# Elixir LS | ||
.elixir_ls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,16 @@ | ||
defmodule CF.Videos.MetadataFetcher do | ||
@moduledoc """ | ||
Methods to fetch metadata (title, language) from videos | ||
Fetch metadata for video. | ||
""" | ||
|
||
require Logger | ||
|
||
alias Kaur.Result | ||
alias GoogleApi.YouTube.V3.Connection, as: YouTubeConnection | ||
alias GoogleApi.YouTube.V3.Api.Videos, as: YouTubeVideos | ||
alias GoogleApi.YouTube.V3.Model.Video, as: YouTubeVideo | ||
alias GoogleApi.YouTube.V3.Model.VideoListResponse, as: YouTubeVideoList | ||
|
||
alias DB.Schema.Video | ||
@type video_metadata :: %{ | ||
title: String.t(), | ||
language: String.t(), | ||
url: String.t() | ||
} | ||
|
||
@doc """ | ||
Fetch metadata from video. Returns an object containing :title, :url and :language | ||
Usage: | ||
iex> fetch_video_metadata("https://www.youtube.com/watch?v=OhWRT3PhMJs") | ||
iex> fetch_video_metadata({"youtube", "OhWRT3PhMJs"}) | ||
Takes an URL, fetch the metadata and return them | ||
""" | ||
def fetch_video_metadata(nil), | ||
do: {:error, "Invalid URL"} | ||
|
||
if Application.get_env(:db, :env) == :test do | ||
def fetch_video_metadata(url = "__TEST__/" <> _id) do | ||
{:ok, %{title: "__TEST-TITLE__", url: url}} | ||
end | ||
end | ||
|
||
def fetch_video_metadata(url) when is_binary(url), | ||
do: fetch_video_metadata(Video.parse_url(url)) | ||
|
||
def fetch_video_metadata({"youtube", provider_id}) do | ||
case Application.get_env(:cf, :youtube_api_key) do | ||
nil -> | ||
Logger.warn("No YouTube API key provided. Falling back to HTML fetcher") | ||
fetch_video_metadata_html("youtube", provider_id) | ||
|
||
api_key -> | ||
fetch_video_metadata_api("youtube", provider_id, api_key) | ||
end | ||
end | ||
|
||
defp fetch_video_metadata_api("youtube", provider_id, api_key) do | ||
YouTubeConnection.new() | ||
|> YouTubeVideos.youtube_videos_list("snippet", id: provider_id, key: api_key) | ||
|> Result.map_error(fn e -> "YouTube API Error: #{inspect(e)}" end) | ||
|> Result.keep_if(&(!Enum.empty?(&1.items)), "Video doesn't exist") | ||
|> Result.map(fn %YouTubeVideoList{items: [video = %YouTubeVideo{} | _]} -> | ||
%{ | ||
title: video.snippet.title, | ||
language: video.snippet.defaultLanguage || video.snippet.defaultAudioLanguage, | ||
url: Video.build_url(%{provider: "youtube", provider_id: provider_id}) | ||
} | ||
end) | ||
end | ||
|
||
defp fetch_video_metadata_html(provider, id) do | ||
url = Video.build_url(%{provider: provider, provider_id: id}) | ||
|
||
case HTTPoison.get(url) do | ||
{:ok, %HTTPoison.Response{body: body}} -> | ||
meta = Floki.attribute(body, "meta[property='og:title']", "content") | ||
|
||
case meta do | ||
[] -> {:error, "Page does not contains an OpenGraph title attribute"} | ||
[title] -> {:ok, %{title: HtmlEntities.decode(title), url: url}} | ||
end | ||
|
||
{_, _} -> | ||
{:error, "Remote URL didn't respond correctly"} | ||
end | ||
end | ||
@callback fetch_video_metadata(String.t()) :: {:ok, video_metadata} | {:error, binary()} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule CF.Videos.MetadataFetcher.Opengraph do | ||
@moduledoc """ | ||
Methods to fetch metadata (title, language) from videos | ||
""" | ||
|
||
@behaviour CF.Videos.MetadataFetcher | ||
|
||
@doc """ | ||
Fetch metadata from video using OpenGraph tags. | ||
""" | ||
def fetch_video_metadata(url) do | ||
case HTTPoison.get(url) do | ||
{:ok, %HTTPoison.Response{body: body}} -> | ||
meta = Floki.attribute(body, "meta[property='og:title']", "content") | ||
|
||
case meta do | ||
[] -> {:error, "Page does not contains an OpenGraph title attribute"} | ||
[title] -> {:ok, %{title: HtmlEntities.decode(title), url: url}} | ||
end | ||
|
||
{_, _} -> | ||
{:error, "Remote URL didn't respond correctly"} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule CF.Videos.MetadataFetcher.Test do | ||
@moduledoc """ | ||
Methods to fetch metadata (title, language) from videos | ||
""" | ||
|
||
@behaviour CF.Videos.MetadataFetcher | ||
|
||
@doc """ | ||
Fetch metadata from video using OpenGraph tags. | ||
""" | ||
def fetch_video_metadata(url) do | ||
{:ok, | ||
%{ | ||
title: "__TEST-TITLE__", | ||
url: url | ||
}} | ||
end | ||
end |
Oops, something went wrong.