youtube-dl

Another place where youtube-dl lives on
git clone git://git.oshgnacknak.de/youtube-dl.git
Log | Files | Refs | README | LICENSE

commit ce7ccb1caa1c6122329779adfe5993dfd47c4021
parent 295eac6165bae681576db8b8da32437822041b4d
Author: Sergey M․ <dstftw@gmail.com>
Date:   Mon,  2 Jan 2017 22:53:05 +0700

[cctv] Improve and merge with cntv (closes #879, closes #6753, closes #8541)

Diffstat:
Myoutube_dl/extractor/cctv.py | 156+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
Dyoutube_dl/extractor/cntv.py | 123-------------------------------------------------------------------------------
Myoutube_dl/extractor/extractors.py | 1-
3 files changed, 128 insertions(+), 152 deletions(-)

diff --git a/youtube_dl/extractor/cctv.py b/youtube_dl/extractor/cctv.py @@ -4,27 +4,84 @@ from __future__ import unicode_literals import re from .common import InfoExtractor -from ..utils import float_or_none +from ..compat import compat_str +from ..utils import ( + float_or_none, + try_get, + unified_timestamp, +) class CCTVIE(InfoExtractor): - _VALID_URL = r'''(?x)https?://(?:.+?\.)? - (?: - cctv\.(?:com|cn)| - cntv\.cn - )/ - (?: - video/[^/]+/(?P<id>[0-9a-f]{32})| - \d{4}/\d{2}/\d{2}/(?P<display_id>VID[0-9A-Za-z]+) - )''' + IE_DESC = '央视网' + _VALID_URL = r'https?://(?:[^/]+)\.(?:cntv|cctv)\.(?:com|cn)/(?:[^/]+/)*?(?P<id>[^/?#&]+?)(?:/index)?\.s?html' _TESTS = [{ - 'url': 'http://english.cntv.cn/2016/09/03/VIDEhnkB5y9AgHyIEVphCEz1160903.shtml', - 'md5': '819c7b49fc3927d529fb4cd555621823', + 'url': 'http://sports.cntv.cn/2016/02/12/ARTIaBRxv4rTT1yWf1frW2wi160212.shtml', + 'md5': 'd61ec00a493e09da810bf406a078f691', 'info_dict': { - 'id': '454368eb19ad44a1925bf1eb96140a61', + 'id': '5ecdbeab623f4973b40ff25f18b174e8', 'ext': 'mp4', - 'title': 'Portrait of Real Current Life 09/03/2016 Modern Inventors Part 1', - } + 'title': '[NBA]二少联手砍下46分 雷霆主场击败鹈鹕(快讯)', + 'description': 'md5:7e14a5328dc5eb3d1cd6afbbe0574e95', + 'duration': 98, + 'uploader': 'songjunjie', + 'timestamp': 1455279956, + 'upload_date': '20160212', + }, + }, { + 'url': 'http://tv.cctv.com/2016/02/05/VIDEUS7apq3lKrHG9Dncm03B160205.shtml', + 'info_dict': { + 'id': 'efc5d49e5b3b4ab2b34f3a502b73d3ae', + 'ext': 'mp4', + 'title': '[赛车]“车王”舒马赫恢复情况成谜(快讯)', + 'description': '2月4日,蒙特泽莫罗透露了关于“车王”舒马赫恢复情况,但情况是否属实遭到了质疑。', + 'duration': 37, + 'uploader': 'shujun', + 'timestamp': 1454677291, + 'upload_date': '20160205', + }, + 'params': { + 'skip_download': True, + }, + }, { + 'url': 'http://english.cntv.cn/special/four_comprehensives/index.shtml', + 'info_dict': { + 'id': '4bb9bb4db7a6471ba85fdeda5af0381e', + 'ext': 'mp4', + 'title': 'NHnews008 ANNUAL POLITICAL SEASON', + 'description': 'Four Comprehensives', + 'duration': 60, + 'uploader': 'zhangyunlei', + 'timestamp': 1425385521, + 'upload_date': '20150303', + }, + 'params': { + 'skip_download': True, + }, + }, { + 'url': 'http://cctv.cntv.cn/lm/tvseries_russian/yilugesanghua/index.shtml', + 'info_dict': { + 'id': 'b15f009ff45c43968b9af583fc2e04b2', + 'ext': 'mp4', + 'title': 'Путь,усыпанный космеями Серия 1', + 'description': 'Путь, усыпанный космеями', + 'duration': 2645, + 'uploader': 'renxue', + 'timestamp': 1477479241, + 'upload_date': '20161026', + }, + 'params': { + 'skip_download': True, + }, + }, { + 'url': 'http://ent.cntv.cn/2016/01/18/ARTIjprSSJH8DryTVr5Bx8Wb160118.shtml', + 'only_matching': True, + }, { + 'url': 'http://tv.cntv.cn/video/C39296/e0210d949f113ddfb38d31f00a4e5c44', + 'only_matching': True, + }, { + 'url': 'http://english.cntv.cn/2016/09/03/VIDEhnkB5y9AgHyIEVphCEz1160903.shtml', + 'only_matching': True, }, { 'url': 'http://tv.cctv.com/2016/09/07/VIDE5C1FnlX5bUywlrjhxXOV160907.shtml', 'only_matching': True, @@ -34,20 +91,63 @@ class CCTVIE(InfoExtractor): }] def _real_extract(self, url): - video_id, display_id = re.match(self._VALID_URL, url).groups() - if not video_id: - webpage = self._download_webpage(url, display_id) - video_id = self._search_regex( - r'(?:fo\.addVariable\("videoCenterId",\s*|guid\s*=\s*)"([0-9a-f]{32})', - webpage, 'video_id') - api_data = self._download_json( - 'http://vdn.apps.cntv.cn/api/getHttpVideoInfo.do?pid=' + video_id, video_id) - m3u8_url = re.sub(r'maxbr=\d+&?', '', api_data['hls_url']) + video_id = self._match_id(url) + webpage = self._download_webpage(url, video_id) + + video_id = self._search_regex( + [r'var\s+guid\s*=\s*["\']([\da-fA-F]+)', + r'videoCenterId["\']\s*,\s*["\']([\da-fA-F]+)', + r'"changePlayer\s*\(\s*["\']([\da-fA-F]+)', + r'"load[Vv]ideo\s*\(\s*["\']([\da-fA-F]+)'], + webpage, 'video id', default=video_id) + + data = self._download_json( + 'http://vdn.apps.cntv.cn/api/getHttpVideoInfo.do', video_id, + query={ + 'pid': video_id, + 'url': url, + 'idl': 32, + 'idlr': 32, + 'modifyed': 'false', + }) + + title = data['title'] + + formats = [] + + video = data.get('video') + if isinstance(video, dict): + for quality, chapters_key in enumerate(('lowChapters', 'chapters')): + video_url = try_get( + video, lambda x: x[chapters_key][0]['url'], compat_str) + if video_url: + formats.append({ + 'url': video_url, + 'format_id': 'http', + 'quality': quality, + 'preference': -1, + }) + + hls_url = try_get(data, lambda x: x['hls_url'], compat_str) + if hls_url: + hls_url = re.sub(r'maxbr=\d+&?', '', hls_url) + formats.extend(self._extract_m3u8_formats( + hls_url, video_id, 'mp4', entry_protocol='m3u8_native', + m3u8_id='hls', fatal=False)) + + self._sort_formats(formats) + + uploader = data.get('editer_name') + description = self._html_search_meta('description', webpage) + timestamp = unified_timestamp(data.get('f_pgmtime')) + duration = float_or_none(try_get(video, lambda x: x['totalLength'])) return { 'id': video_id, - 'title': api_data['title'], - 'formats': self._extract_m3u8_formats( - m3u8_url, video_id, 'mp4', 'm3u8_native', fatal=False), - 'duration': float_or_none(api_data.get('video', {}).get('totalLength')), + 'title': title, + 'description': description, + 'uploader': uploader, + 'timestamp': timestamp, + 'duration': duration, + 'formats': formats, } diff --git a/youtube_dl/extractor/cntv.py b/youtube_dl/extractor/cntv.py @@ -1,123 +0,0 @@ -# coding: utf-8 -from __future__ import unicode_literals - -from .common import InfoExtractor -from ..compat import compat_urllib_request - -class CntvIE(InfoExtractor): - IE_NAME = 'cntv' - IE_DESC = '央视网' - _VALID_URL = r'''(?x) - (?: - http://(?:ent|tv|sports|english|chunwan)\.(?:cntv|cctv)\.(?:com|cn)/ - (?: - (?:video|special)/(?:[_A-Za-z0-9]+)|(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+) - ) - ) - /(?P<id>[A-Za-z0-9]+)(index)?(?:\.shtml|) - ''' - - _TESTS = [{ - 'url': 'http://sports.cntv.cn/2016/02/12/ARTIaBRxv4rTT1yWf1frW2wi160212.shtml', - 'md5': 'd0f8c3e043fe43534517ed47c831fd4e', - 'info_dict': { - 'id': '5ecdbeab623f4973b40ff25f18b174e8', - 'ext': 'flv', - 'title': '[NBA]二少联手砍下46分 雷霆主场击败鹈鹕(快讯)', - 'description': 'md5:7e14a5328dc5eb3d1cd6afbbe0574e95', - 'uploader': 'songjunjie' - } - },{ - 'url': 'http://tv.cctv.com/2016/02/05/VIDEUS7apq3lKrHG9Dncm03B160205.shtml', - 'md5': '3bde627de65b6bad064f3e67ec9e71a1', - 'info_dict': { - 'id': 'efc5d49e5b3b4ab2b34f3a502b73d3ae', - 'ext': 'flv', - 'title': '[赛车]“车王”舒马赫恢复情况成谜(快讯)', - 'description': '2月4日,蒙特泽莫罗透露了关于“车王”舒马赫恢复情况,但情况是否属实遭到了质疑。', - 'uploader': 'shujun' - } - },{ - 'url': 'http://english.cntv.cn/special/four_comprehensives/index.shtml', - 'md5': 'f37a56d5f16647eae24c3e618f8d23a2', - 'info_dict': { - 'id': '4bb9bb4db7a6471ba85fdeda5af0381e', - 'ext': 'flv', - 'title': 'NHnews008 ANNUAL POLITICAL SEASON', - 'description': 'Four Comprehensives', - 'uploader': 'zhangyunlei' - } - },{ - 'url': 'http://ent.cntv.cn/2016/01/18/ARTIjprSSJH8DryTVr5Bx8Wb160118.shtml', - 'md5': '37ad2407b5f28336ddf26aacea570ee5', - 'info_dict': { - 'id': '45336758dd474ef39dc6887f115e0375', - 'ext': 'flv', - 'title': '《爱国之恋》中国梦新歌展播 综艺视频精切', - 'description': 'md5:856836ae2660f000e458ead597b1e2bd', - 'uploader': 'baiyuqing' - } - },{ - 'url': 'http://tv.cntv.cn/video/C39296/e0210d949f113ddfb38d31f00a4e5c44', - 'md5': 'd7188ecc2a2f8447c70023895724c31c', - 'info_dict': { - 'id': 'e0210d949f113ddfb38d31f00a4e5c44', - 'ext': 'flv', - 'title': '《传奇故事》 20150409 金钱“魔术”(下)', - 'description': '本期节目主要内容: 2014年10月23日凌晨,浙江余杭警方出动2000多名警力,一举摧毁1040阳光工程传销组织,抓获65名涉嫌传销的组织头目,教育遣返400多名参与传销人员。', - 'uploader': '陆银凯' - } - }] - - def _real_extract(self, url): - video_id = self._match_id(url) - webpage = self._download_webpage(url, video_id) - - video_id_regex = [r'var guid[ ="]+([a-zA-Z0-9]+)"', - r'"videoCenterId"[ ,"]+([a-zA-Z0-9]+)"', - r'"changePlayer\(\'([a-zA-Z0-9]+)\'\)\"'] - video_id = self._search_regex(video_id_regex, webpage, 'video_id', default=None) - - if video_id is None: - video_id = self._match_id(url) - - # refer to youku.py - def retrieve_data(req_url, note): - req = compat_urllib_request.Request(req_url) - - cn_verification_proxy = self._downloader.params.get('cn_verification_proxy') - if cn_verification_proxy: - req.add_header('Ytdl-request-proxy', cn_verification_proxy) - - raw_data = self._download_json(req, video_id, note=note) - return raw_data - - get_info_url = \ - 'http://vdn.apps.cntv.cn/api/getHttpVideoInfo.do' + \ - '?pid=%s' % video_id + \ - '&tz=-8' + \ - '&from=000tv' + \ - '&url=%s' % url + \ - '&idl=32' + \ - '&idlr=32' + \ - '&modifyed=false' - - data = retrieve_data( - get_info_url, - 'Downloading JSON metadata') - - title = data.get('title') - uploader = data.get('editer_name') - hls_url = data.get('hls_url') - formats = self._extract_m3u8_formats(hls_url, video_id, 'flv') - self._sort_formats(formats) - - description = self._html_search_meta('description', webpage) - - return { - 'id': video_id, - 'title': title, - 'uploader': uploader, - 'formats': formats, - 'description': description, - } diff --git a/youtube_dl/extractor/extractors.py b/youtube_dl/extractor/extractors.py @@ -178,7 +178,6 @@ from .cnn import ( CNNBlogsIE, CNNArticleIE, ) -from .cntv import CntvIE from .coub import CoubIE from .collegerama import CollegeRamaIE from .comedycentral import (