Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Spotify playlist generator #140

Merged
merged 7 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Scripts/Miscellaneous/Spotify_Playlist_Generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Script Title
<!--Remove the below lines and add yours -->
Spotify Playlist Generator

### Prerequisites
<!--Remove the below lines and add yours -->
1. Spotipy
` pip install spotipy --upgrade `

### How to run the script
<!--Remove the below lines and add yours -->
1. [Create Spotify Developers Account and create a new app](https://developer.spotify.com/)
2. Export Client Id, Client Secret and Redirect URI
* `export SPOTIPY_CLIENT_ID='your-spotify-client-id'`
* `export SPOTIPY_CLIENT_SECRET='your-spotify-client-secret'`
* `export SPOTIPY_REDIRECT_URI='your-app-redirect-url'`
3. `python main.py -p [playlist_id] -l [size_of_each_playlist]`
Comment on lines +12 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Can you add a test id and playlist? For viewers to test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! I don't have a test account on Spotify, i tested these on my personal account.


### Screenshot/GIF showing the sample use of the script
<!--Remove the below lines and add yours -->
![alt text](screenshots/screenshot.png)

## *Author Name*
<!--Remove the below lines and add yours -->
Sandeep Jaiswal
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
certifi==2020.6.20
chardet==3.0.4
idna==2.10
requests==2.24.0
six==1.15.0
spotipy==2.14.0
urllib3==1.25.10
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import argparse
import os
import random

import spotipy
from spotipy.oauth2 import SpotifyOAuth

scope = "playlist-modify-public"

# Create an instance of spotify library
spotipy_instance = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, cache_path=os.getcwd()))


#
def get_args():
"""
Method to read the command line arguments and parse them
"""

parser = argparse.ArgumentParser(description='Split a playlist into multiple playlists')
# Required arguments for the program
parser.add_argument('-p', '--playlist_id', required=True,
help='Playlist ID')
parser.add_argument('-l', '--limit', required=True, default=20,
help='Size of each small playlist')
return parser.parse_args()


# This method returns all the Spotify Song IDs from a given playlist
def get_track_ids_for_playlist(playlist):
res = []
for song in playlist:
res.append(song['track']['id']) #Extract the ID of the track
return res



def generate_playlists(playlist_size, playlist_songs, user_id):
"""
This method generates smaller playlists from the input playlist
"""

# Create the smaller playlists from the given large playlist
smaller_playlists = [playlist_songs[x:x + playlist_size]
for x in range(0, len(playlist_songs), playlist_size)]
for index, playlist in enumerate(smaller_playlists):
# Once we have the smaller playlists we need to create them on the account
# For that we need to extract IDS of the songs in the smaller playlists
track_ids = get_track_ids_for_playlist(playlist)
# Create the smaller playllist
created_playlist = spotipy_instance.user_playlist_create(user_id, "generated_playlist_" + str(index + 1))
# Add songs to the playlist and publish them
spotipy_instance.playlist_add_items(created_playlist['id'], track_ids)
print("Generated Playlist", str(index + 1), " of size", (playlist_size))


def main():
# Get the command line arguments
args = get_args()

# Extract playlist size from command line arguments
playlist_size = int(args.limit)
playlist_id = args.playlist_id

print("Received Playlist ID :: ", playlist_id)

# Get the playlist from spotify using the playlist ID
playlist: dict = spotipy_instance.playlist_items(playlist_id)

# Extract only the songs from the playlist, ignore extra metadata
playlist_songs: list = playlist['items']

# Get the user_id of the user from the token
user_id = spotipy_instance.me()['id']

while playlist['next']:
playlist = spotipy_instance.next(playlist)
playlist_songs.extend(playlist['items'])

print("Total songs in the given playlist :: ", str(len(playlist_songs)))

# Shuffle the playlist
random.shuffle(playlist_songs)

# Now generate the smaller playlists
generate_playlists(playlist_size, playlist_songs, user_id)


if __name__ == '__main__':
main()