Web Dev

Interactive REST hacking with Sublime Text and RESTer

Do you ever miss a command line to a web application? If you’re lucky, you may use a REST API with appropriate tools for the same job.

This Christmas, I bought a Sonos system for our family. It’s a wireless speaker system that uses music streaming services. It supports both Spotify and Deezer, but works better with Deezer and it came with a one year free subscription, so Deezer it is.

First order of business: Importing Spotify playlists into Deezer. I found a free service that does this, but by accidentally refreshing the browser a couple of times, I managed to import five copies of each playlist. Disaster!

Thus began my quest: I had a number of accidentally imported playlists in Deezer, and it turns out that after deleting a playlist, I needed about five clicks, including hitting a small icon, in order to delete the next. This was not going to be fun.

As a programmer, you know there must be a better way. Perhaps that better way requires more time because you have to program something, but dammit, better is better. In the case of Deezer, I found that they had a well-documented REST API. But this time, I didn’t feel like writing a program just to solve this one problem. And so we get to the core of this post: How can you just execute a fairly large number of API calls without writing a program for it? All I needed to do was to find out which set of API calls needed to be made based on my playlist listing and execute these.

My text editor of choice these days is Sublime Text, which has a number of plugins. The RESTer plugin was just what I needed.

So, in order to delete the extra playlists:

  1. I installed SublimeText 3, Package Manager and RESTer
  2. Looking at the Deezer API doc, I created a test application and entered the URL: https://connect.deezer.com/oauth/auth.php?app_id=YOUR_APP_ID&redirect_uri=http://localhost& perms=basic_access,email,delete_library&response_type=token in a web browser. This gave me an authorization dialog with Deezer that ends up redirecting to an invalid URL on localhost. However, the URL will be something like http://localhost/#access_token=xxxxxxxxxxxxxxxxxx&expires=3600. Just copy the access_token part, as this will be needed for later.
  3. Now, let’s list the playlists. In Sublime Text, I write the URL http://api.deezer.com/user/2529/playlists?access_token=xxxxx (replace the id with your own user id and the access_token with the result from last step) and press ctrl-alt-r. This executes the REST call and returns the result in a new buffer as a formatted JSON text.
  4. In Sublime Text, I bring up the search dialog with ctrl-f, click the “.*” button (regular expression), enter "title"|"picture" and press “Find All”. This selects all the relevant lines from the JSON response.
  5. Now, some Sublime Text editing magic. All the lines from the output are selected. Press shift-end to extend the selections to the end of their lines, ctrl-c to copy the texts to the clipboard, ctrl-a to select everything, del to delete and ctrl-v to paste it.
  6. Whoa! That should replace the text with a set of alternating lines like "title": "something" and "picture": "something". We almost have what we want. Double click on the word "title" on any line and press alt-f3. This selects all the places where it says "title" in the buffer. Press end to move to the end of the lines and del to join the line with the next.
  7. Now each playlist should be on a separate line with the title and the picture link. Press F9 to sort the lines. In my example, I can now clearly see all the lines the are duplicates. So I delete the lines I want to keep and keep the lines I want to delete. For all playlists with duplicates, I remove one line in my buffer and keep the rest for deleting.
  8. Now I just have to craft the delete commands. I double click an example of the word picture. I press right-arrow until the cursors (several!) are between the ” and the https-part. Press shift-home to select everything before https://api... and del press to delete this. Press end to go to the end of the line and backspace until you have deleted the ‘/image”,’ part so each URL looks like “https://https://api.deezer.com/playlist/number“.
  9. We now have our targets to delete. At this point in time, I went to the beginning of the buffer and pressed ctrl-alt-down arrow until I have a cursor at the start of all lines. I now type “DELETE “, press end, paste in the access_token from step 2 and press enter. I now have all the requests that I need to execute separated by a blank line!
  10. Finally, I go to the first line, press ctrl-alt-r which executes the HTTP request. I press ctrl-w to close the response, go down two lines and press ctrl-alt-r to do it again.

This is the result of the first request:

{
  "data": [
    {
      "id": 160504851,
      "title": "- SKI -",
      "duration": 3128,
      "public": true,
      "is_loved_track": false,
      "collaborative": false,
      "rating": 0,
      "nb_tracks": 12,
      "link": "http://www.deezer.com/playlist/160504851",
      "picture": "https://api.deezer.com/playlist/160504851/image",
      "checksum": "541c0f5944b0081beca989e15b0b16dd",
      "tracklist": "https://api.deezer.com/playlist/160504851/tracks",
      "creator": {
        "id": 2529,
        "name": "dadbond",
        "tracklist": "https://api.deezer.com/user/2529/flow",
        "type": "user"
      },
      "type": "playlist"
    },
    {
      "id": 609456965,
      "title": "-- OLD Favourites --",
.... 

This is what my buffer looks like after selecting titles and pictures and joining the lines:

"title": "01 - From radio","picture": "https://api.deezer.com/playlist/29070708/image",
"title": "01 - From radio","picture": "https://api.deezer.com/playlist/52354392/image",
"title": "01 - From radio","picture": "https://api.deezer.com/playlist/60934426/image",
"title": "01 - From radio","picture": "https://api.deezer.com/playlist/757807/image",
"title": "Alex Croiseaux - TOP TRACK","picture": "https://api.deezer.com/playlist/984435191/image",
"title": "All Good","picture": "https://api.deezer.com/playlist/38559351/image",
"title": "All Good","picture": "https://api.deezer.com/playlist/4341978/image",
"title": "All Good","picture": "https://api.deezer.com/playlist/51724984/image",
"title": "All Good","picture": "https://api.deezer.com/playlist/7068135/image",
"title": "Annif 2014","picture": "https://api.deezer.com/playlist/799155301/image",
"title": "Best Playlist EVER !!","picture": "https://api.deezer.com/playlist/30595446/image",
"title": "Best Playlist EVER !!","picture": "https://api.deezer.com/playlist/374755225/image",
"title": "Best Playlist EVER !!","picture": "https://api.deezer.com/playlist/55027024/image",
"title": "BOULOT","picture": "https://api.deezer.com/playlist/33136192/image",
"title": "daft punk - get Lucky.mp3","picture": "https://api.deezer.com/playlist/791327901/image", 

This is what my buffer looked like in the end.

DELETE https://api.deezer.com/playlist/52354392?access_token=abcde

DELETE https://api.deezer.com/playlist/60934426?access_token=abcde

DELETE https://api.deezer.com/playlist/757807?access_token=abcde

DELETE https://api.deezer.com/playlist/4341978?access_token=abcde

DELETE https://api.deezer.com/playlist/51724984?access_token=abcde

DELETE https://api.deezer.com/playlist/7068135?access_token=abcde

DELETE https://api.deezer.com/playlist/374755225?access_token=abcde

DELETE https://api.deezer.com/playlist/55027024?access_token=abcde 

Sometimes, doing an everyday Christmas task like deleting a lot of playlists is hard to do with the programs available. Luckily, with a little bit of programming knowledge and some sharp tools, you can get the job done through an API.

Johannes Brodwall

Johannes is the chief scientist of the software offshore company Exilesoft. He's got close to 15 years programming Java, C# and a long time ago other languages as well. He believes that programming is about more than just writing the code, but that too many people lose touch with the coding as well. He has been organizing software development activities in Oslo for many years. In addition, he often speaks at conferences all over Europe.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button