2014-10-10 18:48:00 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
__all__ = ['douyutv_download']
|
|
|
|
|
|
|
|
from ..common import *
|
2017-08-07 23:15:07 +03:00
|
|
|
from ..util.log import *
|
2014-10-10 18:48:00 +04:00
|
|
|
import json
|
2015-09-07 06:10:38 +03:00
|
|
|
import hashlib
|
|
|
|
import time
|
2017-08-07 23:15:07 +03:00
|
|
|
import re
|
|
|
|
|
2018-05-11 07:01:31 +03:00
|
|
|
headers = {
|
|
|
|
'user-agent': 'Mozilla/5.0 (iPad; CPU OS 8_1_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B466 Safari/600.1.4'
|
|
|
|
}
|
|
|
|
|
2017-08-07 23:15:07 +03:00
|
|
|
def douyutv_video_download(url, output_dir='.', merge=True, info_only=False, **kwargs):
|
|
|
|
ep = 'http://vmobile.douyu.com/video/getInfo?vid='
|
|
|
|
patt = r'show/([0-9A-Za-z]+)'
|
|
|
|
title_patt = r'<h1>(.+?)</h1>'
|
|
|
|
|
|
|
|
hit = re.search(patt, url)
|
|
|
|
if hit is None:
|
|
|
|
log.wtf('Unknown url pattern')
|
|
|
|
vid = hit.group(1)
|
|
|
|
|
2018-05-11 07:01:31 +03:00
|
|
|
page = get_content(url, headers=headers)
|
2017-08-07 23:15:07 +03:00
|
|
|
hit = re.search(title_patt, page)
|
|
|
|
if hit is None:
|
|
|
|
title = vid
|
|
|
|
else:
|
|
|
|
title = hit.group(1)
|
|
|
|
|
|
|
|
meta = json.loads(get_content(ep + vid))
|
|
|
|
if meta['error'] != 0:
|
|
|
|
log.wtf('Error from API server')
|
|
|
|
m3u8_url = meta['data']['video_url']
|
|
|
|
print_info('Douyu Video', title, 'm3u8', 0, m3u8_url=m3u8_url)
|
|
|
|
if not info_only:
|
|
|
|
urls = general_m3u8_extractor(m3u8_url)
|
|
|
|
download_urls(urls, title, 'ts', 0, output_dir=output_dir, merge=merge, **kwargs)
|
2014-10-10 18:48:00 +04:00
|
|
|
|
2018-05-11 07:01:31 +03:00
|
|
|
|
|
|
|
def douyutv_download(url, output_dir='.', merge=True, info_only=False, **kwargs):
|
2017-08-07 23:15:07 +03:00
|
|
|
if 'v.douyu.com/show/' in url:
|
|
|
|
douyutv_video_download(url, output_dir=output_dir, merge=merge, info_only=info_only, **kwargs)
|
|
|
|
return
|
|
|
|
|
2018-06-25 11:55:13 +03:00
|
|
|
url = re.sub(r'.*douyu.com','https://m.douyu.com/room', url)
|
2017-11-08 09:38:44 +03:00
|
|
|
html = get_content(url, headers)
|
2018-06-25 11:55:13 +03:00
|
|
|
room_id_patt = r'"rid"\s*:\s*(\d+),'
|
2016-07-29 15:50:15 +03:00
|
|
|
room_id = match1(html, room_id_patt)
|
2016-08-08 21:27:59 +03:00
|
|
|
if room_id == "0":
|
2018-05-11 07:01:31 +03:00
|
|
|
room_id = url[url.rfind('/') + 1:]
|
2016-07-29 08:35:05 +03:00
|
|
|
|
2017-11-08 09:38:44 +03:00
|
|
|
api_url = "http://www.douyutv.com/api/v1/"
|
|
|
|
args = "room/%s?aid=wp&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)
|
|
|
|
|
|
|
|
content = get_content(json_request_url, headers)
|
2017-07-12 12:33:05 +03:00
|
|
|
json_content = json.loads(content)
|
|
|
|
data = json_content['data']
|
2018-05-11 07:01:31 +03:00
|
|
|
server_status = json_content.get('error', 0)
|
2015-09-07 06:10:38 +03:00
|
|
|
if server_status is not 0:
|
|
|
|
raise ValueError("Server returned error:%s" % server_status)
|
2016-07-29 08:35:05 +03:00
|
|
|
|
2017-11-08 09:38:44 +03:00
|
|
|
title = data.get('room_name')
|
|
|
|
show_status = data.get('show_status')
|
2015-09-07 06:10:38 +03:00
|
|
|
if show_status is not "1":
|
|
|
|
raise ValueError("The live stream is not online! (Errno:%s)" % server_status)
|
2016-07-29 08:35:05 +03:00
|
|
|
|
2017-11-08 09:38:44 +03:00
|
|
|
real_url = data.get('rtmp_url') + '/' + data.get('rtmp_live')
|
2014-11-18 10:57:04 +03:00
|
|
|
|
2014-10-25 15:46:25 +04:00
|
|
|
print_info(site_info, title, 'flv', float('inf'))
|
2014-10-10 18:48:00 +04:00
|
|
|
if not info_only:
|
2018-05-11 07:01:31 +03:00
|
|
|
download_url_ffmpeg(real_url, title, 'flv', params={}, output_dir=output_dir, merge=merge)
|
|
|
|
|
2014-10-10 18:48:00 +04:00
|
|
|
|
2016-03-25 10:44:32 +03:00
|
|
|
site_info = "douyu.com"
|
2014-10-10 18:48:00 +04:00
|
|
|
download = douyutv_download
|
2016-03-25 10:44:32 +03:00
|
|
|
download_playlist = playlist_not_supported('douyu')
|