Monday, July 22, 2024
HomeJavaScriptUncover New Music with the Spotify API and Pipedream

Uncover New Music with the Spotify API and Pipedream

Frequent readers right here will know I am considerably fascinated by randomness. As just a few examples, I’ve constructed demos that depend on generated textual content: @TBSHoroscope and @MonsterConflict. I’ve additionally constructed demos that randomly choose from an current information set, together with @RandomComicBook and @NPSBot. All of those accounts make me smile after I see them present up in my timeline, they usually’ve been informative as nicely. The @RandomComicBook account has actually shocked me with how a lot artwork types have modified at Marvel over the many years in addition to simply how outdated some characters are.

I am an enormous Spotify fan, and one in every of my favourite options is asking it to play one music after which seeing how Spotify riffs off it and performs comparable music. This obtained me interested by utilizing Spotify’s APIs to actually riff by getting a totally random observe. Whereas I’ve genres I desire, I like music normally and can be keen to provide something a shot as soon as. With that in thoughts, I’ve constructed a Pipedream workflow that emails me one random observe each morning.

To date I’ve gotten:


I’ve heard of Slipknot earlier than however have by no means really listened to any of their music. Guess what – I do not like em! However I actually did take pleasure in enjoying the observe (and some others afterward).

Lil Uzi Vert

I’ve heard of Pharrell, however not Lil Uzi Vert. Once more, not my main model, however I preferred this one higher than the Slipknot observe.

Barry Gray

And in a totally totally different vein, a observe with a number of composers. I’ve heard of Hans ZImmer and Ramin Djawadi earlier than, however not the others. This was my favourite of the bunch.

So how was it constructed?

Getting a Random Spotify Observe

Spotify has a wealthy developer ecosystem. I first coated it again in February (Testing out the brand new Pipedream to Get Trance Releases) and I used to be impressed by how straightforward it was to make use of. Sadly, there is no such thing as a API to get a random observe. Nevertheless, I got here throughout a terrific tutorial on the best way to pretend it: Getting random tracks utilizing the Spotify API.

His method principally comes right down to utilizing random search strings and random offsets. His methodology labored, besides that on the time of publication, Spotify allowed an offset of as much as ten thousand. At present, the max is one thousand as an alternative. I made a decision to construct my model on Pipedream and make use of Python as a lot as doable.

I started with a step that generated a random search string. That is accomplished by choosing a random letter (a-z, technically a-zA-Z) after which returning it with both a p.c signal on the finish, or in entrance and on the finish. Here is the Python I used:

def handler(pd: "pipedream"):
  # Credit score:
  import random
  import string
  letter = random.selection(string.ascii_letters).decrease()

    search = letter + '%'
    search="%" + letter + '%'

  return search

Subsequent, I chosen an offset:

def handler(pd: "pipedream"):
  import random

  return random.randint(0,1000)

By the best way, I’ve each of the above Python scripts in distinctive steps, properly named (create_search_string and select_offeset). When working with Pipedream, I attempt to make every step as distinctive and atomic as doable. Pipedream would not care if I do two or extra issues in a single step, however I really feel prefer it’s higher architected this fashion.

The subsequent step is to carry out a search in opposition to Spotify. I added a brand new Spotify step and used Python to hit the API with my random search and offset:

import requests

def handler(pd: "pipedream"):
  token = f'{pd.inputs["spotify"]["$auth"]["oauth_access_token"]}'
  authorization = f'Bearer {token}'
  headers = {"Authorization": authorization}

  params = {
    "q": pd.steps["create_search_string"]["$return_value"],

  r = requests.get('', params=params, headers=headers)
  return r.json()

The necessary bit is the params block the place I entry the sooner steps. The results of this can be a ‘web page’ of tracks primarily based on the search, an array. Here is how that is rendered in Pipedream:

Result of API call

All we have to do now could be get a random observe from that step. Whereas constructing this, Zalman Lew on the Pipedream Slack let me know there’s really a Pipedream step inbuilt that allows you to level to an array of information and have a random one returned. So whereas I might have accomplished it in just a few strains of Python, I went with the built-in step:

At this level, I’ve obtained a randomly chosen observe worth I can use. How did I exploit it?

Ship the E mail

I made a decision to maintain it easy and have Pipedream ship me an e mail. You noticed the screenshots above and sure, it might actually look nicer. I first created a Python step to generate an HTML e mail string:

def handler(pd: "pipedream"):

  observe = pd.steps["select_random_track"]["$return_value"]
  artists = ""
  for artist in observe["artists"]:
      if artists == "":
        artists = artist["name"]
        artists = artists + ", " + artist["name"]

  html = f"""
  <a href="{ observe["external_urls"]["spotify"] }"><img src="{ observe["album"]["images"][1]["url"] }"></a>
  <h2>Your Random Spotify Observe</h2>
  Your random Spotify observe for immediately is <sturdy>{observe["name"]}</sturdy> by
  {artists}. It seems on the album "{ observe["album"]["name"] }" launched on 
  { observe["album"]["release_date"]}.

  Hearken to it right here: { observe["external_urls"]["spotify"] }


  return html

Spotify’s API returns lots of details about the observe, however I figured the title, artists, and album cowl was sufficient. With my HTML string full, I then simply added a built-in Pipedream “ship the account proprietor an e mail” step, and as at all times, I used the HTML worth for the textual content worth, and as I at all times say, do not do that in manufacturing, however for my testing, it labored high quality.

Sadly, you continue to cannot publicly share Pipedream V2 workflows, however if you wish to see extra of the workflow, be at liberty to succeed in out immediately. Let me know in the event you’ve obtained any questions! Edit: Dylan Pierce of Pipedream was in a position to make a sharable model of my workflow – so if you would like, you may seize it right here:

Photograph by Adrian Korte on Unsplash



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments