import json import datetime as dt import sys import numpy as np from ytmusicapi import YTMusic ytmusic = YTMusic("config.json") # created a config file following the docs here: https://ytmusicapi.readthedocs.io/en/latest/setup.html # I tried the "interactive" authentication but wasn't having luck with it # Add the name of the playlist you want to shuffle here, or provide it as a command line argument if sys.argv[1]: to_shuffle = str(sys.argv[1]) else: to_shuffle = 'playlist name here' def get_playlistID(name): playlistID = ytmusic.get_library_playlists(limit=100) for i in playlistID: if i['title'] == name: return i['playlistId'] # back up the playlist, just in case. bkup_title = to_shuffle + ' autobackup' if get_playlistID(bkup_title): ytmusic.delete_playlist(get_playlistID(bkup_title)) backup = {} backup['source_playlist'] = get_playlistID(to_shuffle) backup['title'] = bkup_title now = dt.datetime.now() now = now.strftime("%Y-%m-%d %H:%M") backup['description'] = 'Created ' + now ytmusic.create_playlist(**backup) playlist = ytmusic.get_playlist(get_playlistID(to_shuffle), limit=1000) #get the songs in the playlist track_ids = [] for i in playlist['tracks']: track_ids.append(i['videoId']) np.random.shuffle(track_ids) privacy = playlist['privacy'] ytmusic.delete_playlist(get_playlistID(to_shuffle)) shuffled = {} shuffled['title'] = to_shuffle now = dt.datetime.now() now = now.strftime("%Y-%m-%d %H:%M") shuffled['description'] = 'Shuffled ' + now shuffled['video_ids'] = track_ids shuffled['privacy_status'] = privacy ytmusic.create_playlist(**shuffled) print(f'The playlist called \"{to_shuffle}\" has been shuffled!')