Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channels #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/tubex/api.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
defmodule Tubex.API do
require Logger

def get(url, query \\ []) do
HTTPoison.start
query = Tubex.Utils.encode_body(query)

unless String.length(query) == 0 do
url = "#{url}?#{query}"
end
url2 = if String.length(query) == 0, do: url, else: url <> "?" <> query

HTTPoison.get!(url, [])
HTTPoison.get!(url2, [])
|> handle_response
end

Expand All @@ -31,7 +30,8 @@ defmodule Tubex.API do
{:ok, Poison.decode!(body)}
end

defp handle_response(%HTTPoison.Response{body: body, status_code: _}) do
defp handle_response(%HTTPoison.Response{body: body, status_code: _} = response) do
Logger.warn("Response: #{inspect response}")
{:error, Poison.decode!(body)}
end

Expand Down
85 changes: 85 additions & 0 deletions lib/tubex/channel.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
defmodule Tubex.Channel do
require Logger

defstruct id: nil, etag: nil, content_details: nil, statistics: nil, topic_details: nil, status: nil

def find_by(:id, channel_id, opts) do
defaults = [key: Tubex.api_key, id: channel_id, part: "id"]
opts2 = Keyword.merge(defaults, opts)
request(opts2)
end

def find_by(:username, username, opts) do
defaults = [key: Tubex.api_key, username: username, part: "id"]
opts2 = Keyword.merge(defaults, opts)
request(opts2)
end

def find_by(type, key, opts) do
Logger.warn("Unknown find_by(#{inspect type}) with key #{inspect key} and options #{inspect opts}")
{:error, "Unknown request"}
end

defp request(opts) do
case Tubex.API.get(Tubex.endpoint <> "/channels", opts) do
{:ok, res} ->
{:ok, res["items"] |> Enum.map(&parse!/1), res["pageInfo"]}
error -> error
end
end

defp parse!(payload) do
case parse(payload) do
{:ok, channel} -> channel
{:error, reason} -> raise "Channel parse error: #{inspect reason}"
end
end

defp parse(payload) do
{:ok,
%Tubex.Channel{
id: payload["id"],
etag: payload["etag"],
content_details: parse_content_details(payload["contentDetails"]),
statistics: parse_statistics(payload["statistics"]),
topic_details: parse_topic_details(payload["topicDetails"]),
status: parse_status(payload["status"])
}
}
end

defp parse_content_details(content_details) do
%{
related_playlists: %{
uploads: get_in(content_details, ~w(relatedPlaylists uploads)),
watch_history: get_in(content_details, ~w(relatedPlaylists watchHistory)),
watch_later: get_in(content_details, ~w(relatedPlaylists watchLater))
}
}
end

defp parse_statistics(statistics) do
%{
view_count: statistics["viewCount"] |> String.to_integer,
comment_count: statistics["commentCount"] |> String.to_integer,
subscriber_count: statistics["subscriberCount"] |> String.to_integer,
hidden_subscriber_count: (statistics["hiddenSubscriberCount"] || "0") |> String.to_integer,
video_count: statistics["videoCount"] |> String.to_integer
}
end

defp parse_topic_details(topic_details) do
%{
ids: topic_details["topicIds"],
categories: topic_details["topicCategories"]
}
end

defp parse_status(status) do
%{
privacy_status: status["privacyStatus"],
is_linked: status["isLinked"],
long_uploads_status: status["longUploadsStatus"]
}
end
end
6 changes: 4 additions & 2 deletions lib/tubex/video.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ defmodule Tubex.Video do
@doc """
fetch contents details
"""
def detail(video_id) do
opts = [key: Tubex.api_key, id: video_id, part: "contentDetails"]
def detail(video_id, opts \\ []) do
defaults = [key: Tubex.api_key, id: video_id, part: "contentDetails"]
opts = Keyword.merge(defaults, opts)

case Tubex.API.get(Tubex.endpoint <> "/videos", opts) do
{:ok, response} ->
response
Expand Down
8 changes: 4 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ defmodule Tubex.Mixfile do
elixir: "~> 1.2",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps,
deps: deps(),
description: @description,
package: package]
package: package()]
end

# Configuration for the OTP application
Expand Down Expand Up @@ -39,8 +39,8 @@ defmodule Tubex.Mixfile do
# Type "mix help deps" for more examples and options
defp deps do
[
{:poison, "~> 2.0"},
{:httpoison, "~> 0.8.0"},
{:poison, "~> 3.1"},
{:httpoison, "~> 0.11.1"},
{:ex_doc, "~> 0.8.0", only: :dev},
{:earmark, ">= 0.0.0", only: :dev}
]
Expand Down
20 changes: 10 additions & 10 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
%{"certifi": {:hex, :certifi, "0.4.0"},
"earmark": {:hex, :earmark, "0.2.1"},
"ex_doc": {:hex, :ex_doc, "0.8.4"},
"hackney": {:hex, :hackney, "1.5.7"},
"httpoison": {:hex, :httpoison, "0.8.2"},
"idna": {:hex, :idna, "1.2.0"},
"metrics": {:hex, :metrics, "1.0.1"},
"mimerl": {:hex, :mimerl, "1.0.2"},
"poison": {:hex, :poison, "2.1.0"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.0"}}
%{"certifi": {:hex, :certifi, "1.1.0", "c9b71a547016c2528a590ccfc28de786c7edb74aafa17446b84f54e04efc00ee", [:rebar3], []},
"earmark": {:hex, :earmark, "0.2.1", "ba6d26ceb16106d069b289df66751734802777a3cbb6787026dd800ffeb850f3", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.8.4", "c74a30b09627ff22a2bb7f75d3b75dec3aedb2bd434bb3009a73a40425c2315d", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, optional: true]}]},
"hackney": {:hex, :hackney, "1.8.0", "8388a22f4e7eb04d171f2cf0285b217410f266d6c13a4c397a6c22ab823a486c", [:rebar3], [{:certifi, "1.1.0", [hex: :certifi, optional: false]}, {:idna, "4.0.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"httpoison": {:hex, :httpoison, "0.11.2", "9e59f17a473ef6948f63c51db07320477bad8ba88cf1df60a3eee01150306665", [:mix], [{:hackney, "~> 1.8.0", [hex: :hackney, optional: false]}]},
"idna": {:hex, :idna, "4.0.0", "10aaa9f79d0b12cf0def53038547855b91144f1bfcc0ec73494f38bb7b9c4961", [:rebar3], []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}}