Get bpm (beats per minute) of a song

Here is my final rule:

song_tempo.rules

import java.net.URLEncoder

rule "Song_Tempo"
    when
        Item HTPC_title changed
    then
        logInfo("Song Tempo", "artist or title changed")

            // wait until all items got updated
            Thread::sleep(250)
        if ( HTPC_mediatype.state == "song" ) {

            var String url     = "https://api.getsongbpm.com/search/?api_key="
            var String api_key = "fadc328ea3a34455a98c70c36f0ed85e"
            var String artist  = URLEncoder::encode(HTPC_artist.state.toString.split(",").get(0), 'UTF-8')
            var String song    = URLEncoder::encode(HTPC_title.state.toString, 'UTF-8')

            logInfo("Song Tempo", "artist: " + artist + " (" + HTPC_artist.state + ")")
            logInfo("Song Tempo", "song: "   + song   + " (" + HTPC_title.state  + ")")

            var String json = sendHttpGetRequest(url + api_key + "&type=both&lookup=artist:" + artist + "+song:" + song)
            var String value = transform("JSONPATH", "$.search[0].tempo", json)

            logInfo("Song Tempo", "request: " + url + api_key + "&type=both&lookup=artist:" + artist + "+song:" + song)
            logInfo("Song Tempo", "json: " + json)
            logInfo("Song Tempo", "Tempo: " + value)

            if ( value != null ) {
                sLED_tempo.sendCommand(value)
            } else {
                logInfo("Song Tempo", "No valid value returned.")
            }

        }
end

and a video. :sunglasses:

1 Like