Skip to content

Commit

Permalink
removed poly references
Browse files Browse the repository at this point in the history
  • Loading branch information
robinkwilson committed Jun 2, 2021
1 parent a7e84ba commit 1e85db4
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 105 deletions.
1 change: 0 additions & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ config :ret, Ret.MediaResolver,
deviantart_client_secret: nil,
imgur_mashape_api_key: nil,
imgur_client_id: nil,
google_poly_api_key: nil,
youtube_api_key: nil,
sketchfab_api_key: nil,
ytdl_host: nil,
Expand Down
1 change: 0 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ config :ret, Ret.MediaResolver,
deviantart_client_secret: nil,
imgur_mashape_api_key: nil,
imgur_client_id: nil,
google_poly_api_key: nil,
youtube_api_key: nil,
sketchfab_api_key: nil,
ytdl_host: nil,
Expand Down
3 changes: 0 additions & 3 deletions habitat/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,6 @@ deviantart_client_secret = {{ toToml cfg.resolver.deviantart_client_secret }}
imgur_mashape_api_key = {{ toToml cfg.resolver.imgur_mashape_api_key }}
imgur_client_id = {{ toToml cfg.resolver.imgur_client_id }}
{{/if}}
{{ #if cfg.resolver.google_poly_api_key }}
google_poly_api_key = {{ toToml cfg.resolver.google_poly_api_key }}
{{/if}}
{{ #if cfg.resolver.youtube_api_key }}
youtube_api_key = {{ toToml cfg.resolver.youtube_api_key }}
{{/if}}
Expand Down
45 changes: 0 additions & 45 deletions lib/ret/media_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ defmodule Ret.MediaResolver do

@youtube_rate_limit %{scale: 8_000, limit: 1}
@sketchfab_rate_limit %{scale: 60_000, limit: 15}
@poly_rate_limit %{scale: 60_000, limit: 1000}
@max_await_for_rate_limit_s 120

@non_video_root_hosts [
Expand Down Expand Up @@ -74,13 +73,6 @@ defmodule Ret.MediaResolver do
|> resolved()}
end

# Necessary short circuit around google.com root_host to skip YT-DL check for Poly
def resolve(%MediaResolverQuery{url: %URI{host: "poly.google.com"}} = query, root_host) do
rate_limited_resolve(query, root_host, @poly_rate_limit, fn ->
resolve_non_video(query, root_host)
end)
end

def resolve(%MediaResolverQuery{} = query, root_host) when root_host in @non_video_root_hosts do
resolve_non_video(query, root_host)
end
Expand Down Expand Up @@ -294,43 +286,6 @@ defmodule Ret.MediaResolver do
{:commit, (resolved_url || uri) |> resolved(meta)}
end

defp resolve_non_video(
%MediaResolverQuery{url: %URI{host: "poly.google.com", path: "/view/" <> asset_id} = uri},
"google.com"
) do
[uri, meta] =
with api_key when is_binary(api_key) <- module_config(:google_poly_api_key) do
Statix.increment("ret.media_resolver.poly.requests")

payload =
"https://poly.googleapis.com/v1/assets/#{asset_id}?key=#{api_key}"
|> retry_get_until_success
|> Map.get(:body)
|> Poison.decode!()

meta =
%{expected_content_type: "model/gltf"}
|> Map.put(:name, payload["displayName"])
|> Map.put(:author, payload["authorName"])
|> Map.put(:license, payload["license"])

formats = payload |> Map.get("formats")

uri =
(Enum.find(formats, &(&1["formatType"] == "GLTF2")) || Enum.find(formats, &(&1["formatType"] == "GLTF")))
|> Kernel.get_in(["root", "url"])
|> URI.parse()

Statix.increment("ret.media_resolver.poly.ok")

[uri, meta]
else
_err -> [uri, nil]
end

{:commit, uri |> resolved(meta)}
end

defp resolve_non_video(
%MediaResolverQuery{url: %URI{path: "/models/" <> model_id}} = query,
"sketchfab.com" = root_host
Expand Down
52 changes: 0 additions & 52 deletions lib/ret/media_search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -169,46 +169,6 @@ defmodule Ret.MediaSearch do
sketchfab_search(query)
end

def search(%Ret.MediaSearchQuery{source: "poly", cursor: cursor, filter: filter, q: q}) do
with api_key when is_binary(api_key) <- resolver_config(:google_poly_api_key) do
query =
URI.encode_query(
pageSize: @page_size,
maxComplexity: :MEDIUM,
format: :GLTF2,
pageToken: cursor,
category: filter,
keywords: q,
key: api_key
)

res =
"https://poly.googleapis.com/v1/assets?#{query}"
|> retry_get_until_success()

case res do
:error ->
:error

res ->
decoded_res = res |> Map.get(:body) |> Poison.decode!()
entries = decoded_res |> Map.get("assets") |> Enum.map(&poly_api_result_to_entry/1)
next_cursor = decoded_res |> Map.get("nextPageToken")

{:commit,
%Ret.MediaSearchResult{
meta: %Ret.MediaSearchResultMeta{
next_cursor: next_cursor,
source: :poly
},
entries: entries
}}
end
else
_ -> nil
end
end

def search(%Ret.MediaSearchQuery{source: "youtube_videos", cursor: cursor, filter: filter, q: q}) do
with api_key when is_binary(api_key) <- resolver_config(:youtube_api_key) do
query =
Expand Down Expand Up @@ -335,7 +295,6 @@ defmodule Ret.MediaSearch do
end
end

def available?(:poly), do: has_resolver_config?(:google_poly_api_key)
def available?(:bing_images), do: has_resolver_config?(:bing_search_api_key)
def available?(:bing_videos), do: has_resolver_config?(:bing_search_api_key)
def available?(:youtube_videos), do: has_resolver_config?(:youtube_api_key)
Expand Down Expand Up @@ -806,17 +765,6 @@ defmodule Ret.MediaSearch do
}
end

defp poly_api_result_to_entry(result) do
%{
id: result["name"],
type: "poly_model",
name: result["displayName"],
attributions: %{creator: %{name: result["authorName"]}},
url: "https://poly.google.com/view/#{result["name"] |> String.replace("assets/", "")}",
images: %{preview: %{url: result["thumbnail"]["url"]}}
}
end

defp youtube_api_result_to_entry(result) do
%{
id: result["id"]["videoId"],
Expand Down
1 change: 0 additions & 1 deletion lib/ret/meta.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ defmodule Ret.Meta do
def available_integrations_meta do
%{
twitter: Ret.TwitterClient.available?(),
poly: Ret.MediaSearch.available?(:poly),
bing_images: Ret.MediaSearch.available?(:bing_images),
bing_videos: Ret.MediaSearch.available?(:bing_videos),
youtube_videos: Ret.MediaSearch.available?(:youtube_videos),
Expand Down
4 changes: 2 additions & 2 deletions lib/ret_web/controllers/api/v1/media_search_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ defmodule RetWeb.Api.V1.MediaSearchController do
end

def index(conn, %{"source" => source} = params)
when source in ["sketchfab", "poly", "tenor", "youtube_videos", "bing_videos", "bing_images", "twitch"] do
when source in ["sketchfab", "tenor", "youtube_videos", "bing_videos", "bing_images", "twitch"] do
query = %Ret.MediaSearchQuery{
source: source,
cursor: params["cursor"],
Expand All @@ -97,7 +97,7 @@ defmodule RetWeb.Api.V1.MediaSearchController do

# For Google services, increase cache duration for landing pages by using long-lived cache, due to quotas.
defp cache_for_query(%Ret.MediaSearchQuery{source: source, q: nil})
when source == "youtube_videos" or source == "poly",
when source == "youtube_videos",
do: :media_search_results_long

defp cache_for_query(_query), do: :media_search_results
Expand Down

0 comments on commit 1e85db4

Please sign in to comment.