Skip to content

Instantly share code, notes, and snippets.

@Justsoos
Last active June 2, 2021 02:46
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Justsoos/5287887068d902b6d3ed3534a47f3ff3 to your computer and use it in GitHub Desktop.
Save Justsoos/5287887068d902b6d3ed3534a47f3ff3 to your computer and use it in GitHub Desktop.
斗鱼2019新api,直播流URL提取,请看最下评论地址
#! /usr/bin/env python3
import re
import requests
import argparse
import time
import hashlib
import json
import logging
from requests.adapters import HTTPAdapter
sess = requests.Session()
sess.mount('', HTTPAdapter(max_retries=5))
headers = {"User-Agent":"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Mobile Safari/537.36"}
logging.getLogger().setLevel(logging.DEBUG)
logging.debug('')
def get_api(url,rate):
url = re.sub(r'.*douyu.com','https://m.douyu.com/room',url)
sess.headers.update({'referer': url})
for i in range(1,8):
html = sess.get(url, headers=headers, allow_redirects=False)
if html.status_code==200:
break
# room_id_patt = r'room_id\s*:\s*(\d+),'
room_id_patt = r'"rid"\s*:\s*(\d+),'
room_id = re.search(room_id_patt, html.text).group(1)
api_url = "http://www.douyutv.com/api/v1/"
args = "room/%s?aid=wp&cdn=ws&client_sys=wp&time=%d" % (room_id, int(time.time()))
auth_md5 = (args + "zNzMV1y4EMxOHS6I5WKm").encode("utf-8")
auth_str = hashlib.md5(auth_md5).hexdigest()
json_request_url = "%s%s&auth=%s" % (api_url, args, auth_str)
sess.headers.update({'referer': json_request_url})
content = sess.get(json_request_url, headers=headers)
json_content = json.loads(content.text)
data = json_content['data']
show_status = data.get('show_status')
if show_status is not "1":
raise ValueError("Offline!!")
other_bit = data.get('rtmp_multi_bitrate')
if rate == "1":
real_url = data.get('rtmp_url') + '/' + other_bit.get('middle')
elif rate == "2":
real_url = data.get('rtmp_url') + '/' + other_bit.get('middle2')
else:
real_url = data.get('rtmp_url') + '/' + data.get('rtmp_live')
return(real_url)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('url')
parser.add_argument('ratio',nargs='?',default='3')
args = parser.parse_args()
rid = args.url
rate = args.ratio
flv_url = get_api(rid,rate)
print (flv_url)
@Justsoos
Copy link
Author

Justsoos commented Jan 26, 2018

斗鱼的技术干什么吃的? 最高码率蓝光5M - 8M,中间码率突然跌落10倍,到高清700k,最低流畅450k,并没有其他档位。
你在拿观众搞笑嘛?

@Justsoos
Copy link
Author

Justsoos commented Jan 26, 2018

LPL2018直播,一共5个子房间 https://www.douyu.com/lpl ,斗鱼前端并没有显式放出URL,观众请手动使用:
"https://www.douyu.com/288016"
"https://www.douyu.com/424559"
"https://www.douyu.com/664810"
"https://www.douyu.com/673305"
"https://www.douyu.com/673327"

用法:
#douyu_new.py https://www.douyu.com/xxxx 2
1是流畅450k,2是高清750k,3或者不加是蓝光或者最高清晰度,可以达到5M到8M

@p0we7
Copy link

p0we7 commented May 22, 2018

好像已经失效了

@Justsoos
Copy link
Author

Justsoos commented Oct 8, 2018

好像已经失效了

还以为这里没人看,更新了。 @p0we7

@obrua
Copy link

obrua commented Oct 15, 2018

能不能写一个获取竞猜结果的,抓了半天包,都没想到办法

@NeverAgain11
Copy link

用 mpv 播放,播几秒钟会自动断开,是什么原因?

@Justsoos
Copy link
Author

Justsoos commented Oct 29, 2018

用 mpv 播放,播几秒钟会自动断开,是什么原因?

要设置 mpv --no-ytdl ,mpv会去探测流地址消耗掉token

@Tesp
Copy link

Tesp commented Nov 7, 2018

大佬 知道ijkplayer 如何避免消耗掉token吗 我这也是遇到 播了几秒就自动断开的情况

@cxc17
Copy link

cxc17 commented Feb 26, 2019

大佬,已经失效了,
api_url = "http://www.douyutv.com/api/v1/"
args = "room/%s?aid=wp&cdn=ws&client_sys=wp&time=%d" % (room_id, int(time.time()))
auth_md5 = (args + "zNzMV1y4EMxOHS6I5WKm").encode("utf-8")
auth_str = hashlib.md5(auth_md5).hexdigest()
json_request_url = "%s%s&auth=%s" % (api_url, args, auth_str)
这个获取不了了

@yjCola
Copy link

yjCola commented Feb 27, 2019

确实获取不到了,有没有新的加密串呀

@Justsoos
Copy link
Author

Justsoos commented Mar 4, 2019

斗鱼2019春节过完立刻改了api,目前请看这里,基本可实用新api:
streamlink/streamlink#2328 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment