Gunale0926 / KO-ON-Bot

A Music Player for KOOK
MIT License
61 stars 21 forks source link

Bilibili '未检索到此歌曲' 問題 #29

Open kkc141 opened 8 months ago

kkc141 commented 8 months ago

status_manage.py內 子程序def getCidAndTitle, def getCidAndTitle中 呼叫程序 session.get 中未加入headers

注意headers中未包含bili_cookie 因為 getCidAndTitle 沒有 bili_cookie 輸入

更新後:


async def getCidAndTitle(duration: dict,
                         deltatime: int,
                         guild: str,
                         bvid: str,
                         session: ClientSession,
                         logger: Logger,
                         p: int = 1):
    url = 'https://api.bilibili.com/x/web-interface/view?bvid=' + bvid
    headers = {
        'Host': 'api.bilibili.com',
        'user-agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',

    }
    async with session.get(url=url, headers=headers, timeout=ClientTimeout(total=5)) as r:
        data = (await r.json())['data']
    title = data['title']
    cid = data['pages'][p - 1]['cid']
    duration[guild] = data['pages'][p - 1]['duration'] + deltatime
    mid = str(data['owner']['mid'])
    name = data['owner']['name']
    pic = data['pic']
    logger.warning(f"{cid},{title},{mid}")
    return str(cid), title, mid, name, pic
async def getAudio(guild: str, item: list, botid: str, bili_cookie:str,session: ClientSession):
    baseUrl = 'http://api.bilibili.com/x/player/playurl?fnval=16&'
    headers = {
        'Host': 'api.bilibili.com',
        'user-agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',

    }
    bvid, cid, title, mid, name, pic = item[0], item[1], item[2], item[
        3], item[4], item[5]
    url = baseUrl + 'bvid=' + bvid + '&cid=' + cid
    async with session.get(url=url, headers=headers, timeout=ClientTimeout(total=5)) as r:
        audioUrl = (await r.json())['data']['dash']['audio'][0]['baseUrl']
    headers = {
        'User-Agent':
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:56.0) Gecko/20100101 Firefox/56.0',
        'Accept': '*/*',
        'Accept-Language': 'en-US,en;q=0.5',
        'Accept-Encoding': 'gzip, deflate, br',
        'Range': 'bytes=0-',
        'Referer':
        'https://api.bilibili.com/x/web-interface/view?bvid=' + bvid,
        'Origin': 'https://www.bilibili.com',
        'Connection': 'keep-alive',
        'cookie': bili_cookie
    }
    async with session.get(url=audioUrl,
                           headers=headers,
                           timeout=ClientTimeout(total=10)) as r:
        with open(guild + "_" + botid + ".mp3", 'wb') as f:
            while True:
                chunk = await r.content.read()
                if not chunk:
                    break
                f.write(chunk)
    return bvid, cid, title, mid, name, pic
olitaire commented 4 months ago

多谢大佬