-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify.py
41 lines (36 loc) · 1.38 KB
/
spotify.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
"""This module allows Spotify urls to be used and the equivalent song is searched on YouTube from details of the spotify track"""
import re
import requests
bearer = None
def getSpotifyBearer():
url = "https://accounts.spotify.com/api/token"
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ZTdmNjNjYjg5M2QwNDA5MzljOTFlOWE1ZjBiMTRkNTQ6ZDQ2MjBhMjEwNzdkNDU1ZTllMWY1MTkzNzBhOGQxYTY=',
}
response = requests.request("POST", url, headers=headers, data='grant_type=client_credentials', timeout=5)
if (response.status_code == 200):
return response.json()['access_token']
else :
raise ValueError("Failed to get Spotify Bearer token")
def getTrackDetails(trackURL):
pattern = r"https:\/\/open\.spotify\.com\/track\/([a-zA-Z0-9]+)"
track_id = None
match = re.search(pattern, trackURL)
if match:
track_id = match.group(1)
else:
print("Spotify Track ID not found in URL")
return None
global bearer
if bearer is None:
bearer = getSpotifyBearer()
url = f"https://api.spotify.com/v1/tracks/{track_id}"
headers = {
'Authorization': 'Bearer ' + bearer
}
response = requests.request("GET", url, headers=headers, timeout=5)
if (response.status_code == 200):
return response.json()
else :
raise ValueError("Failed to get track details")