Archiving Spotify playlists
Music Non-Stop

For a monthly fee, the Spotify streaming service beams music onto your desktop or phone. To intensify the groove, Perlmeister Mike Schilli archived his Spotify playlists for eternity using an OAuth-protected web API.
The media beat is leaving legacy data carriers behind – and increasingly heading online. Faster even than newspapers are being replaced by e-papers, CDs are losing their original significance, with more and more consumers streaming music and movies.
At the same time, a change in licensing is on the horizon: A Blu-ray disk that you purchase and just view once often turns out to be a dust-catcher after a single evening's viewing pleasure; it just occupies valuable living space on specially designed shelves expensively acquired for nothing but this purpose. Three moves later, the owner is likely to sell the rarely touched box at the local flea market. So, why would you actually want to own copies of music and film products?
Marketers in a State of Shock
The established music marketers slept right through the trend. These "gramophone companies" would prefer to uphold their 100-year-old business model, and some artists are complaining, too [1] – whatever, I have my sights firmly set on the future already. Third-party providers, such as the video streamer Netflix in the United States, Canada, and the UK (and soon also in other European countries [2]) or music-only services like Pandora, Rhapsody, or Spotify [3] are making quite a good living out of online rental.
With these services, the customer does not acquire any digital content as a one-off purchase but pays a monthly subscription fee. In return, the provider undertakes to serve the customers via the Internet from a repertoire of millions of songs or movies. They either run on your PC; on small boxes like the Roku, Apple TV, Chromecast, or Amazon Fire; on your TV set; or directly on your Smartphone.
Some music rental providers offer a free radio program, in which the customer approximately defines the style but does not directly select any tracks. At times, annoying commercials interrupt your musical enjoyment. However, commercial versions offer a free choice of tracks from a more or less impressive catalogue, depending on the provider.
Subscribers can put together playlists (Figures 1 and 2) so their favorites reach the digital stage faster. The system is extremely convenient – as long as you remain a paying customer. If you cancel your subscription, however, you lose your painstakingly compiled playlists.

Above the Clouds
Because the playlists reside in the provider's data center and not on my local machine, this left me with a slight feeling of unease. What would happen with my laboriously compiled playlists if Spotify were to fold? Thanks to the web API [4] that is also offered, I managed to save the data locally and overcome my fear of loss.
Via its web API, Spotify allows general catalog queries for music products without registering. For example, if you want to find out what the California band Weezer released on which albums, you can use a simple HTTP request as shown in Listing 1 [5]. The q
(for query) parameter is set by the script to the name of the band; the type
is set to track
– this tells the server that the client is interested in tracks by the band in question.
Listing 1
search
The server sends back the results in JSON format; the from_json()
method from the CPAN JSON module converts them into a nested hash data structure. The for
loop from line 15 iterates over the array entries attached to the tracks-> items
key and returns the music titles it found, as well as the album they are on. Figure 3 shows the output, which is limited to 20 entries but can be expanded with the API's paging mechanism.

The script filters out the bulk of the information returned in JSON format. If you want, you can look out for the extra bits of information, like the popularity or play time, and additional details such as the release date of an album via links by dumping out everything to the screen.
And What's Your Favorite Music?
More interesting queries relate to the personal data of the Spotify user and require that user's consent. For example, to retrieve all playlists of a user, including the tracks, the requesting script needs to be registered as an application on the Spotify developer page [6], and it must identify itself with a valid OAuth token before Spotify releases the information. The instructions for application developers [7] describe the registration and handling of OAuth tokens step by step. However, Spotify only grants paying users access to the API.
Because I have presented several OAuth-controlled web APIs in this column – for example, by Tumblr [8], Google Drive [9], or Dropbox [10] – and have always used a Mojolicious script to obtain the first access token, it is about time that I wrote an extra CPAN module for this task: Scripts like the one shown in Listing 2 can henceforth use OAuth::Cmdline
to start a web server and obtain the consent of the user in a docking browser. In spotify-token-init
the two hex values in lines 9 and 10 accept the client identifier and client secret; the Spotify developer site hands these out to registered applications (Figures 4 and 5).
Listing 2
spotify-token-init
Additionally, the script user states the URLs provided by Spotify for login and token renewal as login_uri
and token_uri
in the OAuth::Cmdline
constructor. The extent of the permitted access is defined by the scope
parameter, where user-read-private
allows reading of private data. With write permission, scripts would be able to create playlists and add tracks to them.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
News
-
Another New Linux Laptop has Arrived
Slimbook has released a monster of a Linux gaming laptop.
-
Mozilla VPN Now Available for Linux
The promised subscription-based VPN service from Mozilla is now available for the Linux platform.
-
Wayland and New App Menu Coming to KDE
The 2021 roadmap for the KDE desktop environment includes some exciting features and improvements.
-
Deepin 20.1 has Arrived
Debian-based Deepin 20.1 has been released with some interesting new features.
-
CloudLinux Commits Over 1 Million Dollars to CentOS Replacement
An open source, drop-in replacement for CentOS is on its way.
-
Linux Mint 20.1 Beta has Been Released
The first beta of Linux Mint, Ulyssa, is now available for downloading.
-
Manjaro Linux 20.2 has Been Unleashed
The latest iteration of Manjaro Linux has been released with a few interesting new features.
-
Patreon Project Looks to Bring Linux to Apple Silicon
Developer Hector Martin has created a patreon page to fund his work on developing a port of Linux for Apple Silicon Macs.
-
A New Chrome OS-Like Ubuntu Remix is Now Available
Ubuntu Web looks to be your Chrome OS alternative.
-
System76 Refreshes the Galago Pro Laptop
Linux hardware maker has revamped one of their most popular laptops.