Update Readme, add usability

This commit is contained in:
David Daily 2022-05-23 15:21:20 -05:00
parent b0ff92e541
commit 0140f43010
2 changed files with 45 additions and 29 deletions

View File

@ -1,3 +1,16 @@
# YTM-Shuffler # YTM-Shuffler
Shuffles a playlist because google can't seem to be bothered to do it 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.

View File

@ -7,48 +7,51 @@ ytmusic = YTMusic("config.json")
# created a config file following the docs here: https://ytmusicapi.readthedocs.io/en/latest/setup.html # 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 # 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): def get_playlistID(name):
playlistID = ytmusic.get_library_playlists(limit=100) playlistID = ytmusic.get_library_playlists(limit=100)
for i in playlistID: for i in playlistID:
if i['title'] == name: if i['title'] == name:
return i['playlistId'] return i['playlistId']
# back up the playlist, just in case. if sys.argv[1]:
to_shuffle = str(sys.argv[1])
to_shuffleID = get_playlistID(to_shuffle)
else:
print("Usage: shuffler.py playlistname")
sys.exit()
try:
backup = sys.argv[2]
# back up the playlist if requested
bkup_title = to_shuffle + ' autobackup' bkup_title = to_shuffle + ' autobackup'
if get_playlistID(bkup_title): if get_playlistID(bkup_title):
ytmusic.delete_playlist(get_playlistID(bkup_title)) ytmusic.delete_playlist(get_playlistID(bkup_title))
backup = {} backup = {}
backup['source_playlist'] = get_playlistID(to_shuffle) backup['source_playlist'] = to_shuffleID
backup['title'] = bkup_title backup['title'] = bkup_title
now = dt.datetime.now() now = dt.datetime.now()
now = now.strftime("%Y-%m-%d %H:%M") now = now.strftime("%Y-%m-%d %H:%M")
backup['description'] = 'Created ' + now backup['description'] = 'Created ' + now
ytmusic.create_playlist(**backup) ytmusic.create_playlist(**backup)
print(f'{bkup_title} created')
except:
track_ids = [] # Python needs SOMETHING here, whatever.
playlist = ytmusic.get_playlist(get_playlistID(to_shuffle), limit=1000) #get the songs in the playlist playlist = ytmusic.get_playlist(to_shuffleID, limit=1000) #get the songs in the playlist
track_ids = [] track_ids = []
for i in playlist['tracks']: for i in playlist['tracks']:
track_ids.append(i['videoId']) track_ids.append(i['videoId'])
np.random.shuffle(track_ids) 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 = dt.datetime.now()
now = now.strftime("%Y-%m-%d %H:%M") now = now.strftime("%Y-%m-%d %H:%M")
shuffled = {}
shuffled['playlistId'] = to_shuffleID
shuffled['description'] = 'Shuffled ' + now shuffled['description'] = 'Shuffled ' + now
shuffled['video_ids'] = track_ids ytmusic.edit_playlist(**shuffled)
shuffled['privacy_status'] = privacy
ytmusic.create_playlist(**shuffled)
print(f'The playlist called \"{to_shuffle}\" has been shuffled!') print(f'The playlist called \"{to_shuffle}\" has been shuffled!')