Skip to content

Commit

Permalink
fix: Handle Odesli API changes (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
papey authored Aug 27, 2024
1 parent 4b2f1f7 commit af3e794
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 43 deletions.
24 changes: 6 additions & 18 deletions lib/streaming_finder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)

Expand All @@ -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
Expand Down
37 changes: 12 additions & 25 deletions lib/utils/odesli.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit af3e794

Please sign in to comment.