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

fix(fix/odesli-api-change): Fix streaming links feature since Odesli API change #19

Merged
merged 1 commit into from
Aug 27, 2024
Merged
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
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
Loading