Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 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)
Copy link
Member

Choose a reason for hiding this comment

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

remove /

Suggested change
![alt text](/screenshots/screenshot.png)
![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,63 @@
import argparse
import os
import random

import spotipy
from spotipy.oauth2 import SpotifyOAuth

scope = "playlist-modify-public"
spotipy_instance = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, cache_path=os.getcwd()))


def get_args():
parser = argparse.ArgumentParser(description='Split a playlist into multiple playlists')
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()


def get_track_ids_for_playlist(playlist):
res = []
for song in playlist:
res.append(song['track']['id'])
return res


def generate_playlists(playlist_size, playlist_songs, user_id):
smaller_playlists = [playlist_songs[x:x + playlist_size] for x in range(0, len(playlist_songs), playlist_size)]
Copy link
Member

Choose a reason for hiding this comment

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

Limit all lines to a maximum of 79 characters.

Suggested change
smaller_playlists = [playlist_songs[x:x + playlist_size] for x in range(0, len(playlist_songs), playlist_size)]
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):
track_ids = get_track_ids_for_playlist(playlist)
created_playlist = spotipy_instance.user_playlist_create(user_id, "generated_playlist_" + str(index + 1))
spotipy_instance.playlist_add_items(created_playlist['id'], track_ids)
print("Generated Playlist", str(index + 1), " of size", (playlist_size))


def main():
args = get_args()

playlist_size = int(args.limit)
playlist_id = args.playlist_id

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

playlist: dict = spotipy_instance.playlist_items(playlist_id)

playlist_songs: list = playlist['items']

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)))

random.shuffle(playlist_songs)

generate_playlists(playlist_size, playlist_songs, user_id)


if __name__ == '__main__':
main()