Esempio n. 1
0
    def get_lrc(self, playingsong):
        """
        获取歌词

        如果测试频繁会发如下信息:
        {'msg': 'You API access rate limit has been exceeded.
                 Contact [email protected] if you want higher limit. ',
         'code': 1998,
         'request': 'POST /v2/fm/lyric'}
        """
        try:
            url = "http://api.douban.com/v2/fm/lyric"
            postdata = {
                'sid': playingsong['sid'],
                'ssid': playingsong['ssid'],
            }
            s = requests.session()
            response = s.post(url, data=postdata, headers=HEADERS)

            # 把歌词解析成字典
            lyric = json.loads(response.text, object_hook=decode_dict)
            if lyric.get('code', None) == 1998:
                logger.info('lrc API access rate limit has been exceeded')
                return {}
            lrc_dic = lrc2dict(lyric['lyric'])

            # 原歌词用的unicode,为了兼容
            for key, value in lrc_dic.iteritems():
                lrc_dic[key] = value.decode('utf-8')
            if lrc_dic:
                logger.debug('Get lyric success!')
            return lrc_dic
        except requests.exceptions.RequestException:
            logger.error('Get lyric failed!')
            return {}
Esempio n. 2
0
    def get_lrc(self, playingsong):
        """
        获取歌词

        如果测试频繁会发如下信息:
        {'msg': 'You API access rate limit has been exceeded.
                 Contact [email protected] if you want higher limit. ',
         'code': 1998,
         'request': 'POST /v2/fm/lyric'}
        """
        try:
            url = "http://api.douban.com/v2/fm/lyric"
            postdata = {
                'sid': playingsong['sid'],
                'ssid': playingsong['ssid'],
            }
            s = requests.session()
            response = s.post(url, data=postdata)

            # 把歌词解析成字典
            lyric = json.loads(response.text, object_hook=decode_dict)
            if lyric.get('code', None) == 1998:
                logger.info('lrc API access rate limit has been exceeded')
                return {}
            lrc_dic = lrc2dict(lyric['lyric'])

            # 原歌词用的unicode,为了兼容
            for key, value in lrc_dic.iteritems():
                lrc_dic[key] = value.decode('utf-8')
            if lrc_dic:
                logger.debug('Get lyric success!')
            return lrc_dic
        except requests.exceptions.RequestException:
            logger.error('Get lyric failed!')
            return {}