0
Get the tracks by album
let apiKey := "8c6c8610-4f04-11ee-b74b-03c9318fae41";
let artist := substitute(text(Name), { " " : "%20" });
let album := substitute(text(Title), { " " : "%20" });
let url := "https://ws.audioscrobbler.com/2.0/?method=album.getinfo&artist=" + artist +
"&album=" + album +
"&api_key=" + apiKey +
"&format=json";
let headers := {};
let body := null;
let response := http("GET", url, headers, body);
let albumData := record(response, "album");
let tracksData := record(albumData, "tracks");
let trackList := tracksData.track;
let allTracks := "";
for t in trackList do
allTracks := allTracks + record(t, "name") + lineBreak()
end;
Tracks := allTracks
I'm trying to get the tracks by album from Last.fm but I haven't been able to and the AI isn't helping much. Any ideas?
4 replies
-
Hello Rafael,
I see you were trying to encode artist and album manually by replacing spaces with%20
.
However, it's better to useurlEncode()
in Ninox, because it also handles special characters, accents, and Unicode properly.let artist := urlEncode(Name); let album := urlEncode(Title);
In the second part, after retrieving the result, you need to iterate through the response using a key-value loop.
Here is the complete code:
let apiKey := "your-api-key"; let artist := urlEncode(Name); let album := urlEncode(Title); let url := --- https://ws.audioscrobbler.com/2.0/?method=album.getinfo &artist= { artist } &album={ album } &api_key={ apiKey } &format=json ---; let response := http("GET", url, {}, {}); let allTracks := for key, value in response.result do if key = "album" then join(for t in value.tracks.track do text(t.name) end, " ") end end; allTracks
-
Better
let api := "API Key"; let artist := urlEncode(Name); let album := urlEncode(Title); let url := "https://ws.audioscrobbler.com/2.0/?method=album.getinfo" + "&artist=" + artist + "&album=" + album + "&api_key=" + api + "&format=json"; let response := http("GET", url, {}, {}); let allTracks := ""; for key, value in response.result do if key = "album" then let i := 1; allTracks := join(for t in value.tracks.track do let line := number(i) + ". " + text(t.name); i := i + 1; line end, " ") end end; allTracks
-
let api := "Last.fm API"; let artistName := text(Name); let artistParam := replace(artistName, " ", "%20"); let albumParam := replace(Title, " ", "%20"); let url := "https://ws.audioscrobbler.com/2.0/?method=album.getinfo" + "&artist=" + artistParam + "&album=" + albumParam + "&api_key=" + api + "&format=json"; let response := http("GET", url, {}, {}); let found := false; if response.result then for key, value in response.result do if key = "album" then found := true; for t in value.tracks.track do let trackName := text(t.name); let durationSec := number(t.duration); let min := floor(durationSec / 60); let sec := durationSec - min * 60; let secText := if sec < 10 then "0" + text(sec) else text(sec) end; let durText := text(min) + ":" + secText; let durationText := if durationSec > 0 then "(" + durText + ")" else "(sin duración)" end; let trackParam := replace(trackName, " ", "%20"); let infoUrl := "https://ws.audioscrobbler.com/2.0/?method=track.getInfo&artist=" + artistParam + "&track=" + trackParam + "&api_key=" + api + "&format=json"; let info := http("GET", infoUrl, {}, {}); let listeners := if info.result and info.result.track and info.result.track.listeners then text(info.result.track.listeners) else "N/A" end; let newTrack := (create Tracks); newTrack.( Tracks := trackName; Title := replace(albumParam, "%20", " "); Duration := durationText; Artist := artistName; Listeners := listeners ) end end end end; if not found then "Álbum no encontrado o sin pistas." else "Importación completa." end
With this script I managed to transfer the tracks by album to the tracks table (Artist, Title, Tracks, Duration & Listeners)
Content aside
- Status Answered
- yesterdayLast active
- 4Replies
- 70Views
-
2
Following