[cctv] Relax _VALID_URL
[youtube-dl] / youtube_dl / extractor / cctv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9     float_or_none,
10     try_get,
11     unified_timestamp,
12 )
13
14
15 class CCTVIE(InfoExtractor):
16     IE_DESC = '央视网'
17     _VALID_URL = r'https?://(?:[^/]+)\.(?:cntv|cctv)\.(?:com|cn)/(?:[^/]+/)*?(?P<id>[^/?#&]+?)(?:/index)?(?:\.s?html|[?#&]|$)'
18     _TESTS = [{
19         'url': 'http://sports.cntv.cn/2016/02/12/ARTIaBRxv4rTT1yWf1frW2wi160212.shtml',
20         'md5': 'd61ec00a493e09da810bf406a078f691',
21         'info_dict': {
22             'id': '5ecdbeab623f4973b40ff25f18b174e8',
23             'ext': 'mp4',
24             'title': '[NBA]二少联手砍下46分 雷霆主场击败鹈鹕(快讯)',
25             'description': 'md5:7e14a5328dc5eb3d1cd6afbbe0574e95',
26             'duration': 98,
27             'uploader': 'songjunjie',
28             'timestamp': 1455279956,
29             'upload_date': '20160212',
30         },
31     }, {
32         'url': 'http://tv.cctv.com/2016/02/05/VIDEUS7apq3lKrHG9Dncm03B160205.shtml',
33         'info_dict': {
34             'id': 'efc5d49e5b3b4ab2b34f3a502b73d3ae',
35             'ext': 'mp4',
36             'title': '[赛车]“车王”舒马赫恢复情况成谜(快讯)',
37             'description': '2月4日,蒙特泽莫罗透露了关于“车王”舒马赫恢复情况,但情况是否属实遭到了质疑。',
38             'duration': 37,
39             'uploader': 'shujun',
40             'timestamp': 1454677291,
41             'upload_date': '20160205',
42         },
43         'params': {
44             'skip_download': True,
45         },
46     }, {
47         'url': 'http://english.cntv.cn/special/four_comprehensives/index.shtml',
48         'info_dict': {
49             'id': '4bb9bb4db7a6471ba85fdeda5af0381e',
50             'ext': 'mp4',
51             'title': 'NHnews008 ANNUAL POLITICAL SEASON',
52             'description': 'Four Comprehensives',
53             'duration': 60,
54             'uploader': 'zhangyunlei',
55             'timestamp': 1425385521,
56             'upload_date': '20150303',
57         },
58         'params': {
59             'skip_download': True,
60         },
61     }, {
62         'url': 'http://cctv.cntv.cn/lm/tvseries_russian/yilugesanghua/index.shtml',
63         'info_dict': {
64             'id': 'b15f009ff45c43968b9af583fc2e04b2',
65             'ext': 'mp4',
66             'title': 'Путь,усыпанный космеями Серия 1',
67             'description': 'Путь, усыпанный космеями',
68             'duration': 2645,
69             'uploader': 'renxue',
70             'timestamp': 1477479241,
71             'upload_date': '20161026',
72         },
73         'params': {
74             'skip_download': True,
75         },
76     }, {
77         'url': 'http://ent.cntv.cn/2016/01/18/ARTIjprSSJH8DryTVr5Bx8Wb160118.shtml',
78         'only_matching': True,
79     }, {
80         'url': 'http://tv.cntv.cn/video/C39296/e0210d949f113ddfb38d31f00a4e5c44',
81         'only_matching': True,
82     }, {
83         'url': 'http://english.cntv.cn/2016/09/03/VIDEhnkB5y9AgHyIEVphCEz1160903.shtml',
84         'only_matching': True,
85     }, {
86         'url': 'http://tv.cctv.com/2016/09/07/VIDE5C1FnlX5bUywlrjhxXOV160907.shtml',
87         'only_matching': True,
88     }, {
89         'url': 'http://tv.cntv.cn/video/C39296/95cfac44cabd3ddc4a9438780a4e5c44',
90         'only_matching': True
91     }]
92
93     def _real_extract(self, url):
94         video_id = self._match_id(url)
95         webpage = self._download_webpage(url, video_id)
96
97         video_id = self._search_regex(
98             [r'var\s+guid\s*=\s*["\']([\da-fA-F]+)',
99              r'videoCenterId["\']\s*,\s*["\']([\da-fA-F]+)',
100              r'"changePlayer\s*\(\s*["\']([\da-fA-F]+)',
101              r'"load[Vv]ideo\s*\(\s*["\']([\da-fA-F]+)'],
102             webpage, 'video id')
103
104         data = self._download_json(
105             'http://vdn.apps.cntv.cn/api/getHttpVideoInfo.do', video_id,
106             query={
107                 'pid': video_id,
108                 'url': url,
109                 'idl': 32,
110                 'idlr': 32,
111                 'modifyed': 'false',
112             })
113
114         title = data['title']
115
116         formats = []
117
118         video = data.get('video')
119         if isinstance(video, dict):
120             for quality, chapters_key in enumerate(('lowChapters', 'chapters')):
121                 video_url = try_get(
122                     video, lambda x: x[chapters_key][0]['url'], compat_str)
123                 if video_url:
124                     formats.append({
125                         'url': video_url,
126                         'format_id': 'http',
127                         'quality': quality,
128                         'preference': -1,
129                     })
130
131         hls_url = try_get(data, lambda x: x['hls_url'], compat_str)
132         if hls_url:
133             hls_url = re.sub(r'maxbr=\d+&?', '', hls_url)
134             formats.extend(self._extract_m3u8_formats(
135                 hls_url, video_id, 'mp4', entry_protocol='m3u8_native',
136                 m3u8_id='hls', fatal=False))
137
138         self._sort_formats(formats)
139
140         uploader = data.get('editer_name')
141         description = self._html_search_meta('description', webpage)
142         timestamp = unified_timestamp(data.get('f_pgmtime'))
143         duration = float_or_none(try_get(video, lambda x: x['totalLength']))
144
145         return {
146             'id': video_id,
147             'title': title,
148             'description': description,
149             'uploader': uploader,
150             'timestamp': timestamp,
151             'duration': duration,
152             'formats': formats,
153         }