def get_new_release_albums(self):
self.logger.info("Get albums of the new release from Spotify.")
service_host = "https://api.spotify.com"
"""
Get client_id and client_secret value from local.
"""
config = Config()
client_id = config.spotify_client_id
client_secret = config.spotify_client_secret
access_token = Spotify(client_id, client_secret).get_token()
headers = ["Authorization: Bearer " + access_token]
offset = 0
new_release = []
while True:
params = {
"offset": offset,
"limit": 50
}
buf = BytesIO()
client = pycurl.Curl()
client.setopt(pycurl.HEADER, False)
client.setopt(pycurl.HTTPHEADER, headers)
client.setopt(pycurl.URL, service_host +
"/v1/browse/new-releases" + "?" + urlencode(params))
client.setopt(pycurl.WRITEFUNCTION, buf.write)
client.perform()
client.close()
body = json.loads(buf.getvalue().decode("utf-8"))
buf.close()
if body["albums"]["total"] > 0:
for data in body["albums"]["items"]:
album = Album(data["name"], None)
images = []
for image_data in data["images"]:
images.append(Image(image_data["url"],
image_data["width"],
image_data["height"]))
album.images = images
album.provider = self._provider
album.provider_res_id = data["id"]
artist_id = data["artists"][0]["id"] \
if len(data["artists"]) > 0 else ""
new_release.append((artist_id, album))
if body["albums"]["total"] \
> body["albums"]["limit"] * (offset + 1):
offset = offset + 1
else:
break
return new_release
评论列表
文章目录