Code Monkey home page Code Monkey logo

export-spotify-playlists's Introduction

Export Spotify Playlists

Export Spotify playlist data to either Text or CSV files using Spotipy and the WebAPI.

Implementation Details

The application is written almost completely in python and I have used the recommended python library for the Spotify WebAPI - Spotipy. The library makes working with the WebAPI easier than if you used the python requests library directly. You can either run spotify_exporter.py directly or you can run app.py, open http://localhost:5000 in your browser and follow along with the web app. The Flask app uses spotify_exporter.py and lets you login to your spotify account to export your playlists from the web app. Output can be written either to a text (.txt) file or a CSV (.csv) file.

Spotify uses URIs as unique identifiers for almost everything. For example, every Spotify user has their unique URI, every Spotify playlist has its unique URI, as does every artist, album and so on. spotify_exporter.py requires the playlist URI, while the Flask app can extract the URI from the playlist's URL. Further, for you to be able to use the WebAPI you must get yourself registered with Spotify so that you may be authorized. This is good because nobody can just access the database. The registration process is fairly straightforward. You can find more information at Spotify for Developers > WebAPI. If you use the Flask app, you still need to register using the link.

Background

The premise of the program is pretty obvious. This application lets you save your playlists outside of Spotify in text or CSV format. The text files can be used to maintain a readable database of your playlists in the event that you cannot access Spotify or somehow lose data. The CSV files on the other hand can be used to do other analysis or can be plugged into another script to create identical playlists accross streaming services. I have sneaked a bonus, "Random Song Selecter" into the script as well.

Getting around the file

Libraries used

I have used the following libraries for various purposes in the script :

  • Spotipy - Install using pip install spotipy
  • Pprint - Install using pip install pprint
  • CSV module
  • OS module
  • Random module

The Flask app uses the following libraries :

  • Flask
  • String module
  • Secrets module
  • Requests module
  • Urllib module

Control Variables

The control variables in spotify_exporter.py are the variables you must change to select what you want the script to do. The Flask app requires the same information. The information required is -

  • mode - You can select whether you want to export to csv, txt or none. None would just select a random song form the playlist.
  • OVERWRITE - This is a boolean variable to choose whether you want to overwrite existing files or not.
  • playlist - Used to select which playlist you want to use in the script.
  • CLI_ID and CLI_KEY - They store your client ID and client secret that you can obtain after you have registered with Spotify.

Every function has a description in comments and explains what it does. You can select the show_ds mode of the script if you want to better understand what's going on in the write_txt() and write_csv() functions.

Export Format

TXT Files

  • Track URL
  • Track name
  • Artist name(s)
  • Total number of songs
  • Owner's username

CSV Files

  • Track URL
  • Track name
  • Artist name(s)
  • Explicit
  • Popularity
  • Duration in ms

Screenshots

The txt file

The web page - 1

The web page - 2

Notes

You will have to add your own client details and your own paths. If you use spotify_exporter.py directly, update this information in the main() function of the script. If you use the Flask web app, update this information in app.py. Everything else can be left as is.

export-spotify-playlists's People

Contributors

shivsondhi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

export-spotify-playlists's Issues

Keep receiving "TypeError: 'NoneType' object is not subscriptable" on CSV writer

Hello, I'm new to github so I'm not sure if this is the correct spot for this, but I'm having an issue trying to write some data to a CSV.

I keep receiving "TypeError: 'NoneType' object is not subscriptable."

Here is the full error I am getting.

Traceback (most recent call last):
File "spoti_api.py", line 199, in
main()
File "spoti_api.py", line 53, in main
write_playlist(playlist[1], playlist[0], mode)
File "spoti_api.py", line 81, in write_playlist
old_total = write_csv(filename, tracks)
File "spoti_api.py", line 177, in write_csv
track_url = track['external_urls']['spotify']
TypeError: 'NoneType' object is not subscriptable

I entered my valid CLI_ID & CLI_KEY and set the mode to CSV, as well as entered the filepath correctly for the output. I apologize I am pretty new to python, and have never worked with an API before so I am fairly clueless about what I'm doing here.

Am I missing something? Thanks.

INVALID_CLIENT: Invalid redirect URI

I'm able to use the exporter to make txt files by with $ run spotify_exporter.py however, I'm trying to format things such that the trick is links in markdown and I think that would be easier to manage with .csv maybe? I am able to get app.py to work, however when I click to connect, sometimes I get INVALID_CLIENT: Invalid redirect URI but sometimes I am able to sign in, and then after I sign into spotify it throws INVALID_CLIENT: Invalid redirect URI.

My final goal is to get all of my wrapped playlists into .md tables for my Obsidian vault. I looked at the exporter code and can't see how I'd modify it to format it how I need it. Any help in this endeavor would be greatly appreciated :)

example table in Obsidian

Artist | Track 
 :- | :- 
Artist | [Track](https://www.spotify.com)
Artist Track
Artist Track

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.