Add Last.Fm function to get session

This commit is contained in:
Claudio Ortolina
2025-05-05 20:07:18 +01:00
parent 7c547f2d80
commit 299cc468b6
2 changed files with 35 additions and 0 deletions
+6
View File
@@ -41,6 +41,12 @@ defmodule LastFm do
end
end
def get_session(token) do
last_fm_config = last_fm_config()
API.get_session(token, last_fm_config)
end
def auth_url do
last_fm_config = last_fm_config()
"https://www.last.fm/api/auth/?api_key=" <> last_fm_config.api_key
+29
View File
@@ -54,6 +54,35 @@ defmodule LastFm.API do
def retry_delay(_), do: nil
end
def get_session(token, config) do
# Note that params needs to be ordered alphabetically
params =
[
api_key: config.api_key,
method: "auth.getSession",
token: token
]
signature = signature(params, config.shared_secret)
params = Keyword.put(params, :api_sig, signature)
config
|> new_request()
|> Req.merge(url: "/", params: params)
|> Req.Request.append_response_steps(parse_error: &parse_error/1)
|> get_request()
end
defp signature(params, shared_secret) do
encoded_params =
Enum.map_join(params, fn {key, value} ->
"#{key}#{value}"
end)
:crypto.hash(:md5, encoded_params <> shared_secret) |> Base.encode16(case: :lower)
end
def get_recent_tracks(config) do
params =
config