What Should I Play next?

I’ve got a lot of records (At the time of writing 1,076 Discogs tells me) and I always seem to gravitate to the same ones. I decided I needed help to selecting something to play so I wrote Now Playing to help guide me.

The Discogs API

Like many, I record details of my collection on the online service Discogs, which contains a user-generated database of audio releases in whatever format they are or were available. It also has an API that allows you to access certain information that is held within its database. If you are authenticated that you are able to access your own collection and this was the information that I was interested in.

You can read about how to get the necessary token to authorise your calls in the documentation. Once you have this calls to the API can be made through a simple cURL request like the following which retrieves the user details.

    // get the user details
    $ch = curl_init($endpoint."/users/{$username}");
    curl_setopt($ch, CURLOPT_USERAGENT, 'MyDiscogsClient/1.0 +https://nei.lt');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET' );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: Discogs token={$token}"
    ]);

The Discogs API also makes use of pagination when returning results which we can use to our advantage as you will see.

Getting a Random Item from Your Collection

Here’s what my code does:

  1. obtain details about your Discogs account, specifically the number of items in your collection
  2. generates a random number between 1 and the number of items in your collection
  3. works out what page that item is on the downloads that page of results
  4. gets information about that release such as title and artist and the master release
  5. downloads and caches the cover art
  6. displays all the details on a webpage.

You may be wondering why I bother to cache the images. There are several reasons for this. The first is that it saves another round trip to the server once you have an image in your cache, making the response time quicker. The other reason is that I am using a very old iPad Mini sat next to my record player to use with the app. It is so old that it won’t display images from remote sites due to security issues; therefore, caching and displaying locally is the only way to get it working.

Show All

By default the code only selects LPs and 12″ singles to display as that is what I am interested in playing. If you want to include other types just extend out line 34. For example to include CDs:

while (!(in_array("CD", $desc) || in_array("LP", $desc) || in_array("12\"", $desc))) {

And if you want it to return anything irrespective of type simply comment out the while statement and closing curly brace.

As ever you can find the code on my Github page.

*And yes, I spent ages refreshing in order to get a screenshot of an album that I own that I thought was cool enough to display :D.

Leave a Reply

Your email address will not be published. Required fields are marked *