This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ElectricShuffle.py
80 lines (63 loc) · 2.29 KB
/
ElectricShuffle.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
73
74
75
76
77
78
79
80
#!/bin/env python3
import datetime
import json
import os
from os import path
import random
from select import select
import tidalapi
dir = path.dirname(path.realpath(__file__))
credential_file = path.join(dir, '.credentials.json')
need_oauth = True
session = tidalapi.Session()
# Check if the credentials are still valid
if os.path.exists(credential_file):
with open(credential_file, "r") as open_file:
token_json = json.load(open_file)
# Check tokens with tidal
session.load_oauth_session(
token_json["token_type"],
token_json["access_token"],
token_json["refresh_token"],
datetime.datetime.fromisoformat(token_json["expiry_time"])
)
if not session.check_login():
# Have user go log in and when this completes, we will auto continue
session.login_oauth_simple()
session_dict = {
"token_type": session.token_type,
"access_token": session.access_token,
"refresh_token": session.refresh_token,
"expiry_time": session.expiry_time.isoformat()
}
with open(credential_file, "w") as out_file:
out_file.write(json.dumps(session_dict))
### Now that session is handled, get playlists
playlists = session.user.playlists()
print("Please select the playlist you wish to shuffle by intering it's number below")
for idx, playlist in enumerate(playlists):
print(f" [{idx+1}] {playlist.name}")
# Get playlists on user account
playlist_num = int(input("Playlist Number: ")) - 1
selected_playlist = playlists[playlist_num]
print(f"Electric shuffle {selected_playlist.name}?")
should_continue = bool(input("Continue (Yes/No): "))
### Allow bailing
if not should_continue:
exit(1)
### Do the thing
tracklist = selected_playlist.tracks()
track_ids = []
for track in tracklist:
track_ids.append(track.id)
### Signify that this is a copy for the user
shuffled_name = f"{selected_playlist.name} - Electrified"
for playlist in playlists:
### Delete an existing version of this playlist if we are re-rolling
if playlist.name is shuffled_name:
playlist.delete()
## Create (or re create) the electric playlist
new_playlist = session.user.create_playlist(shuffled_name, selected_playlist.description)
## Add the beautiful music after a quick shuffle
random.shuffle(track_ids)
new_playlist.add(track_ids)