Over the weekend I decided to do some tinkering with data from my long list of vinyl records. I’ve been trying to ensure that the more valuable LPs are stored away from the hands of my little cohorts – and popular website discogs.com happens to have both marketplace data, and a free web API. I have already matched LPs from my collection to releases documented on the Discogs website which can take hours over many years to get the correct matches. To learn more about Discog’s developer support, check out the Developer section on the site.
I thought this process would make for an interesting beginner’s guide to using PowerShell to interact in a meaningful way with Web APIs. As an aside, the DIscogs developer pages lack samples in C# or PowerShell, so I thought this might be useful for those who don’t speak Ruby, PHP or Python.
Why don’t we start out with a basic scenario. Say I just wanted to look up information about a single release (album), given the release number Discogs uses to identify individual LPs? I have a copy of the Smashing Pumpkins’ Siamese Dream, which happens to be release 1845456. To use PowerShell to make an API request, I need a few lines of code, and most of it is to format the result data.
First off, I put the release into a variable (making it clean to maintain). For basic release info, you don’t need to authenticate, so you can immediately launch a request of the API to return the release information, which is done easily like this:
Now, to get the marketplace information you need to have an authorization token. If you create an account on the site, you can request a personal use API token from the developer pages. You need to copy the personal use token and insert it into the script in order to make authenticated API calls.
To do this in PowerShell, define a variable for HTTP HEADER information, and then make an authenticated request like so:
If you planned to make greater use of the API, you’d instead use complete OAuth lifecycle access tokens, which is documented in the Discogs development pages.
Once you have this working, all you need to do is format the returned results into something more concrete, like this:
The Complete Script
Here is the complete script. Remember to insert your personal use token which you can generate by registering for a free account on the Discogs website.
In the next article
I’ll show you how to convert this basic script into a repeatable script which can process the details of hundreds of entries.
We’ll cover off:
- Reading from a CSV source
- Looping through items
- Pausing execution when errors occur
- Writing output to CSV
- Catering for resuming queries