-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSbotify.py
117 lines (75 loc) · 3.55 KB
/
Sbotify.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
import spotipy
from spotipy import oauth2
import config
import pprint #!!!!!!!!!
### SCOPES AND CREDENTIALS ###
username = config.spotify_username
scopes = config.scopes
client_id = config.spotify_client_id
client_secret = config.spotify_client_secret
redirect_uri = config.redirect_uri
sp_oauth = oauth2.SpotifyOAuth(client_id=client_id,client_secret=client_secret,redirect_uri=redirect_uri,scope=scopes)
token_info = sp_oauth.get_cached_token()
if not token_info:
auth_url = sp_oauth.get_authorize_url(show_dialog=True)
print(auth_url)
response = input('Paste the above link into your browser, then paste the redirect url here: ')
code = sp_oauth.parse_response_code(response)
token_info = sp_oauth.get_access_token(code)
token = token_info['access_token']
sp = spotipy.Spotify(auth=token)
def refresh():
global token_info, sp
if sp_oauth.is_token_expired(token_info):
token_info = sp_oauth.refresh_access_token(token_info['refresh_token'])
token = token_info['access_token']
sp = spotipy.Spotify(auth=token)
def create_playlist(playlist_title, playlist_desc):
sp.user_playlist_create(username, playlist_title, public=True, description=playlist_desc)
def search_track(track_title, artist_title):
results = sp.search(q=track_title + ' ' + 'artist:' + artist_title,type='track',limit=1)
try:
return results['tracks']['items'][0]['id']
except IndexError:
print('Track not found')
def get_playlist_id(link_id):
playlists = sp.user_playlists(username)
for playlist in playlists['items']:
if (playlist['owner']['id'] == username) and (link_id.lower() in playlist['name'].lower()):
return playlist['id']
return None
def get_all_track_ids(link_id):
# look at docs
pass
def add_track(track_id, playlist_id):
tracks = [track_id] #for some reason, Spotipy only takes an array for track ids
# Check if track is already in playlist
track_list = sp.user_playlist(username, playlist_id, fields="tracks,next")
for track in track_list['tracks']['items']:
if track['track']['id'] == track_id:
return None
sp.user_playlist_add_tracks(username, playlist_id=playlist_id, tracks=tracks)
def remove_track(track_id, playlist_id):
tracks = [track_id]
sp.user_playlist_remove_all_occurrences_of_tracks(username, playlist_id=playlist_id, tracks=tracks)
#BELOW FUNCTIONS ARE FOR POSTING BOT REPLY
def get_track_names(track_id):
track_name = sp.track(track_id)['name']
artist_name = sp.track(track_id)['artists'][0]['name']
return [track_name, artist_name]
def get_track_preview(track_id):
return sp.track(track_id)['preview_url']
def get_track_link(track_id):
return sp.track(track_id)['external_urls']['spotify']
def get_playlist_name(link_id):
playlists = sp.user_playlists(username)
for playlist in playlists['items']:
if (playlist['owner']['id'] == username) and (link_id.lower() in playlist['name'].lower()):
return playlist['name']
return None
def get_playlist_link(link_id):
playlists = sp.user_playlists(username)
for playlist in playlists['items']:
if (playlist['owner']['id'] == username) and (link_id.lower() in playlist['name'].lower()):
return playlist['external_urls']['spotify']
return None