summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/last_fm_plugin.ex
diff options
context:
space:
mode:
authorhref <href@random.sh>2021-09-02 13:00:31 +0200
committerhref <href@random.sh>2021-09-02 13:00:31 +0200
commitb534510f6545bf2a6ff7636999f2d0d24f4a5c34 (patch)
treebfda78dc7515700723c11feb50f4bace7294cf3c /lib/lsg_irc/last_fm_plugin.ex
parentpuppet connections (diff)
lastfm: improvements, fixes
Diffstat (limited to 'lib/lsg_irc/last_fm_plugin.ex')
-rw-r--r--lib/lsg_irc/last_fm_plugin.ex76
1 files changed, 49 insertions, 27 deletions
diff --git a/lib/lsg_irc/last_fm_plugin.ex b/lib/lsg_irc/last_fm_plugin.ex
index 5bf582d..04f887c 100644
--- a/lib/lsg_irc/last_fm_plugin.ex
+++ b/lib/lsg_irc/last_fm_plugin.ex
@@ -89,7 +89,8 @@ defmodule LSG.IRC.LastFmPlugin do
defp irc_now_playing(nick_or_user, message, state) do
nick_or_user = String.strip(nick_or_user)
- id_or_user = if account = IRC.Account.find_always_by_nick(message.network, message.channel, nick_or_user) do
+
+ id_or_user = if account = IRC.Account.get(nick_or_user) || IRC.Account.find_always_by_nick(message.network, message.channel, nick_or_user) do
account.id
else
nick_or_user
@@ -97,15 +98,22 @@ defmodule LSG.IRC.LastFmPlugin do
username = case :dets.lookup(state.dets, id_or_user) do
[{_, username}] -> username
- _ -> id_or_user
+ _ ->
+ id_or_user
end
case now_playing(username) do
{:error, text} when is_binary(text) ->
message.replyfun.(text)
{:ok, map} when is_map(map) ->
- text = format_now_playing(map)
- user = lookup_nick(username, state)
+ track = fetch_track(username, map)
+ text = format_now_playing(map, track)
+ user = if account = IRC.Account.get(username) do
+ user = IRC.UserTrack.find_by_account(message.network, account)
+ if(user, do: user.nick, else: account.name)
+ else
+ username
+ end
if user && text do
message.replyfun.("#{user} #{text}")
else
@@ -118,26 +126,40 @@ defmodule LSG.IRC.LastFmPlugin do
defp now_playing(user) do
api = Application.get_env(:lsg, :lastfm)[:api_key]
- url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&format=json&limit=1" <> "&api_key=" <> api <> "&user="<> user
+ url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&format=json&limit=1&extended=1" <> "&api_key=" <> api <> "&user="<> user
case HTTPoison.get(url) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} -> Jason.decode(body)
- {:ok, %HTTPoison.Response{status_code: 400}} -> {:error, "last.fm: utilisateur #{user} inexistant"}
+ {:ok, %HTTPoison.Response{status_code: 404}} -> {:error, "last.fm: utilisateur \"#{user}\" inexistant"}
{:ok, %HTTPoison.Response{status_code: code}} -> {:error, "last.fm: erreur #{to_string(code)}"}
error ->
Logger.error "Lastfm http error: #{inspect error}"
:error
end
end
+ defp fetch_track(user, %{"recenttracks" => %{"track" => [ t = %{"name" => name, "artist" => %{"name" => artist}} | _]}}) do
+ api = Application.get_env(:lsg, :lastfm)[:api_key]
+ url = "http://ws.audioscrobbler.com/2.0/?method=track.getInfo&format=json" <> "&api_key=" <> api <> "&username="<> user <> "&artist="<>URI.encode(artist)<>"&track="<>URI.encode(name)
+ case HTTPoison.get(url) do
+ {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
+ case Jason.decode(body) do
+ {:ok, body} -> body["track"] || %{}
+ _ -> %{}
+ end
+ error ->
+ Logger.error "Lastfm http error: #{inspect error}"
+ :error
+ end
+ end
- defp format_now_playing(%{"recenttracks" => %{"track" => [track = %{"@attr" => %{"nowplaying" => "true"}}, _old]}}) do
- format_track(true, track)
+ defp format_now_playing(%{"recenttracks" => %{"track" => [track = %{"@attr" => %{"nowplaying" => "true"}} | _]}}, et) do
+ format_track(true, track, et)
end
- defp format_now_playing(%{"recenttracks" => %{"track" => [track]}}) do
- format_track(false, track)
+ defp format_now_playing(%{"recenttracks" => %{"track" => [track | _]}}, et) do
+ format_track(false, track, et)
end
- defp format_now_playing(%{"error" => err, "message" => message}) do
+ defp format_now_playing(%{"error" => err, "message" => message}, _) do
"last.fm error #{err}: #{message}"
end
@@ -145,22 +167,22 @@ defmodule LSG.IRC.LastFmPlugin do
nil
end
- defp format_track(np, track) do
- album = if track["album"]["#text"] do
- " (" <> track["album"]["#text"] <> ")"
- else
- ""
- end
- action = if np, do: "écoute ", else: "a écouté "
- action <> track["artist"]["#text"] <> " - " <> track["name"] <> album <> " — " <> track["url"]
- end
-
- defp lookup_nick(username, state) do
- case :dets.match(state.dets, {:'$1', username}) do
- [[match]] -> match
- [[match] | _many] -> match
- _ -> username
- end
+ defp format_track(np, track, extended) do
+ artist = track["artist"]["name"]
+ album = if track["album"]["#text"], do: " (" <> track["album"]["#text"] <> ")", else: ""
+ name = track["name"] <> album
+ action = if np, do: "écoute ", else: "a écouté"
+ love = if track["loved"] != "0", do: "❤️"
+ count = if x = extended["userplaycount"], do: "x#{x} #{love}"
+ tags = (get_in(extended, ["toptags", "tag"]) || [])
+ |> Enum.map(fn(tag) -> tag["name"] end)
+ |> Enum.filter(& &1)
+ |> Enum.join(", ")
+
+ [action, artist, name, count, tags, track["url"]]
+ |> Enum.filter(& &1)
+ |> Enum.map(&String.trim(&1))
+ |> Enum.join(" - ")
end
end