Archiving Spotify playlists
Music Non-Stop
![](/var/linux_magazin/storage/images/issues/2014/167/perl-spotify/schilli_michael_6.png/624494-1-eng-US/Schilli_Michael_6.png_medium.png)
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.
![](/var/linux_magazin/storage/images/issues/2014/167/perl-spotify/figure-1/624497-1-eng-US/Figure-1_large.png)
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.
![](/var/linux_magazin/storage/images/issues/2014/167/perl-spotify/figure-3/624503-1-eng-US/Figure-3_large.png)
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
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.