Current section
Files
Jump to
Current section
Files
lib/extube.ex
defmodule Extube do
@moduledoc """
Extube is a wrapper for the hubtraffic API.
It gathers data from multiple tube sites including
pornhub,
redtube,
tube8,
youporn,
xtube,
spankwire,
keezmovies,
and extremetube.
"""
@doc """
Checks if a given `video_id` is active or not.
Input is the video_id as a string value.
Returns `true` or `false`.
If a video has been deleted/removed from the network, then it will return false.
If the video is currently still active and playing online, it will return true.
"""
def is_video_active(video_id) do
url = "https://www.pornhub.com/webmasters/is_video_active?id=" <> video_id
result = HTTPoison.get!(url)
|> Map.get(:body)
|> Poison.decode!
case hubtraffic_error_code(result["code"]) do
:error -> :error
:success -> result |> extract_active_value
end
end
@doc """
Queries the hubtraffic API to search for videos given a map of parameters.
Parameters right now include the following:
`:category`
`:page`
`:search`
`:ordering`
`:period`
`:thumbsize` (required)
For information on possible values of the parameters, see here
<https://www.hubtraffic.com/resources/api?site_id=3>
"""
def search_videos(params \\ %{thumbsize: "medium"}) do
base_url = "https://www.pornhub.com/webmasters/search?"
url = base_url <> URI.encode_query(params)
{:ok, data} = HTTPoison.get(url)
data_map = Poison.decode!(data.body)
case hubtraffic_error_code(data_map["code"]) do
:error -> {:error, data_map}
:success -> {:success, data_map["videos"]}
end
end
defp extract_active_value(result) do
case result["active"]["is_active"] do
"1" -> true
_ -> false
end
end
defp hubtraffic_error_code(code) do
error_codes = [
"1001",
"1002",
"1003",
"2001",
"2002",
"3001",
"3002",
"3003",
]
case (code in error_codes) do
true -> :error
false -> :success
end
end
end