Alexa routine/any song on sonos/save to spotify


#1

I’m trying to figure out how to get individual values for track name and track artist for every single song that is played through sonos, and set those as global variables. I’ve tried several things from looking at other codes, which sometimes works, but cannot figure out how to extract that information.
I thought maybe i could go through last.fm scrobble data, and I’m able to get info from that, which looks like this.

{“recenttracks”:{“track”:[{“artist”:{"#text":“Cottontail”,“mbid”:""},“name”:“Storyteller”,“streamable”:“0”,“mbid”:"",“album”:{"#text":“Chimney Chat”,“mbid”:""}

But the issue is still I suppose parsing that data. Ultimately the goal is to create an alexa routine, that when I say “i like this song” will make a request to webcore to get whatever song is currently playing, then send that information to ifttt’s spotify channel, to find that song and save it. This sounds so remarkably simple that I can’t imagine there isn’t a solution for people without node.js, but I’ve spent hours searching and have found no way to accomplish this.

Currently, when I send the track data of the sonos as a value to ifttt, the song, artist, and album are sent as one value, when it asks for two values, one a song name, the other, an artist. When all of it is as one value, the applet fails.

If anyone is able to break this down for me, it would be greatly appreciated.


#2

Oh. And I understand how to create the variables that I want information for. Just to be clear, I need help filling the appropriate information into those variables and sending that data on to ifttt, preferably with the make a web request option rather than ifttt maker, as this is going to someone else’s ifttt account.


#3

Sorry, tried a few things but don’t understand the syntax.


#4

You mean of the response? It also comes through as an XML file if that’s any easier.

<artist mbid=" "> Cottontail</artist>

#5

How did you get this data? It looks like a list within a list which I am not sure how to recreate for testing. Also, what you posted is not complete there is no ] and two missing }. I assume they are all at the end.


#6

Sorry. My ignorance knows no bounds in this area. I get this data by making a web request to the last.fm api, a key to which can be acquired from Their site.

Using that key, the request goes to

https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=[USER NAME]&api_key=[API KEY]&format=json&limit=1

I added the limit of one, because I only need the most recent to make the variable I’m after. What is returned is the following:

Censored by the forum. I can only post two links.
The data is full of links.
{“recenttracks”:{“track”:[{“artist”:{"#text":“Cheat Codes”,“mbid”:""},“name”:“Feeling of Falling”,“streamable”:“0”,“mbid”:"",“album”:{"#text":“Feeling of Falling”,“mbid”:""},“url”:

It goes on.
There are so many brackets and quotations and whatnot that for me, someone without the knowledge of how to extract the particular information in the first place, I’m at a complete loss. Without the &format=Jason, the returned information looks like this (Oops) I apparently can’t get the same result for that on my phone, but it is in xml and somewhat more concise looking.

Ultimately, current track data can be attained from sonos in probably the same way, and with less steps. I’ve looked at several other solutions like a discord bot called fmbot, integromat, and various ifttt workarounds all with the same result of needing to separate data into individual values. Having variables for artist and track is something I really feel several smartthings users could benefit from, besides being able to save a spotify song with alexa in non wordy ways. I’ll probably post there too.


#7

And that goes into a variable which you log and shows as what you provided? Is the variable a list type?


#8

No. It is the response.


#9

Sorry, this is beyond my expertise. The data is obviously all there but I am not sure how to access it.


#10

try this piston as an example:

in the device variable set it to your sonos speaker you are trying to monitor. then save the piston. it will not run by itself. you must press the test button to run it or call it from another piston.

then play a song on spotify on it.

then press the test piston. it should fill in the other variables and replay the song as a test that the url is correct for the song that was playing.


#11

OH! Well, that’s definitely something. When playing my own music through sonos, the station gives me an album and the uri gives a long collection of numbers and letters that somehow show the original file being played. Through spotify, nothing is really returned that is useful. But thank you, that gave me a little something more to play around with.


#12

I don’t know what speaker your using, but sonos track description is returned on three separate lines (yes I know you used track DATA to parse the json response), the first being song name, the second being the artist, and the third being the album. I feel like that could be differentiated somehow.


#13

$.response.recenttracks.track[:1].artist.#text gives me the test of the artist name. I do not know how to use that from the response to update a variable