-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
111 lines (92 loc) · 3.31 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import sys
import json
import spotipy
import requests
import datetime
import configparser
import spotipy.util as util
config = configparser.ConfigParser()
config.read('config.ini')
SCOPE = 'playlist-read-private playlist-modify-private'
CLIENT_ID = "a27eac5f72414c3190b565abd15eb2f1"
CLIENT_SECRET = config["creds"]["CLIENT_SECRET"]
def kill():
input('Press enter to exit.')
sys.exit(0)
if __name__ == '__main__':
try:
try:
number = sys.argv[1]
except:
number = int(input("Enter the number of the Daily Mix playlist which you want saved (1/2/3/4/5/6): "))
f = open("username.txt", "r+")
rf = f.read()
if rf == "":
try:
user_uri = input("Enter your spotify uri: ").split(':')
username = user_uri[2]
f.write(username)
except:
print('Invalid spotify uri')
kill()
else:
username = rf
try:
token = util.prompt_for_user_token(username, SCOPE, CLIENT_ID, CLIENT_SECRET, redirect_uri='https://google.com/')
except:
os.remove(f".cache-{username}")
token = util.prompt_for_user_token(username, SCOPE, CLIENT_ID, CLIENT_SECRET, redirect_uri='https://google.com/')
headers = {"Authorization": f"Bearer {token}"}
try:
res_playlists = requests.get(f'https://api.spotify.com/v1/users/{username}/playlists?limit=50', headers = headers)
except:
print('Invalid spotify uri')
kill()
if res_playlists.ok:
respl_json = res_playlists.json()
else:
print('Couldn\'t get the playlists')
kill()
mix_id = ''
mix_desc = ''
for i in respl_json["items"]:
if i["name"] == f"Daily Mix {number}":
mix_id = i["id"]
mix_desc = i["description"]
break
else:
print(f"Could not find Daily Mix {number} in followed playlists")
kill()
res_tracks = requests.get(f"https://api.spotify.com/v1/playlists/{mix_id}/tracks", headers = headers)
if res_tracks.ok:
restr_json = res_tracks.json()
else:
print('Couldn\'t get the tracks')
try:
uris = [i["track"]["uri"] for i in restr_json["items"]]
except:
print('Couldn\'t find any tracks')
kill()
json = {
"name": f"Daily Mix {number} | {datetime.date.today()}",
"public": False,
"description": mix_desc
}
res_newpl = requests.post(f"https://api.spotify.com/v1/users/{username}/playlists", headers = headers, json = json)
if res_newpl.ok:
resnp_json = res_newpl.json()
else:
print('Couldn\'t create a new playlist')
newpl_id = resnp_json["id"]
new_songs = requests.post(f"https://api.spotify.com/v1/playlists/{newpl_id}/tracks", headers = headers, json = {"uris": uris})
if new_songs.ok:
print("Successful!")
else:
print("Something went wrong")
input('Press enter to exit.')
except SystemExit:
sys.exit(0)
except:
print('Something went wrong')
input('Press enter to exit')