spotifyprovider.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:xmusic-crawler 作者: rockers7414 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号