Project Mo’ Music – API

Shit i haven’t been writing on this blog as much as i want. backlog isn’t as fun as i’d hoped. hmm. Anyway, i’m trying. i am probably going to jump a bit between posts.

So i wanted to easily be able to use all my different services with ONE globally used API. An API that could be called through a HTTP call but also be used internally.

At this point i wanted to learn Python. I created a HTTP server (a story for another post) which i later hooked up to this API, however this post is going to be about how the api works.

First i want to talk about how you use it, as that was my point of entry for creating it; i first visualized how i wanted to work with it and then i simply made something that fitted my criteria.

So an API call is 2 things. A command chain  and some JSON data.

An example. We want to download the song “BrightSide – Tom Pepe” from Youtube and then play the song. To do this we send the following Command Chain and the following data.
Command Chain (CC):

/DOWNLOAD/ADD/PLAY/

Data:

{

 “url”: [“https://www.youtube.com/watch?v=aa3iw20IYwg”]

}

The server does the following:
1) It takes the first command in the CC, which is “DOWNLOAD” and do said command and then remove it from the CC. for a download command it needs a youtube-link.

1.2) It then looks at the data and checks if it has a string called “url”. The observant user would realize that the url-value is an array (a list of strings). This is because the download-command can take an unlimited amount of urls, and it will then download every single one.

1.3) if it does, it downloads that, and removes the url from the data – if no url is found it throws an error. After downloading the song, it then places the songname in the data, as a value to the key “song-name”. This is going to be an array aswell, because if we had downloaded multiple songs we wanted it to remember all the songnames.

2) As the next command is “ADD” it will then add a bunch of songs to the MPD queue. this command also needs data – and it takes the data from the “song-name” key. In our example it will simply add our recently downloaded song to the queue. As it adds songs it is going to remove the songs from the data-object.

3) The final command is “PLAY”. which simply means “play” on MPD. So it’s simply going to start playing the queued songs.

And that’s pretty much the idea of it. The code is simply downloadable below, heavily commented to make it understandable (i hope). It has to be said that the following is not the full source code as it  is on my RPi, as i have a bunch of different commands that fits my specific build – it’s more of an outline of the actual mechanics, where more commands are easily added. The download script is a seperate script and so is many of the actual functionalities (which i wont explain in this post). the point is simply to have a “hub” where all the different functionalities can be run from.

To be able to run the below sample you need the “mpd” and “Pytube” packages from PIP. The youtube download part and the convert file IS included but i will not describe in detail here. I might make a post about them at some point but. meh. wont bother