Clypdown: Tool for grabbing music from clyp.it

in #programming8 years ago

So, I happen to like music, and programming. I also have an intense dislike of how a lot of stuff these days requires one to be "constantly online" to do stuff. So, when it comes to music, I like to keep copies of things I like "offline" so I can listen to stuff when I do not have reliable internet access.

Now, there is this awesome site named "clyp.it", where a lot of cool musicians, especially chiptune/electronic artists, share their music. I often find tunes I like on there, but there is a slight problem - the site sometimes does not offer a "download" button, if the artist decided to not enable downloads of their track!

Anyway, I figured, "why not find a way to download music from this site", and dug into the sites API. This post will document how exactly I went about doing this, in great detail.

The first thing I did, was look at the sites API documentation. The part that really caught my eye was the following (quoted from the API docs):

GET https://api.clyp.it/{id}

Gets all relevant metadata of an audio file. This includes location of the mp3 and ogg files, title, description, duration of audio file in seconds, URL of the player page for the given audio file, and latitude/longitude of where the audio file was recorded.

"API docs - clyp.it"

"Ok then". I can get the locations of the MP3 and OGG files for a track... This will be easy. So I fired off a request for a track using curl, which returned some JSON. I then piped this to jq to make it readable.

$ curl -s https://api.clyp.it/hrvttfzp | jq
{
  "Status": "DownloadDisabled",
  "CommentsEnabled": true,
  "Category": "None",
  "AudioFileId": "hrvttfzp",
  "Title": "Sabrepulse feat. Ten Thousand Free Men & Their Families - Chiptune Night",
  "Description": "#Sabrepulse #ChiptuneNight #Chiptune",
  "Duration": 214.491,
  "Url": "https://clyp.it/hrvttfzp",
  "Mp3Url": "https://a.clyp.it/hrvttfzp.mp3",
  "SecureMp3Url": "https://a.clyp.it/hrvttfzp.mp3",
  "OggUrl": "https://a.clyp.it/hrvttfzp.ogg",
  "SecureOggUrl": "https://a.clyp.it/hrvttfzp.ogg",
  "DateCreated": "2015-10-11T07:04:46.983Z"
}

"json response"

This JSON output is pretty simple to read. Of note are the "Mp3Url", "SecureMp3Url", "OggUrl", "SecureOggUrl" fields. These point to URL's where the MP3 or OGG of the track can be downloaded. We also want to grab the "Title" field so that we preserve the tracks name. The "Status" field indicates that downloading the track via the web interface is disabled, but hey, we are about to work around that.

So now we have the information we need. To download a track from Clyp.it, we simply have to do the following:

  1. Rewrite the host in the URL from clyp.it to api.clyp.it.
  2. Send a GET request and download the JSON response.
  3. Extract the "Title" and "Mp3Url" fields from the JSON response.
  4. Download the MP3 from the "Mp3Url" and save it (using the "Title").
  5. ?????
  6. PROFIT!

Implementing this was pretty easy. Python has a built-in JSON library for parsing JSON, and I used the requests library for handling the requests. I also decided to use the clint library so that the download would have a nice progress bar.

The whole thing came out to ~45 LoC, including boilerplate. I could have probably done this in a Bash script instead, but I prefer Python.

Here is a screenshot of the finished project.
clypdown.png

You may download the source code of this program on github, or, you could use the excellent youtube-dl software instead, which is actively maintained and has now got support for clyp.it (amongst many other sites).

Oh, one last thing... I actually did implement most of it as a shell script, just for fun, while writing this post.

#!/bin/bash
if [[ $# -eq 0 ]] ; then
    echo "use: $0 https://clyp.it/something"
    exit 0
fi
SUPPLIED_URL=$1
API_HOST="api.clyp.it"
NEW_URL="${SUPPLIED_URL/clyp.it/$API_HOST}"
echo "Rewrote $1 to $NEW_URL"
Mp3Url=$(curl -s https://api.clyp.it/hrvttfzp | jq ."Mp3Url" | cut -d '"' -f 2)
echo "Got $Mp3Url, downloading"
wget $Mp3Url

"clypit.sh"

As always, please do comment, and let me know if there is anything you would like me to write about. I am always open to suggestions and ideas, and also welcome criticism of both my writing and my code :)

Coin Marketplace

STEEM 0.12
TRX 0.34
JST 0.033
BTC 124979.30
ETH 4633.92
SBD 0.78