Generate Spotify Code Playlist Mac

TL;DR: If you have a premium subscription to Spotify, you can develop third-party Spotify applications with Python, libspotify, and the pyspotify bindings. Install pyspotify 2.x from GitHub (documentation is here), the one in PyPi is stale. I created a tool that takes a CSV file of Artist,Title entries and creates a playlist in Spotify. You can find the source code on GitHub.

What is Spotify Embed Code. The option of 'Copy embed code' means to take a piece of the HTML code and then put it on their website or a blog post. Users are required to copy the Spotify embed code if they are trying to embed a playlist, video, or even another third-party widget right into their website.

I'm a big fan of Spotify. I use Spotify at home, at work, in the gym, in the car, at coffee shops, on airplanes, and anywhere else you can imagine listening to music. I pay $9.99/month (at time of writing) for the premium subscription which allows you to sync playlists and stream music, commercial-free, to any device. Another perk of the premium subscription is ability to use libspotify to create third-party Spotify applications.

I recently wanted to create a playlist in Spotify from a CSV file consisting of about 250 Artist,Title entries. Unfortunately, there is no 'Import from CSV File' menu item in the Spotify application...I checked. This means I had to either (a) create a playlist, search for each song one-by-one, find the best match, and add it to the playlist or (b) get creative and write a script to do it for me. I knew this could be accomplished with a small program and using libspotify could make it possible with minimal effort. In the interest of getting it done quickly, I opted to use Python. A quick search for Python-to- libspotify bindings turned up pyspotify.

After some experimenting, I managed to piece together a 'working' prototype. I won't show you that code; it crashed occasionally, everything was hard-coded, and it was very much a 'one off' script...but it did the job. Briefly, here's how it works: first a session is created and the user is logged in. For each Artist,Title entry in the input CSV file, the Spotify song database is queried. Each query will return zero or more matching tracks. The track that best matches (if any) is added to a list. Finally, a playlist is created and the matching tracks are added to the playlist.

For the prototype, I used the version of pyspotify available in PyPi, version 1.11. A majority of this version was written in C and hence must be tediously maintained to work correctly with each version of libspotify on each platform. Version 2.x of pyspotify is written completely in Python, and uses CFFI, the Foreign Function Interface for Python calling C code. Given access to C source code that defines the data structures and functions of a library, CFFI can automatically generate Python bindings. This reduces the likelihood of programming mistakes, and allows the developers of a binding to focus on making it more Pythonic. Another benefit of using CFFI is that if a feature becomes available in the library it will be immediately available to a programmer in Python, albeit without a Pythonic interface. pyspotify 2.x is being actively maintained and has implemented Pythonic support for a majority of the libspotify interfaces (of course all the underlying libspotify API are available), and has some great documentation, but unfortunately lacks quality examples. You can find pyspotify version 2.x on GitHub.

I decided to re-write my tool using pyspotify version 2, making it more robust. Hopefully it can be a helpful example to others who want to do something with Spotify in Python using pyspotify. You can find the source code here.

I developed the tool on Mac OS X 10.9 (Mavericks) using the factory-installed Python 2.7.5. It should work out-of-the-box on other platforms, but I haven't tested it.

How to Run the Tool on Mac OS X 10.9

Spotify playlist creator

Install libspotify

libspotify is freely available from the Spotify developer website for a number of platforms. For OS X, it's most conveniently installed using the Homebrew package manager.

Spotify Playlist Maker

$ brew install libspotify

Generate Spotify Code

Install pyspotify

Playlist

Spotify Playlist Codes

The version of pyspotify on PyPi is 1.11, so unfortunately pip is not going to help us here. We must download the package directly and install it.

Tip: For those familiar with Virtual Python Environments, now would be a good time to create one. I may cover this later but for now I'll keep it simple. For those unfamiliar, don't worry, it isn't required (but do read up on it).

$ cd ~$ git clone https://github.com/mopidy/pyspotify.git$ cd pyspotify$ python setup.py install

Download the Project

$ git clone https://github.com/mborgerson/spotify-playlist-from-csv.git$ cd spotify-playlist-from-csv

Download Your libspotify API Key

Download your API key in binary format and save it as spotify_appkey.key place it in the spotify-playlist- from-csv directory.

Run the Tool

You can see the program help by passing the --help argument.

```$ python main.py -husage: main.py [-h] [-p] file name

Builds a playlist in Spotify by searching for tracks from a CSV file withArtist, Song entries.

positional arguments: file Input CSV file name Name of the playlist to create

Generate Spotify Code Playlist Mac 10

optional arguments: -h, --help show this help message and exit -p Re-login using the previous credentials```