Last Chapter: Creating a REST client to get Tracks from Spotify REST API - Part3 Get some data (e.g. Artists)
Git link: https://github.com/ecelg/InterSystems-IRIS-as-a-Spotify-REST-client
OK we create a method to get data and lets try to get some Tracks 😁
Now open a terminal and test the code
.png)
Run the following line
w ##class(rest.utli.requestUtli).getdata("Spotify","/search","offset=5&limit=10&query=Shape%20of%20you&type=track&market=SG")
.png)
ooooo no seems there is huge among of data returns.....😥
I would like to know what information can be found in 1 track....🤔 how about only query 1 track?
let's try the following
w ##class(rest.utli.requestUtli).getdata("Spotify","/tracks/5cy3CNTBZbX8vZUIsu4p7K","market=SG")
.png)
Ok... there are quite a lot of information about a track...🤔... but I am pretty sure I don't need them all....🤔
maybe name, spotify url, album basic information and artists basic information are good enough for me.... just make it simple is ok
the hierarchy of the 3 object should be like this...
.png)
So... let's go
1. Build the object class to store the Artists information
create a spotify folder under rest
.png)
.png)
create a class artists.cls
.png)
.png)
Create a Persistent object and extend it with the %JSON.Adaptor for generating JSON string in a convenience way.
you can use a different key field name of the JSON string by adding the setting %JSONFIELDNAME
Class rest.spotify.artists Extends (%Persistent, %JSON.Adaptor)
{
Property name As %String
Property spotifyid As %String(%JSONFIELDNAME = "id")
Property spotifyurl As %String(%JSONFIELDNAME = "spotify", MAXLEN = 100)
ClassMethod checkRowid(spotifyid As %String = "") As %Integer [ Language = objectscript ]
{
set rtn=0
set rowid=""
&sql(select id into :rowid from rest_spotify.artists where spotifyid=:spotifyid )
if rowid'="" set rtn=rowid
return rtn
}
}
Save the class
.png)
2. Build the object class to store the Album information
under the spotify folder create a class album.cls
.png)
.png)
Create a Persistent object and extend it with the %JSON.Adaptor
Class rest.spotify.album Extends (%Persistent, %JSON.Adaptor)
{
Property name As %String
Property albumtype As %String(%JSONFIELDNAME = "album_type")
Property releasedate As %String(%JSONFIELDNAME = "release_date")
Property spotifyid As %String(%JSONFIELDNAME = "id")
Property spotifyurl As %String(%JSONFIELDNAME = "spotify", MAXLEN = 100)
Property artists As artists
ClassMethod checkRowid(spotifyid As %String = "") As %Integer [ Language = objectscript ]
{
set rtn=0
set rowid=""
&sql(select id into :rowid from rest_spotify.album where spotifyid=:spotifyid )
if rowid'="" set rtn=rowid
return rtn
}
}
Save the class
.png)
3. Build the object class to store the Track information
under the spotify folder create a class track.cls
.png)
.png)
Create a Persistent object and extend it with the %JSON.Adaptor
Class rest.spotify.track Extends (%Persistent, %JSON.Adaptor)
{
Property name As %String
Property popularity As %String
Property tracknumber As %String(%JSONFIELDNAME = "track_number")
Property spotifyid As %String(%JSONFIELDNAME = "id")
Property spotifyurl As %String(%JSONFIELDNAME = "spotify", MAXLEN = 100)
Property album As album
ClassMethod checkRowid(spotifyid As %String = "") As %Integer [ Language = objectscript ]
{
set rtn=0
set rowid=""
&sql(select id into :rowid from rest_spotify.track where spotifyid=:spotifyid )
if rowid'="" set rtn=rowid
return rtn
}
}
Save the class
.png)
4. Now, we try to study what is the result look like if we try to search a track
Let's try to run the following command in the terminal again
w ##class(rest.utli.requestUtli).getdata("Spotify","/search","offset=5&limit=10&query=Shape%20of%20you&type=track&market=SG")
.png)
paste the result in a JSON fromatter for easy reading
.png)
.png)
Mummmm...🤨 mummmmm..............🤔
I think ... I need to write a function to loop through the the items of the result JSON to get the tracks information and save it. We also need to save the album information inside the items.......😐........ and...... also.... loop through the artist information inside the items and save them........oooooooooooooooooooooo.... tones of work!!!
5. write some functions to loop though and save the search track result.
Create a class spotifyUtli.cls under the folder utli
.png)
.png)
write the following class methods
Class rest.utli.spotifyUtli Extends %RegisteredObject
{
ClassMethod getSearchResult(str As %String = "") As %Status [ Language = objectscript ]
{
set rtn=0
set a={}.%FromJSON(str)
set iter = a.%GetIterator()
while iter.%GetNext(.key , .value )
{
if key="tracks"
{
set std=##class(rest.utli.spotifyUtli).getTrakSearchResult(.value)
}
}
kill a
return rtn
}
ClassMethod getTrakSearchResult(ByRef tracksobj As %DynamicObject) As %Status [ Language = objectscript ]
{
set rtn=0
set iter = tracksobj.%GetIterator()
while iter.%GetNext(.key , .value )
{
if key="items"
{
set iter1=value.%GetIterator()
while iter1.%GetNext(.key1 , .value1 )
{
kill tkobj
set tkobj=##class(rest.spotify.track).%New()
set rowid=""
set iter2=value1.%GetIterator()
while iter2.%GetNext(.key2 , .value2)
{
if key2="album"
{
set album=##class(rest.spotify.album).%New()
set std=##class(rest.utli.spotifyUtli).getTrakSearchAlbumResult(.value2,.album)
set tkobj.album=album
kill album
}
if key2="external_urls"
{
set tkobj.spotifyurl=value2.spotify
}
if key2="name"
{
set tkobj.name=value2
}
if key2="popularity"
{
set tkobj.popularity=value2
}
if key2="track_number"
{
set tkobj.tracknumber=value2
}
if key2="id"
{
set tkobj.spotifyid=value2
set rowid=##class(rest.spotify.track).checkRowid(value2)
}
}
if (rowid=0)
{
set std=tkobj.%Save()
}
kill tkobj
}
}
}
set rtn=1
return rtn
}
ClassMethod getTrakSearchAlbumResult(ByRef albumobj As %DynamicObject, ByRef album As rest.spotify.album) As %Status [ Language = objectscript ]
{
set rtn=0
kill alobj
set alobj=##class(rest.spotify.album).%New()
set rowid=""
set iter = albumobj.%GetIterator()
while iter.%GetNext(.key , .value )
{
if key="name"
{
set alobj.name=value
}
if key="release_date"
{
set alobj.releasedate=value
}
if key="id"
{
set alobj.spotifyid=value
set rowid=##class(rest.spotify.album).checkRowid(value)
}
if key="artists"
{
set artist=##class(rest.spotify.artists).%New()
set std=##class(rest.utli.spotifyUtli).getTrakSearchArtistsResult(.value,.artist)
set alobj.artists=artist
kill artist
}
if key="album_type"
{
set alobj.albumtype=value
}
if key="external_urls"
{
set alobj.spotifyurl=value.spotify
}
}
if (rowid=0)
{
set std=alobj.%Save()
set album=alobj
}
if (rowid>0)
{
set album=##class(rest.spotify.album).%OpenId(rowid)
}
kill alobj
set rtn=1
return rtn
}
ClassMethod getTrakSearchArtistsResult(ByRef artistsobj As %DynamicObject, ByRef artist As rest.spotify.artists) As %Status [ Language = objectscript ]
{
set rtn=0
set iter = artistsobj.%GetIterator()
while iter.%GetNext(.key , .value )
{
kill aobj
set aobj=##class(rest.spotify.artists).%New()
set rowid=""
set iter1 = value.%GetIterator()
while iter1.%GetNext(.key1 , .value1 )
{
if key1="id"
{
set aobj.spotifyid=value1
set rowid=##class(rest.spotify.artists).checkRowid(value1)
}
if key1="name"
{
set aobj.name=value1
}
if key1="external_urls"
{
set aobj.spotifyurl=value1.spotify
}
}
if (rowid=0)
{
set std=aobj.%Save()
set artist=aobj
}
if (rowid>0)
{
set artist=##class(rest.spotify.artists).%OpenId(rowid)
}
kill aobj
}
set rtn=1
return rtn
}
}
Save the class
.png)
6. Test the class method getSearchResult
.png)
Run the following line
w ##class(rest.utli.spotifyUtli).getSearchResult(##class(rest.utli.requestUtli).getdata("Spotify","/search","offset=5&limit=10&query=Shape%20of%20you&type=track&market=SG"))
.png)
Seems ok.... so far....😀
7. Check the data from the table
.png)
.png)
a. check the artist by the following SQL
SELECT
ID, name, spotifyid, spotifyurl
FROM rest_spotify.artists
.png)
b. Check the album by the following SQL
SELECT
ID, albumtype, artists->name as artist, name, releasedate, spotifyid, spotifyurl
FROM rest_spotify.album
.png)
c. Check the track by the following SQL
SELECT
ID, album->name as album, album->artists->name as artist, name, popularity, spotifyid, spotifyurl, tracknumber
FROM rest_spotify.track
.png)
Yeah!!!! Working!!! I can create my track list by querying Spotify now!!!!!!!!😂🤣😉