-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotify_client.py
More file actions
29 lines (26 loc) · 850 Bytes
/
spotify_client.py
File metadata and controls
29 lines (26 loc) · 850 Bytes
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
import os
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
SPOTIPY_CLIENT_ID = os.environ['SPOTIPY_CLIENT_ID']
SPOTIPY_CLIENT_SECRET = os.environ['SPOTIPY_CLIENT_SECRET']
class SpotifyClient:
def __init__(self):
self.sp = spotipy.Spotify (
auth_manager=SpotifyClientCredentials (
client_id=SPOTIPY_CLIENT_ID,
client_secret=SPOTIPY_CLIENT_SECRET
)
)
def getTrackId(self, query):
markets = ['US', 'JP']
for market in markets:
result = self.sp.search (
q=query,
type='track',
limit=1,
offset=0,
market=market
)
if result['tracks']['items']:
return result['tracks']['items'][0]['id']
return -1