Sat Dec 12 2020Getting Real-time Data from Spotify, and why?

GitHub Repo

The other day I was rewatching Forrest Gump (just lockdown things), and the beginning scene about shoes struck me differently. Although it seems to be a very successful Nike placement, I think about the quote a lot.

notion image
Momma always says there's an awful lot you could tell about a person by their shoes. Where they're going. Where they've been.

Our life has transitioned differently in 2020 due to the obvious reason that I am not going to mention. We are living a more virtual life than ever before; my folk, the software people, were extremely lucky since most of us carried on working from home. Though, our social dynamics changed drastically; not just the online meetings at work, speaking with family on FaceTime became the new norm. I have a very close friend at work that we used to go to a pub together and talk about stuff after work. Now, unfortunately, we don't do this routine.

Shoes literally and figuratively leave footprints. Indeed, you can guess many things about people, how they feel, where they come from and where they are going with just a look. But what if you live most of your life on the internet nowadays? The assumptions you can make about someone is two dimensional and as far as what the webcam is showing. But, what about "behind the cameras"? <sub>I think I am killing with all these puns btw.</sub>

Yes, you can ask about your friends’ feelings and talk about your feelings as well. But most of my friends are young adult males who are into video games and memes, including myself. It is somehow secretly forbidden among us to talk or ask about emotions.

Social media might help but let’s not kid ourselves; the profiles are just the people we want to become.

This brings me to the songs we listen.

Songs in 2020 became one of the closest things to shoes in the same manner. You can make a lot of assumptions about the person from what they have been listening. Are they down, or happy? Have they been working out? Are they working? It’s literally there. Hence, I created this little application.

This is my little sign to show how I am feeling to the rest of the world.

notion image

Boring Stuff & Instructions

Create App on Spotify

Create an app from Spotify Developer Dashboard. Add http://localhost:4567 as the redirect URI to the app settings.

Generate Refresh Token

For apps that use the Spotify API, users are required to login once every 60 minutes. This can be resolved by implementing the Authorization Code Flow and getting a refresh token. Using this flow requires that you set up your own server that performs a token swap and refresh.

I have listed two ways to generate this token. The refresh tokens never expire unless the user revokes it.

1. Easy way

I've created an automated script refresh_token.rb within the ./scripts/ directory. It's a terminal-based application where you can input the required info and retrieve the necessary token.

Ruby not installed on your machine? Here I added this to repl.it.

2. Hard Way

Authenticate your application with the app's client id. The client id and client secret can be retrieved from the Spotify Developer Dashboard at any time.

<https://accounts.spotify.com/authorize?client_id={CLIENT_ID}&response_type=code&redirect_uri=http>
%3A%2F%2Flocalhost:4567&scope=user-read-currently-playing%20
user-top-read%20user-read-recently-played

Save the value of the code parameter.

Generate Base 64 encoded string with the following keys: client_id:client_secret

Post the info to Spotify. Example:

axios.post({
  url: "<https://accounts.spotify.com/api/token>",
  headers: {
    Authorization: "Basic {base64 encoded client_id:client_secret}",
    "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
  },
  data:
    "grant_type=authorization_code&code={AUTH_CODE}&redirect_uri=http%3A%2F%2Flocalhost%3A4567",
});

Demo

I am using URL Params to identify whether I am currently playing a song or request the most recent music I played.

The URL below gives the current song I am playing, if I am, of course! Otherwise, it will return false.

https://4tiumvx0uh.execute-api.eu-west-2.amazonaws.com/what-im-listening?preference=nowPlaying

This one, on the other hand, will give the most recent piece.

https://4tiumvx0uh.execute-api.eu-west-2.amazonaws.com/what-im-listening?preference=mostRecent

 

Stay up-to-date

Subscribe to my newsletter and stay up-to-date. Why not? It's free.