xiaoqie / pybiliroku

使用 python 写的自动化 B 站录播机。
GNU General Public License v3.0
13 stars 3 forks source link

linux下运行报错 #1

Open ZhongMingKun opened 4 years ago

ZhongMingKun commented 4 years ago
Traceback (most recent call last):
  File "roku.py", line 88, in <module>
    flv_url = random.choice(info['playUrlRes']['data']["durl"])["url"] if "playUrlRes" in info else None
KeyError: 'durl'

单独把获取playUrlRes的部分运行

import urllib.request
import json

def get_json_from(url):
    req = urllib.request.Request(url)
    with urllib.request.urlopen(req, timeout=5) as conn:
        response = conn.read()
        text = response.decode("utf-8")
        info = json.loads(text)
        return info

playUrlRes = get_json_from(f"https://api.live.bilibili.com/room/v1/Room/playUrl?cid=81170&qn=0&platform=web")

print(playUrlRes)

返回异常数据

{
  'code': 19001012,
  'message': '调用bvc-play-url-one出错',
  'ttl': 1,
  'data': {
    'current_quality': 0,
    'accept_quality': None,
    'current_qn': 0
  }
}

请问要怎么解决

xiaoqie commented 4 years ago

我这里访问 https://api.live.bilibili.com/room/v1/Room/playUrl?cid=81170&qn=0&platform=web 不会得到异常。 最近b站经常换api。 访问 https://api.live.bilibili.com/xlive/web-room/v1/index/getRoomPlayInfo?room_id=81170&play_url=1&mask=1&qn=0&platform=web 这个会返回成功吗?

ZhongMingKun commented 4 years ago

windows下可正常访问https://api.live.bilibili.com/room/v1/Room/playUrl?cid=81170&qn=0&platform=web ,但在linux下访问会出错 另外 https://api.live.bilibili.com/xlive/web-room/v1/index/getRoomPlayInfo?room_id=81170&play_url=1&mask=1&qn=0&platform=web 会返回一下信息,请问信息要怎么用

{
  'code': 0,
  'message': '0',
  'ttl': 1,
  'data': {
    'room_id': 81170,
    'short_id': 0, 'uid': 376772,
    'need_p2p': 0,
    'is_hidden': False,
    'is_locked': False,
    'is_portrait': False,
    'live_status': 1,
    'hidden_till': 0,
    'lock_till': 0,
    'encrypted': False,
    'pwd_verified': True,
    'live_time': 1578031308,
    'room_shield': 1,
    'is_sp': 0,
    'special_type': 0,
    'play_url': None
  }
}
xiaoqie commented 4 years ago

怀疑是user-agent的问题

import urllib.request
import json

def get_json_from(url):
    req = urllib.request.Request(
        url,
        headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36',
        }
    )
    with urllib.request.urlopen(req, timeout=5) as conn:
        response = conn.read()
        text = response.decode("utf-8")
        info = json.loads(text)
        return info

playUrlRes = get_json_from(f"https://api.live.bilibili.com/room/v1/Room/playUrl?cid=81170&qn=0&platform=web")

print(playUrlRes)

这样可否获得成功的返回?

这个api主要是为了获取下载地址,但是你的返回结果并没有play_url

ZhongMingKun commented 4 years ago

还是一样呢。。

xiaoqie commented 4 years ago

现在还报错吗

ZhongMingKun commented 4 years ago

还有的,同样的报错

luoshuijs commented 4 years ago

我在Linux执行是没什么问题的,看那个返回的错误代码,估计是B站那边的问题。 而且按你的那个”单独把获取playUrlRes的部分运行"的代码放在我这边Linux运行后,返回的数据没有异常。