-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshazammanager.py
65 lines (45 loc) · 1.95 KB
/
shazammanager.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
import os.path
from apikey import APIKey
import requests
import json
class ShazamManager:
def __init__(self):
key = APIKey().api_key
self.headers_get = {
"X-RapidAPI-Key": key,
"X-RapidAPI-Host": "shazam.p.rapidapi.com"
}
self.headers_post = {
"content-type": "text/plain",
"X-RapidAPI-Key": key,
"X-RapidAPI-Host": "shazam.p.rapidapi.com"
}
def get_from_api(self, endpoint_url, querystring):
response = requests.request("GET", endpoint_url, headers=self.headers_get, params=querystring)
response.raise_for_status()
return json.loads(response.text)
def post_to_api(self, endpoint_url, data):
response = requests.request("POST", endpoint_url, data=data, headers=self.headers_post)
response.raise_for_status()
return json.loads(response.text)
def detect_raw_audio(self, bytestream):
url = "https://shazam.p.rapidapi.com/songs/detect"
return self.post_to_api(url, bytestream)
def get_details_from_song_id(self, song_id):
url = "https://shazam.p.rapidapi.com/songs/get-details"
querystring = {"key": song_id, "locale": "en-US"}
return self.get_from_api(url, querystring)
def get_album_details_from_album_id(self, album_id):
url = "https://shazam.p.rapidapi.com/albums/get-details"
querystring = {"id": album_id, "l": "en-US"}
return self.get_from_api(url, querystring)
@staticmethod
def get_album_artwork(filepath, song_details):
img_url = song_details['images']['coverart']
base_name = os.path.basename(filepath)
base_name_no_ext = base_name[:base_name.rindex('.')]
dir_name = os.path.dirname(filepath)
jpg_path = os.path.join(dir_name, base_name_no_ext + '.jpg')
img_data = requests.get(img_url).content
with open(jpg_path, 'wb') as handler:
handler.write(img_data)