-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore-backup.py
72 lines (53 loc) · 2.13 KB
/
restore-backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from ytmusicapi import YTMusic
import json
import os.path
# Uncomment next line to auth as per https://ytmusicapi.readthedocs.io/en/latest/usage.html#authenticated
# YTMusic.setup(filepath="headers_auth.json")
ytmusic = YTMusic('headers_auth.json')
with open('./playlist.txt', 'r') as playlistFile:
playlistId = playlistFile.read()
dataFilePath = "./" + playlistId + ".json"
columnStr = input(
"Which column to sort on? 1: Title (default) | 2: Album | 3: Artist. \n")
if (columnStr == "1"):
column = 'title'
elif (columnStr == "2"):
column = 'albumName'
elif (columnStr == "3"):
column = 'artist'
else:
column = 'title'
ascStr = input(
"Ascending or Descending? 1: Ascending (default) | 2: Descending\n")
isDec = ascStr == '2'
playlist = ytmusic.get_playlist(playlistId, limit=None)
# This is to make it easier in development, don't want to load my huge playlist every time
# if os.path.exists(dataFilePath):
# with open(dataFilePath, 'r') as openfile:
# playlist = json.load(openfile)
# else:
# playlist = ytmusic.get_playlist(playlistId, limit=None)
# playlistObj = json.dumps(playlist, indent=4)
# with open(dataFilePath, "w") as outfile:
# outfile.write(playlistObj)
for track in playlist["tracks"]:
track['artist'] = track['artists'][0]['name']
if track['album']:
track['albumName'] = track['album']['name']
else:
track['albumName'] = ''
sortedIds = []
sortedTracks = sorted(playlist["tracks"], key=lambda d: (
d[column] or '').lower(), reverse=isDec)
for track in sortedTracks:
sortedIds.append(track['videoId'])
backupName = 'backup-sort-' + playlistId + '.json'
with open(backupName, "w") as outfile:
outfile.write(json.dumps(sortedTracks, indent=4))
# with open('./backup.json', 'r') as openfile:
# sortedTracks = json.load(openfile)
confirm = input(
"The script will now remove all playlist items and add them again (sorted). I've made a backup "+backupName+".\nAre you ready? Y/N \n")
if confirm.lower() == 'y':
ytmusic.remove_playlist_items(playlistId, videos=sortedTracks)
# ytmusic.add_playlist_items(playlistId, videoIds=sortedIds)