diff --git a/README.md b/README.md index 91f78a9..edf896c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,16 @@ # YTM-Shuffler -Shuffles a playlist because google can't seem to be bothered to do it \ No newline at end of file +Shuffles a playlist because google can't seem to be bothered to do it + +--- + +# Usage + +1. Clone this repo `git clone https://daviddaily.dev/david/YTM-Shuffler.git && cd YTM-Shuffler` +2. Run `pipenv install` +3. Follow the documentation from the [API docs](https://ytmusicapi.readthedocs.io/en/latest/setup.html#manual-file-creation) to create the `config.json` +4. Run `pipenv shell` +5. Use `python shuffler.py ` followed by the name of the playlist you'd like to be shuffled, in quotes if its more than one word. +6. If you'd like to create an automagic backup, pass a second argument like `python shuffler.py "shuffle this" yes` + +Currently its limited to 1000 songs, but I feel like that's enough for most people. \ No newline at end of file diff --git a/shuffler.py b/shuffler.py index 4ae4673..21bc653 100644 --- a/shuffler.py +++ b/shuffler.py @@ -7,48 +7,51 @@ 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) +if sys.argv[1]: + to_shuffle = str(sys.argv[1]) + to_shuffleID = get_playlistID(to_shuffle) +else: + print("Usage: shuffler.py playlistname") + sys.exit() -playlist = ytmusic.get_playlist(get_playlistID(to_shuffle), limit=1000) #get the songs in the playlist +try: + backup = sys.argv[2] + # back up the playlist if requested + bkup_title = to_shuffle + ' autobackup' + if get_playlistID(bkup_title): + ytmusic.delete_playlist(get_playlistID(bkup_title)) + backup = {} + backup['source_playlist'] = to_shuffleID + backup['title'] = bkup_title + now = dt.datetime.now() + now = now.strftime("%Y-%m-%d %H:%M") + backup['description'] = 'Created ' + now + ytmusic.create_playlist(**backup) + print(f'{bkup_title} created') +except: + track_ids = [] # Python needs SOMETHING here, whatever. + +playlist = ytmusic.get_playlist(to_shuffleID, 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)) +ytmusic.remove_playlist_items(to_shuffleID, playlist['tracks']) +ytmusic.add_playlist_items(to_shuffleID, track_ids) -shuffled = {} -shuffled['title'] = to_shuffle now = dt.datetime.now() now = now.strftime("%Y-%m-%d %H:%M") +shuffled = {} +shuffled['playlistId'] = to_shuffleID shuffled['description'] = 'Shuffled ' + now -shuffled['video_ids'] = track_ids -shuffled['privacy_status'] = privacy -ytmusic.create_playlist(**shuffled) +ytmusic.edit_playlist(**shuffled) + print(f'The playlist called \"{to_shuffle}\" has been shuffled!') \ No newline at end of file