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

Commit 251d79f

Browse files
authored
Merge pull request #140 from sandyjswl/spotify-playlist-generator
Spotify playlist generator
2 parents 872c805 + 1d6d275 commit 251d79f

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Script Title
2+
<!--Remove the below lines and add yours -->
3+
Spotify Playlist Generator
4+
5+
### Prerequisites
6+
<!--Remove the below lines and add yours -->
7+
1. Spotipy
8+
` pip install spotipy --upgrade `
9+
10+
### How to run the script
11+
<!--Remove the below lines and add yours -->
12+
1. [Create Spotify Developers Account and create a new app](https://developer.spotify.com/)
13+
2. Export Client Id, Client Secret and Redirect URI
14+
* `export SPOTIPY_CLIENT_ID='your-spotify-client-id'`
15+
* `export SPOTIPY_CLIENT_SECRET='your-spotify-client-secret'`
16+
* `export SPOTIPY_REDIRECT_URI='your-app-redirect-url'`
17+
3. `python main.py -p [playlist_id] -l [size_of_each_playlist]`
18+
19+
### Screenshot/GIF showing the sample use of the script
20+
<!--Remove the below lines and add yours -->
21+
![alt text](screenshots/screenshot.png)
22+
23+
## *Author Name*
24+
<!--Remove the below lines and add yours -->
25+
Sandeep Jaiswal
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
certifi==2020.6.20
2+
chardet==3.0.4
3+
idna==2.10
4+
requests==2.24.0
5+
six==1.15.0
6+
spotipy==2.14.0
7+
urllib3==1.25.10
125 KB
Loading
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import argparse
2+
import os
3+
import random
4+
5+
import spotipy
6+
from spotipy.oauth2 import SpotifyOAuth
7+
8+
scope = "playlist-modify-public"
9+
10+
# Create an instance of spotify library
11+
spotipy_instance = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, cache_path=os.getcwd()))
12+
13+
14+
#
15+
def get_args():
16+
"""
17+
Method to read the command line arguments and parse them
18+
"""
19+
20+
parser = argparse.ArgumentParser(description='Split a playlist into multiple playlists')
21+
# Required arguments for the program
22+
parser.add_argument('-p', '--playlist_id', required=True,
23+
help='Playlist ID')
24+
parser.add_argument('-l', '--limit', required=True, default=20,
25+
help='Size of each small playlist')
26+
return parser.parse_args()
27+
28+
29+
# This method returns all the Spotify Song IDs from a given playlist
30+
def get_track_ids_for_playlist(playlist):
31+
res = []
32+
for song in playlist:
33+
res.append(song['track']['id']) #Extract the ID of the track
34+
return res
35+
36+
37+
38+
def generate_playlists(playlist_size, playlist_songs, user_id):
39+
"""
40+
This method generates smaller playlists from the input playlist
41+
"""
42+
43+
# Create the smaller playlists from the given large playlist
44+
smaller_playlists = [playlist_songs[x:x + playlist_size]
45+
for x in range(0, len(playlist_songs), playlist_size)]
46+
for index, playlist in enumerate(smaller_playlists):
47+
# Once we have the smaller playlists we need to create them on the account
48+
# For that we need to extract IDS of the songs in the smaller playlists
49+
track_ids = get_track_ids_for_playlist(playlist)
50+
# Create the smaller playllist
51+
created_playlist = spotipy_instance.user_playlist_create(user_id, "generated_playlist_" + str(index + 1))
52+
# Add songs to the playlist and publish them
53+
spotipy_instance.playlist_add_items(created_playlist['id'], track_ids)
54+
print("Generated Playlist", str(index + 1), " of size", (playlist_size))
55+
56+
57+
def main():
58+
# Get the command line arguments
59+
args = get_args()
60+
61+
# Extract playlist size from command line arguments
62+
playlist_size = int(args.limit)
63+
playlist_id = args.playlist_id
64+
65+
print("Received Playlist ID :: ", playlist_id)
66+
67+
# Get the playlist from spotify using the playlist ID
68+
playlist: dict = spotipy_instance.playlist_items(playlist_id)
69+
70+
# Extract only the songs from the playlist, ignore extra metadata
71+
playlist_songs: list = playlist['items']
72+
73+
# Get the user_id of the user from the token
74+
user_id = spotipy_instance.me()['id']
75+
76+
while playlist['next']:
77+
playlist = spotipy_instance.next(playlist)
78+
playlist_songs.extend(playlist['items'])
79+
80+
print("Total songs in the given playlist :: ", str(len(playlist_songs)))
81+
82+
# Shuffle the playlist
83+
random.shuffle(playlist_songs)
84+
85+
# Now generate the smaller playlists
86+
generate_playlists(playlist_size, playlist_songs, user_id)
87+
88+
89+
if __name__ == '__main__':
90+
main()

0 commit comments

Comments
 (0)