获取斗鱼房间以及直播地址

douyu.py

#-*- coding:utf-8 -*-

import requests

import re,sys

import execjs
import time
import hashlib



def get_tt():
    tt1 = str(int(time.time()))
    tt2 = str(int((time.time() * 1000)))
    today = time.strftime('%Y%m%d', time.localtime())
    return tt1, tt2, today


def get_homejs(rid):
    room_url = 'https://m.douyu.com/' + rid
    response = requests.get(url=room_url)
    pattern_real_rid = r'"rid":(\d{1,7})'
    real_rid = re.findall(pattern_real_rid, response.text, re.I)[0]
    if real_rid != rid:
        room_url = 'https://m.douyu.com/' + real_rid
        response = requests.get(url=room_url)
    homejs = ''
    pattern = r'(function ub9.*)[\s\S](var.*)'
    result = re.findall(pattern, response.text, re.I)
    str1 = re.sub(r'eval.*;}', 'strc;}', result[0][0])
    homejs = str1 + result[0][1]
    return homejs, real_rid


def get_sign(rid, post_v, tt, ub9):
    docjs = execjs.compile(ub9)
    res2 = docjs.call('ub98484234')
    str3 = re.sub(r'\(function[\s\S]*toString\(\)', '\'', res2)
    md5rb = hashlib.md5((rid + '10000000000000000000000000001501' + tt + '2501' +
                         post_v).encode('utf-8')).hexdigest()
    str4 = 'function get_sign(){var rb=\'' + md5rb + str3
    str5 = re.sub(r'return rt;}[\s\S]*','return re;};', str4) 
    str6 = re.sub(r'"v=.*&sign="\+', '', str5)
    docjs1 = execjs.compile(str6)
    sign = docjs1.call(
        'get_sign', rid, '10000000000000000000000000001501', tt)
    return sign


def mix_room(rid):
    result1 = 'PKing'
    return result1


def get_pre_url(rid, tt):
    request_url = 'https://playweb.douyucdn.cn/lapi/live/hlsH5Preview/' + rid
    post_data = {
        'rid': rid,
        'did': '10000000000000000000000000001501'
    }
    auth = hashlib.md5((rid + str(tt)).encode('utf-8')).hexdigest()
    header = {
        'content-type': 'application/x-www-form-urlencoded',
        'rid': rid,
        'time': tt,
        'auth': auth
    }
    response = requests.post(url=request_url, headers=header, data=post_data)
    response = response.json()
    pre_url = ''
    if response.get('error') == 0:
        real_url = (response.get('data')).get('rtmp_live')
        if 'mix=1' in real_url:
            pre_url = mix_room(rid)
        else:
            pattern1 = r'^[0-9a-zA-Z]*'
            pre_url = re.search(pattern1, real_url, re.I).group()
    return pre_url


def get_sign_url(post_v, rid, tt, ub9):
    sign = get_sign(rid, post_v, tt, ub9)
    request_url = 'https://m.douyu.com/api/room/ratestream'
    post_data = {
        'v': '2501' + post_v,
        'did': '10000000000000000000000000001501',
        'tt': tt,
        'sign': sign,
        'ver': '219032101',
        'rid': rid,
        'rate': '-1'
    }
    header = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Mobile Safari/537.36'
    }
    response = requests.post(url=request_url, headers=header, data=post_data).json()
    if response.get('code') == 0:
        real_url = (response.get('data')).get('url')
        if 'mix=1' in real_url:
            result1 = mix_room(rid)
        else:
            pattern1 = r'live/(\d{1,8}[0-9a-zA-Z]+)_?[\d]{0,4}/playlist'
            result1 = re.findall(pattern1, real_url, re.I)[0]
    else:
        result1 = 0
    return result1


def get_real_url(rid):
    rid = str(rid)
    tt = get_tt()
    url = get_pre_url(rid, tt[1])
    if url:
        return "http://tx2play1.douyucdn.cn/live/" + url + ".flv?uuid="
    else:
        result = get_homejs(rid)
        real_rid = result[1]
        homejs = result[0]
        real_url = get_sign_url(tt[2], real_rid, tt[0], homejs)
        if real_url != 0:
            real_url = "http://tx2play1.douyucdn.cn/live/" + real_url + ".flv?uuid="
        else:
            real_url = '未开播'
        return real_url


def get_url_from_js(rid):
    # 从播放页源代码中直接匹配地址
    header = {
    'Content-Type': 'application/x-www-form-urlencoded'
    }
    try:
        response = requests.get('https://www.douyu.com/{}'.format(rid), headers=header).text
        real_url = re.findall(r'live/({}[\d\w]*?)_'.format(rid), response)[0]
    except:
        real_url = '直播间未开播或不存在'
    return "http://tx2play1.douyucdn.cn/live/" + real_url + ".flv?uuid="


