How to get currently playing song?

I want to create a widget that displays the currently playing song on spotify. I tried using the spotify API but I get an error when trying ti exchange the code for the access token I need (no idea why). After trying that for hours I had an idea. What if there alteady is a way to get the song without spotify. Is there any way in scriptable to read what song is currently playing on the iPhone?

I realize this isn’t as much fun as building your own :smiley:, but there is almost an app for that…

TuneTrack

The refresh frequency isn’t great. The widget doesn’t stay up to date…

If you can’t get the Spotify API to work, try Last.fm with a Spotify integration to your account. It’s what I’m working on for a Spotify widget now.

Just create a Last.fm account, connect it to Spotify, create a developer account, and get an API key.
From there, you can access their API and request the track history of any user with any username. No API authentication required, just include the API key in the query parameters.

Here’s the documentation page you’ll need to get started with their API:

I haven’t written the code for it yet (or I’d post it for you) but after you figure out what you want from the API, the code should be pretty easy using Request().

Hello, i made here a widget that displays the currently playing song on spotify using the Last.fm API.
For this to work you’ll need to sign in the last.fm web and follow the steps like the comment above from @ [jacobsnarr]
“Just create a Last.fm account, connect it to Spotify, create a developer account, and get an API key.”

You’ll have to paste you user name and api key in the code.
I couldnt make the widget update more frecuently, if someone can just let me know.

//Replace "USER" with you last.fm user and "APIKEY" with the api key for your user.

let url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=USER&api_key=APIKEY&format=json&limit=1" 
let req = new Request(url)
async function createWidget(nowPlaying) {
 
  let widget = new ListWidget()
    // load image
  const coverArt = await loadImage(nowPlaying.recenttracks.track[0].image[3]["#text"])
     widget.backgroundImage = coverArt
     
  widget.addSpacer()
      // set gradient background
  let startColor = new Color("#1c1c1c19")
  let endColor = new Color("#1c1c1cb4")
  let gradient = new LinearGradient()
  gradient.colors = [startColor, endColor]
  gradient.locations = [0.0, 1]
  widget.backgroundGradient = gradient
    widget.backgroundColor = new Color("1c1c1c")
  // add title and artist
    let title = nowPlaying.recenttracks.track[0].name.toLowerCase()
    // capitalize every first character 
    title = title.replace(/\b\w/g, function(c) {
      return c.toUpperCase();
    });
  let titleTxt = widget.addText(title)
  titleTxt.font = Font.boldSystemFont(12)
  titleTxt.textColor = Color.white()
  titleTxt.leftAlignText()
  widget.addSpacer(2)
  
    let artist = nowPlaying.recenttracks.track[0].artist["#text"]
    // capitalize every first character 
    artist = artist.replace(/\b\w/g, function(c) {
      return c.toUpperCase();
    });
  let artistTxt = widget.addText(artist)
  artistTxt.font = Font.systemFont(10)
  artistTxt.textColor = Color.yellow()
  artistTxt.textOpacity = 1
  artistTxt.leftAlignText()
  
  widget.setPadding(8, 15, 10, 5)
  widget.url = nowPlaying.recenttracks.track[0].url
  return widget
}
  
// helper function to load and parse a restful json api
async function loadNowPlaying(coverArt) {
  const req = new Request(url)
  const json = await req.loadJSON()
  
  return json
}
// helper function to download an image from a given url
async function loadImage(imgUrl) {
  const url = imgUrl !== null ? imgUrl : placeholder;
  const req = new Request(url)
  const image = await req.loadImage()
  
  return image
}
const nowPlaying = await loadNowPlaying()
const widget = await createWidget(nowPlaying)
Script.setWidget(widget)
Script.complete()
widget.presentSmall()

1 Like

I made a widget using the Spotify API. You can have a look at the code to see how to refresh a token. The initial query to obtain the access & refresh tokens is in the shortcut I built to simplify the process:

1 Like