This repository was archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 263
Spotify playlist generator #140
Merged
mergify
merged 7 commits into
Python-World:master
from
sandyjswl:spotify-playlist-generator
Oct 26, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f4d913e
Add Spotify Playlist Generator script
sandyjswl d58e2f1
Add Spotify Playlist Generator scripts
sandyjswl 366692e
Added comments
sandyjswl 5ccad2b
PR Changes
sandyjswl 1d10ebb
Add deepsource
sandyjswl 742441c
Edit deep source
sandyjswl 1d6d275
Delete .deepsource.toml
AdityaJ7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
Scripts/Miscellaneous/Spotify_Playlist_Generator/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]` | ||
|
||
### Screenshot/GIF showing the sample use of the script | ||
<!--Remove the below lines and add yours --> | ||
 | ||
|
||
## *Author Name* | ||
<!--Remove the below lines and add yours --> | ||
Sandeep Jaiswal |
7 changes: 7 additions & 0 deletions
7
Scripts/Miscellaneous/Spotify_Playlist_Generator/requirements.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Binary file added
BIN
+125 KB
Scripts/Miscellaneous/Spotify_Playlist_Generator/screenshots/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions
90
Scripts/Miscellaneous/Spotify_Playlist_Generator/spotify_playlist_generator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.