#rid = str(input('请输入斗鱼数字房间号:\n'))

def main(rid):
    real_url = get_real_url(rid)
    return real_url




main(rid="607575")
main(rid="2132902")
main(rid="579963")

get_douyu_url.py

# -*- coding: utf-8 -*-

from douyu import main
import requests
import re,sys


reload(sys)
sys.setdefaultencoding("utf-8")

url="https://www.douyu.com/g_LOL"
header={
    "accept":" text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", 
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
}
req=requests.get(url)#获取响应
con=req.content

pattern = re.compile(r'"room_id":"(\d+)",.*?,"nickname":"(.*?)","icon":"(.*?)",')
  
match =re.findall(pattern , con)

f=open(r'インタネットs.txt'.decode("utf-8"),'w')
for li in match:
    #print li[1].decode("utf-8")+",https://www.douyu.com/"+li[0]+" pic:"+li[2]
    print li[1].decode("utf-8")+","+main(li[0])
    f.write(li[1].decode("utf-8")+","+main(li[0])+"\n")
f.close()

インタネットs.txt
辛巴imb丶丶,http://tx2play1.douyucdn.cn/live/3487376r1D1fzmm6.flv?uuid=
ShinyRuoの,http://tx2play1.douyucdn.cn/live/13703ro4JBwp1Bne.flv?uuid=
小猪是超级大帅Bi,http://tx2play1.douyucdn.cn/live/168000rsNmkOeEHj.flv?uuid=
宝贝球球ouo,http://tx2play1.douyucdn.cn/live/5974716rkBzoDGWh.flv?uuid=
叶音符,http://tx2play1.douyucdn.cn/live/12313rjVSeVyskva.flv?uuid=
南波儿大魔王丶,http://tx2play1.douyucdn.cn/live/5526219rEx8vP6zB.flv?uuid=
懒猫猫QAQ,http://tx2play1.douyucdn.cn/live/577224rhfzd1IVRR.flv?uuid=
金咕咕金咕咕doinb,http://tx2play1.douyucdn.cn/live/252140r5BMWiDCGK.flv?uuid=
岁月刀妹QAQ,http://tx2play1.douyucdn.cn/live/5132911roE44q4jf.flv?uuid=
魔獸后裔,http://tx2play1.douyucdn.cn/live/262537reZ88fqH2q.flv?uuid=
李冉冉i,http://tx2play1.douyucdn.cn/live/210398rCKVxHi72Q.flv?uuid=
zzc啊哦额,http://tx2play1.douyucdn.cn/live/666743rZ73OM62TF.flv?uuid=
Night丶杀神风,http://tx2play1.douyucdn.cn/live/217785rEzODyfTBm.flv?uuid=
诺夏辅助,http://tx2play1.douyucdn.cn/live/8932rQE8XMa61dU0.flv?uuid=
阿冷aleng丶,http://tx2play1.douyucdn.cn/live/520rHYUEOg3jqYzk.flv?uuid=
小白白睡醒了吗,http://tx2play1.douyucdn.cn/live/5324613r7VsJEaS8.flv?uuid=
萌面酥,http://tx2play1.douyucdn.cn/live/266055rWTCMD5Egt.flv?uuid=
石页的第一根矛s,http://tx2play1.douyucdn.cn/live/593392rj4WvDD3yn.flv?uuid=
不2不叫周淑怡,http://tx2play1.douyucdn.cn/live/290935rjVxzK8wr8.flv?uuid=
无千语吖,http://tx2play1.douyucdn.cn/live/4496475rUSl3A61C.flv?uuid=
冰凡,http://tx2play1.douyucdn.cn/live/17349rcZgVsrYUDF.flv?uuid=
峡谷杀神酱油瓶,http://tx2play1.douyucdn.cn/live/6717704rLtJSW9ea.flv?uuid=
洞主丨歌神洞庭湖,http://tx2play1.douyucdn.cn/live/138243rFTYzMUEcQ.flv?uuid=
作作金克丝,http://tx2play1.douyucdn.cn/live/544865rcia0F1sCa.flv?uuid=
14德莱文,http://tx2play1.douyucdn.cn/live/2016309r2SksIKcc.flv?uuid=
熙宝兒,http://tx2play1.douyucdn.cn/live/838936rHWLXJM1AC.flv?uuid=
当年的瞎王,http://tx2play1.douyucdn.cn/live/12118rNSZenbNNAF.flv?uuid=
T1丶JK特烦恼,http://tx2play1.douyucdn.cn/live/534183rWTAULgCXv.flv?uuid=
浪灯丶,http://tx2play1.douyucdn.cn/live/600290rJNcnntP0X.flv?uuid=
仙凡哥哥丶,http://tx2play1.douyucdn.cn/live/787579r0NPV4KmRZ.flv?uuid=

发布了107 篇原创文章 · 获赞 23 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_38641985/article/details/104351483