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