did the needful
This commit is contained in:
52
shuffler.py
Normal file
52
shuffler.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import json
|
||||
import datetime as dt
|
||||
import os
|
||||
from dpath import util
|
||||
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
|
||||
to_shuffle = 'Sleep'
|
||||
|
||||
|
||||
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!')
|
||||
#TODO and UI: auth and playlist name
|
Reference in New Issue
Block a user