-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotifyClient.py
119 lines (93 loc) · 3.46 KB
/
spotifyClient.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
112
113
114
115
116
117
118
119
import spotipy
import sys
import spotipy.util as util
import credentials
import requests
import json
from ast import literal_eval
import login
from login import *
from track import *
from spotipy_utils import *
import time
def search_track(param):
sp = spotipy.Spotify(get_token())
results = sp.search(param, limit=10, offset=0, type='track', market=None)
json_data = json.dumps(results, indent=2)
response = json_data.replace("'", '"')
response = json_data.replace("\\", "")
output = []
response = json.loads(response)
for item in response['tracks']['items']:
newObj = Track(item['name'], item['album']['artists'][0]['name'], item['album']['name'], item['id'], item['album']['images'][2]['url'])
output.append(newObj)
#print("%s from album %s with id %s" % (item['name'], item['album']['name'], item['id']))
#print(item['album']['images'][2]['url'])
return output
def show_tracks(tracks):
output = []
for i, item in enumerate(tracks['items']):
track = item['track']
print(" %d %32.32s %s %s" % (i, track['artists'][0]['name'],track['name'], track['id']))
newObj = Track(track['name'], track['artists'][0]['name'], track['album']['name'], track['id'], track['album']['images'][2]['url'])
output.append(newObj)
return output
def read_playlist(id):
username = get_user()
sp = spotipy.Spotify(get_token())
playlist = sp.user_playlist(username, playlist_id = id)
if playlist['owner']['id'] == username:
print(playlist['name'])
print(' total tracks', playlist['tracks']['total'])
results = sp.user_playlist(username, playlist['id'], fields="tracks,next")
tracks = results['tracks']
output = show_tracks(tracks)
return output
def play_track(id, song_queue):
full_id = "spotify:track:" + id
full_context = "spotify:user:" + get_user() + ":playlist:" + song_queue.get_playlist_id()
sp = spotipy.Spotify(get_playback_token('write'))
token = sp.audio_features(id)
json_data = json.dumps(token, indent=2)
response = json_data.replace("'", '"')
response = json_data.replace("\\", "")
response = json.loads(response)
sp.start_playback(device_id = None, context_uri = None, uris = [full_id], offset = None)
return time.time() + float(response[0]['duration_ms']) / 1000
#print(sp.devices())
def remove_current(playlist_id):
track_id = get_current()
#playlist-modify-public
remove_from_playlist(track_id, playlist_id)
def get_current():
#user-read-playback-state
sp = spotipy.Spotify(auth = get_playback_token('read') )
results = sp.current_playback()
json_data = json.dumps(results, indent=2)
#print(json_data)
response = json_data.replace("'", '"')
response = json_data.replace("\\", "")
response = json.loads(response)
if response is not None:
track_id = response['item']['id']
else:
track_id = 0
return track_id
def reorder_track(start, before, id):
sp = spotipy.Spotify(get_token())
sp.user_playlist_reorder_tracks(user = get_user(), playlist_id = id, range_start = start, insert_before = before, range_length = 1)
# Main method
if __name__ == "__main__":
print(time.time())
#print time.time() + 255
#timedelta(date).seconds = timedelta(date).seconds + 255
#print date
#reorder_track(1, 0, "5OyaappkOODQPVWGZesvUr")
#play_track("5cbpoIu3YjoOwbBDGUEp3P")
#remove_current("5OyaappkOODQPVWGZesvUr")
#input = read_playlist("5OyaappkOODQPVWGZesvUr")
#for each in input:
#print(each.track_name)
#output = search_track("martin garrix")
#for item in output:
#print ("%s by %s - %s" % (item.track_name, item.artist_name, item.track_id))