Creating a REST client to get Tracks from Spotify REST API - Part4 Save the Search Result
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 ]
{
//w ##class(rest.spotify.artists).checkRowid("6eUKZXaKkcviH0Ku9w2n3V")set rtn=0set rowid=""
&sql(selectidinto :rowidfrom rest_spotify.artists where spotifyid=:spotifyid )
//w rowid,!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 ]
{
//w ##class(rest.spotify.album).checkRowid("6rUYFVeZl5rExNNV8seV98")set rtn=0set rowid=""
&sql(selectidinto :rowidfrom rest_spotify.album where spotifyid=:spotifyid )
//w rowid,!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 ]
{
//w ##class(rest.spotify.track).checkRowid("1cKuv7hAoX7ZN8f39hlDKX")set rtn=0set rowid=""
&sql(selectidinto :rowidfrom rest_spotify.track where spotifyid=:spotifyid )
//w rowid,!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 ]
{
//w ##class(rest.utli.spotifyUtli).getSearchResult()set rtn=0set a={}.%FromJSON(str) //from json tn dynamic object//w a,!// loop through the object using an iteratorset iter = a.%GetIterator()
while iter.%GetNext(.key , .value )
{
//write !, ?5, "Key: ", key, ", Value: ", value, " type: ", a.%GetTypeOf(key)_" with value "_a.%Get(key)if key="tracks"
{
//w !,?10,"call get tracks",!set std=##class(rest.utli.spotifyUtli).getTrakSearchResult(.value)
}
//set b=value//w !,?10,b.name
}
kill a
return rtn
}
ClassMethod getTrakSearchResult(ByRef tracksobj As%DynamicObject) As%Status [ Language = objectscript ]
{
//w ##class(rest.utli.spotifyUtli).getTrakSearchResult()set rtn=0// loop through the object using an iteratorset iter = tracksobj.%GetIterator()
while iter.%GetNext(.key , .value )
{
//write !, ?5, "Key: ", key, ", Value: ", valueif key="items"
{
// loop through the array using an iterator twice**set iter1=value.%GetIterator()
while iter1.%GetNext(.key1 , .value1 )
{
kill tkobj
set tkobj=##class(rest.spotify.track).%New()
set rowid=""//write !, ?10, "Key: ", key1, ", Value: ", value1set iter2=value1.%GetIterator()
while iter2.%GetNext(.key2 , .value2)
{
//write !, ?10, "Key: ", key2, ", Value: ", value2if key2="album"
{
//set std=##class(rest.utli.spotifyUtli).getTrakSearchAlbumResult(.value2) //w !,?15,"call get 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"
{
//w !,?15,value2.spotifyset tkobj.spotifyurl=value2.spotify
}
if key2="name"
{
//w !,?15,"name: ",value2set tkobj.name=value2
}
if key2="popularity"
{
//w !,?15,"popularity: ",value2set tkobj.popularity=value2
}
if key2="track_number"
{
//w !,?15,"track_number: ",value2set tkobj.tracknumber=value2
}
if key2="id"
{
//w !,?15,"id: ",value2set tkobj.spotifyid=value2
set rowid=##class(rest.spotify.track).checkRowid(value2)
}
}
if (rowid=0)
{
set std=tkobj.%Save()
}
kill tkobj
}
}
}
set rtn=1return rtn
}
ClassMethod getTrakSearchAlbumResult(ByRef albumobj As%DynamicObject, ByRef album As rest.spotify.album) As%Status [ Language = objectscript ]
{
//w ##class(rest.utli.spotifyUtli).getTrakSearchAlbumResult()set rtn=0kill alobj
set alobj=##class(rest.spotify.album).%New()
set rowid=""// loop through the object using an iteratorset iter = albumobj.%GetIterator()
while iter.%GetNext(.key , .value )
{
//write !, ?5, "Key: ", key, ", Value: ", valueif key="name"
{
//w !,?10,"name: ",valueset alobj.name=value
}
if key="release_date"
{
//w !,?10,"release_date: ",valueset alobj.releasedate=value
}
if key="id"
{
//w !,?10,"id: ",valueset 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
//w !,?10,"call get artists"
}
if key="album_type"
{
//w !,?10,"album_type: ",valueset alobj.albumtype=value
}
if key="external_urls"
{
//w !,?10,value.spotifyset 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=1return rtn
}
ClassMethod getTrakSearchArtistsResult(ByRef artistsobj As%DynamicObject, ByRef artist As rest.spotify.artists) As%Status [ Language = objectscript ]
{
//w ##class(rest.utli.spotifyUtli).getTrakSearchArtistsResult()set rtn=0// loop through the array using an iterator twice**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 )
{
//write !, ?5, "Key: ", key1, ", Value: ", value1if key1="id"
{
set aobj.spotifyid=value1
set rowid=##class(rest.spotify.artists).checkRowid(value1)
}
if key1="name"
{
//w !,?12,"name: ",value1set aobj.name=value1
}
if key1="external_urls"
{
//w !,?12,value1.spotifyset 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=1return 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
SELECTID, name, spotifyid, spotifyurl
FROM rest_spotify.artists.png)
b. Check the album by the following SQL
SELECTID, albumtype, artists->nameas artist, name, releasedate, spotifyid, spotifyurl
FROM rest_spotify.album.png)
c. Check the track by the following SQL
SELECTID, album->nameas album, album->artists->nameas artist, name, popularity, spotifyid, spotifyurl, tracknumber
FROM rest_spotify.track.png)
Yeah!!!! Working!!! I can create my track list by querying Spotify now!!!!!!!!ππ€£π