diff --git a/lib/streaming_finder.ex b/lib/streaming_finder.ex index 7ac73cb..b9d5cac 100644 --- a/lib/streaming_finder.ex +++ b/lib/streaming_finder.ex @@ -12,11 +12,10 @@ defmodule StreamingFinder do urls -> for url <- urls |> Enum.take(@max_urls) do case Odesli.get(url) do - {:ok, %Odesli.Response{artist: artist, title: title, urls: urls}} -> - Odesli.get(url) - - Nostrum.Api.create_message(origin.channel_id, - embed: message(artist, title, urls), + {:ok, %Odesli.Response{id: id, type: type, provider: provider}} -> + Nostrum.Api.create_message( + origin.channel_id, + content: message(type, id, provider), message_reference: %{message_id: origin.id} ) @@ -27,19 +26,8 @@ defmodule StreamingFinder do end end - defp message(artist, title, urls) do - formatted_links = - urls - |> Enum.map(fn {platform, url} -> - "[#{String.capitalize(platform)}](<#{url}>)" - end) - |> Enum.join(" - ") - - %Nostrum.Struct.Embed{ - :title => "#{artist} - #{title}", - :color => 431_948, - :description => formatted_links - } + defp message(type, id, provider) do + "https://#{type}.link/#{String.first(provider)}/#{id}" end defp extract_urls(content) do diff --git a/lib/utils/odesli.ex b/lib/utils/odesli.ex index 798cc06..8d4a3dd 100644 --- a/lib/utils/odesli.ex +++ b/lib/utils/odesli.ex @@ -1,46 +1,33 @@ defmodule Odesli do - @api_version "v1-alpha.1" - @base_url "https://api.song.link" - @links_url "#{@base_url}/#{@api_version}/links" + @base_url "https://api.odesli.co" + @resolve_path "resolve" - @platforms ["spotify", "deezer", "appleMusic", "youtube", "bandcamp", "tidal"] - - @timeout :timer.minutes(0.5) + @timeout 30_000 defmodule Response do - defstruct [:artist, :title, :urls] + defstruct [:type, :id, :provider] end def get(url) do - {:ok, resp} = HTTPoison.get("#{@links_url}?#{URI.encode_query(%{url: url})}", [], timeout: @timeout, recv_timout: @timeout) + {:ok, resp} = + HTTPoison.get("#{@base_url}/#{@resolve_path}?#{URI.encode_query(%{url: url})}", [], + timeout: @timeout, + recv_timeout: @timeout + ) case resp do %HTTPoison.Response{status_code: 200} -> parsed = Jason.decode!(resp.body) - meta = parsed["entitiesByUniqueId"] |> Map.values() |> List.first() - {:ok, %Response{ - artist: meta["artistName"], - title: meta["title"], - urls: plateform_urls(parsed) + id: parsed["id"], + type: parsed["type"], + provider: parsed["provider"] }} _ -> {:error, :no_match} end end - - defp plateform_urls(%{"linksByPlatform" => links}) do - Enum.reduce(@platforms, %{}, fn plt, acc -> - link = links[plt]["url"] - - if link do - Map.put(acc, plt, link) - else - acc - end - end) - end end