How I Built Spotify Alternative for Google Home
Tags: python, node, webdev, sideprojects
Published: 2021-05-02 12:49:11

It was Sunday night. Hackathon results were about to be announced and when I heard my team's name, I jumped out of my chair, I was thrilled. To add more excitement, I asked my Google Home to play *"We Are The Champions"* and to my surprise, it played totally different song. I could be disappointed but instead, I was inspired to know what I was building next.

Why did it play a different song? Because choosing songs is only available to Spotify Premium subscribers. Instead of subscribing, I decided to build an alternative myself.

{% twitter 1328395582252183553 %}

From the day I got my Google Home, I was wondering how to program it to do things I want. The first and easiest option was [IFTTT](https://ifttt.com). After connecting my Google Account, I was able to make simple applets that allowed to make Google Assistant say things back when certain questions were asked and bind it to actions like making a web request.

![IFTTT Simple Phrase Applet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2dqn4fly5bdkldgshuhf.png)

IFTTT wasn't quite what I was looking for, because it couldn't perform tasks like playing audio or programmatically generate a response. Then I discovered [Google Actions](http://actions.google.com/). When I saw its capabilities, I realized - this is the way. 

The first thing I needed to set up for the application was a webhook. So whenever the application gets invoked, Google Action makes a request to the webhook. One way to provide a publicly available URL for the webhook would be to deploy the application somewhere. Instead, for the development, I used JPRQ, a Ngrok Alternative for exposing a local webserver. You can read about how I built it. 

{% post azimjohn/how-i-built-ngrok-alternative-3n0g %}

![Setting Up WebHook in Google Actions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xa3mfocxvbo7cux61pad.png)

After setting up the webhook, my local server was receiving anything I say to Google Assistant after the application was invoked. I could easily send a media response - a playable URL, and the assistant would play it. Great!

The best tutorial I could find on Google Actions is [Behind the Actions](https://www.youtube.com/watch?v=Z1hxvniJ18s), it helped me to understand basic building blocks like *Scenes, Types, and Intents*. 

The next part is searching for songs. I first looked into available APIs provided by Deezer, Sound Cloud, and Apple Music. But none of them provided the full song, only the partial content. Then I decided to write a simple crawler to search songs from z1.fm. I thought about copyright issues, so I decided to keep MusicSpider to myself and not publish it for others.

{% youtube 6fVuZLaj9WI %}

Source code for MusicSpider is available on [Github](https://github.com/azimjohn/musicspider), both the web application built using Flask and the crawler for searching for songs.

To share my learnings, I gave a talk about "*Programmable Google Assistant*" where I also shared some of my other applications I built for Google Assistant.

{% youtube G1EigCSIsIs %}

Happy building,

Cheers