Archiving Spotify playlists
Juggling with URLs
When the user launches the script in Listing 2, it outputs
Server available at http://localhost:8082
to inform the user about the web address they need to enter in their browser's address box. Then, the browser displays a link labeled Login on Spotify. If you press this, the browser redirects you to the Spotify site and prompts you to log in with your username and password.
If the user acquiesces to the terms (Figure 6), Spotify sends the browser back to the local test server – in Listing 2, this is localhost:8082
– and issues a valid access and refresh token. With the latter, OAuth::Cmdline later manages to renew expired access tokens. The script (Listing 2) saves the access credentials in the .spotify.yml
file in the user's home directory.
In other words, if the user presses Okay, the Spotify server branches back to the registered Redirect URI. The test server in OAuth::Cmdline::Mojo sets this to http://localhost:8082/callback. Make sure you entered this in exactly the same way when filling out the application on the Spotify developer site; otherwise, things will go wrong.
Caution: After you add a redirect URL, as shown in Figure 5, Spotify strangely requires you to click the Save
button at the bottom of the page. If you forget this, you will wonder later why you are seeing Bad Request or Invalid Redirect URI server error pages.
Access Until Further Notice
Not only can the current Unix user read the data in the newly created local credentials file, but also a curious system administrator (or a successful intruder) with root privileges – so be careful. Using this detour, the user has now granted the server permission to hand over user data to the PerlSnapshot application, until explicitly revoked; the app authenticates with a valid access token in the HTTP header for each request.
Recycling
Other scripts, like the one in Listing 3, can now call the constructor of OAuth::Cmdline with the site
parameter set to "spotify"
and extract a valid access token from the ~/.spotify.yml
cache file.
Listing 3
user-playlists
To read a user's playlists, in line 16 I use the authorization_headers
method from OAuth::Cmdline to send the access token required by the server in the subsequent GET request to the web API. Should the access token have expired in the meantime, OAuth::Cmdline automatically renews it behind the scenes. To accomplish this, the module uses the refresh token, which is also stored in the cache file, to send a request to the handler for the OAuth refresh service on the server.
The answer returned in JSON format contains the names of all the user's playlists and their hex IDs; later on, scripts such as Listing 4 retrieve the music referenced there. Thanks to OAuth::Cmdline, you no longer need to do battle with OAuth but can focus on the data to be read.
Listing 4
songs-in-playlist
After picking up the data of one of the previously obtained playlist IDs in Listing 3 (hardcoded in line 23 of Listing 4), the songs-in-playlist
script stores them in YAML format, which is easily readable by both humans and machines (Figure 7).
This puts worried and concerned music lovers in the happy position of being able to back up their playlists on a regular basis. In this way, all of the effort they put into their music collection is preserved – even if they would like to give a cold shoulder to their previous rental music provider – or vice versa.
Mike Schilli
Mike Schilli works as a software engineer with Yahoo! in Sunnyvale, California. He can be contacted at mailto:mschilli@perlmeister.com. Mike's homepage can be found at http://perlmeister.com.
Infos
- "The internet will suck all creative content out of the world" by David Byrne, The Guardian, 2013: http://www.theguardian.com/music/2013/oct/11/david-byrne-internet-content-world
- "Netflix to expand to Germany, France and Switzerland" by Leo Kelion: http://www.bbc.com/news/technology-27496055
- Spotify: https://www.spotify.com/us/
- Libspotify-SDK: https://developer.spotify.com/technologies/libspotify/
- Listings for this article: ftp://ftp.linux-magazin.com/pub/listings/magazine/167
- Registering applications: https://developer.spotify.com/my-applications/#!
- Spotify Web API Authorization Guide: https://developer.spotify.com/web-api/authorization-guide/
- "Security cam blog posts on Tumblr" by Michael Schilli, Linux Magazine, Issue 158, 2014: http://www.linux-magazine.com/Issues/2014/158/Perl-Security-Snapshots
- "Scanning and storing books on Google Drive" by Michael Schilli, Linux Magazine, Issue 146, 2013: http://www.linux-magazine.com/Issues/2013/146/Perl-Google-Drive
- "Doing more with Dropbox" by Michael Schilli, Linux Magazine, Issue 129, 2011: http://www.linux-magazine.com/Issues/2011/129/Perl-Dropbox
« Previous 1 2
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.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.