From 63c21e9e3f8c9b72af0fc4876e90d706f0e8efc6 Mon Sep 17 00:00:00 2001 From: wbt5 Date: Fri, 31 Jul 2020 19:35:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96YY=E7=9B=B4=E6=92=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yy.py | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/yy.py b/yy.py index 2967f61..3f3672f 100644 --- a/yy.py +++ b/yy.py @@ -1,28 +1,35 @@ -# 获取YY直播的真实流媒体地址。 -# 默认画质为高清:1280*720 - - +# 获取YY直播的真实流媒体地址。https://www.yy.com/1349606469 +# 默认获取最高画质 import requests import re import json -def get_real_url(rid): - room_url = 'http://interface.yy.com/hls/new/get/{rid}/{rid}/1200?source=wapyy&callback=jsonp3'.format(rid=rid) +def yy(rid): headers = { 'referer': 'http://wap.yy.com/mobileweb/{rid}'.format(rid=rid), - 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36' - } - try: - response = requests.get(url=room_url, headers=headers).text - json_data = json.loads(re.findall(r'\(([\W\w]*)\)', response)[0]) - real_url = json_data.get('hls', 0) - if not real_url:real_url='未开播或直播间不存在' - except: - real_url = '请求错误或直播间不存在' - return real_url + 'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko)' + ' Version/11.0 Mobile/15A372 Safari/604.1' + } + room_url = 'http://interface.yy.com/hls/new/get/{rid}/{rid}/1200?source=wapyy&callback='.format(rid=rid) + with requests.Session() as s: + res = s.get(room_url, headers=headers) + if res.status_code == 200: + data = json.loads(res.text[1:-1]) + if data.get('hls', 0): + xa = data['audio'] + xv = data['video'] + xv = re.sub(r'0_\d+_0', '0_0_0', xv) + url = 'https://interface.yy.com/hls/get/stream/15013/{}/15013/{}?source=h5player&type=m3u8'.format(xv, xa) + res = s.get(url).json() + real_url = res['hls'] + return real_url + else: + raise Exception('未开播') + else: + raise Exception('直播间不存在') -rid = input('请输入YY直播房间号:\n') -real_url = get_real_url(rid) -print('该直播间源地址为:\n' + real_url) +if __name__ == '__main__': + r = input('输入YY直播房间号:\n') + print(yy(r))