-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (34 loc) · 1.54 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
import json
import requests
from secrets import spotify_user_id,playlist_id
from refresh import Refresh
class SpotifyHistory:
def __init__(self):
self.spotify_user_id = spotify_user_id
self.playlist_id = playlist_id
self.tracks = ""
self.spotify_access_token = ""
def find_songs(self):
print("Finding recently played...")
query = "https://api.spotify.com/v1/me/player/recently-played"
response = requests.get(query,headers={"Content-Type":"application/json","Authorization":"Bearer {}".format(self.spotify_access_token)},params={"limit":50})
response_json = response.json()
for i in response_json["items"]:
self.tracks += i["track"]["uri"] + ","
print(i["track"]["name"])
self.tracks = self.tracks[:-1]
self.replace_songs()
# print(i["track"]["name"])
def replace_songs(self):
print("Replacing songs in playlist...")
query = "https://api.spotify.com/v1/playlists/{}/tracks".format(self.playlist_id)
response = requests.put(query,headers={"Content-Type":"application/json","Authorization":"Bearer {}".format(self.spotify_access_token)},params={"uris":self.tracks})
print(response)
print("Success!")
def start(self):
print("Refreshing token....")
refreshCaller = Refresh()
self.spotify_access_token = refreshCaller.refresh()
self.find_songs()
a = SpotifyHistory()
a.start()