Coder Social home page Coder Social logo

yennanliu / spotify-playlist-generator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mileshenrichs/spotify-playlist-generator

0.0 2.0 0.0 659 KB

A scheduled Python script that scrapes new song titles and builds ๐Ÿ”ฅ playlists through the Spotify API

Python 100.00%

spotify-playlist-generator's Introduction

Shoutout to MyBridge for listing this project as one of the Top 10 Python Open Source Projects of June 2018!

Python Open Source

Spotify Playlist Generator

Spotify Playlist Generator is a Python script that automatically builds a new Spotify playlist each week with new music. This script was written for personal use, so the playlist creation is authorized by an access token retreived for my Spotify account. I used Windows Task Scheduler to regulate the script to run every morning at 10:00 AM on my computer.

How it works

  1. The most recently acquired Spotify access token, the script's Spotify refresh token, and a Base64-encoded string containing my "app's" client ID and secret are stored in a local SQLite table called tokens (I've excluded this database from the repository because these tokens provide read and write access to my Spotify account).
  2. When the script starts, it first checks the authority of its current access token. Since this token expires after one hour, a new one will usually have to be generated. The script uses its refresh token, which never changes, to acquire a new access token from Spotify's token service.
  3. Now that it has a valid access token, it proceeds to scrape the current top 100 songs from HotNewHipHop, located at this URL: https://www.hotnewhiphop.com/top100/ Top 100 list
  4. It parses through the page with the Beautiful Soup 4 library, building a list of "song candidates." A song qualifies as a candidate if its artist list contains at least one artist in a desiredArtists list defined at the beginning of the script (a static list of artists who I like). The binary search algorithm is used to check for matches.
  5. Then, for each song candidate, it ensures that the candidate is not a duplicate by querying the songs_added local db table for a song name and primary artist match.
  6. Songs that are not duplicates are searched through Spotify's /search endpoint. The Spotify song ids that are found (since these songs are new, many aren't on Spotify yet) are accumulated into a list.
  7. Now that the script has a list of song ids to add, it must determine whether to create a new playlist or add to the most recently created (or current) playlist. New playlist are created on Saturday. If it's a Saturday, the script uses the access token retrieved earlier to make a POST request to create a new playlist on my account, named New Songs {date}. Otherwise, it queries the local playlists_created table for the Spotify playlist id of the most recently created playlist.
  8. It then uses the playlist id and song ids to make a POST request to a Spotify endpoint that adds each song to the playlist, entering these songs into the songs_added local db table in the process.

Scheduling

This task is scheduled with Windows Task Scheduler: Spotify Task in Scheduler

Final Result

Generated Spotify Playlist

spotify-playlist-generator's People

Contributors

mileshenrichs avatar

Watchers

James Cloos avatar yennj12 avatar

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.