[cbsnews] Relax video info regex (fixes #13284)
[youtube-dl] / youtube_dl / extractor / cbsnews.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from .cbs import CBSIE
6 from ..utils import (
7     parse_duration,
8 )
9
10
11 class CBSNewsIE(CBSIE):
12     IE_NAME = 'cbsnews'
13     IE_DESC = 'CBS News'
14     _VALID_URL = r'https?://(?:www\.)?cbsnews\.com/(?:news|videos)/(?P<id>[\da-z_-]+)'
15
16     _TESTS = [
17         {
18             'url': 'http://www.cbsnews.com/news/tesla-and-spacex-elon-musks-industrial-empire/',
19             'info_dict': {
20                 'id': 'tesla-and-spacex-elon-musks-industrial-empire',
21                 'ext': 'flv',
22                 'title': 'Tesla and SpaceX: Elon Musk\'s industrial empire',
23                 'thumbnail': 'http://beta.img.cbsnews.com/i/2014/03/30/60147937-2f53-4565-ad64-1bdd6eb64679/60-0330-pelley-640x360.jpg',
24                 'duration': 791,
25             },
26             'params': {
27                 # rtmp download
28                 'skip_download': True,
29             },
30             'skip': 'Subscribers only',
31         },
32         {
33             'url': 'http://www.cbsnews.com/videos/fort-hood-shooting-army-downplays-mental-illness-as-cause-of-attack/',
34             'info_dict': {
35                 'id': 'SNJBOYzXiWBOvaLsdzwH8fmtP1SCd91Y',
36                 'ext': 'mp4',
37                 'title': 'Fort Hood shooting: Army downplays mental illness as cause of attack',
38                 'description': 'md5:4a6983e480542d8b333a947bfc64ddc7',
39                 'upload_date': '20140404',
40                 'timestamp': 1396650660,
41                 'uploader': 'CBSI-NEW',
42                 'thumbnail': r're:^https?://.*\.jpg$',
43                 'duration': 205,
44                 'subtitles': {
45                     'en': [{
46                         'ext': 'ttml',
47                     }],
48                 },
49             },
50             'params': {
51                 # m3u8 download
52                 'skip_download': True,
53             },
54         },
55         {
56             'url': 'http://www.cbsnews.com/news/maria-ridulph-murder-will-the-nations-oldest-cold-case-to-go-to-trial-ever-get-solved/',
57             'info_dict': {
58                 'id': 'QpM5BJjBVEAUFi7ydR9LusS69DPLqPJ1',
59                 'ext': 'mp4',
60                 'title': 'Cold as Ice',
61                 'description': 'Can a childhood memory of a friend\'s murder solve a 1957 cold case? "48 Hours" correspondent Erin Moriarty has the latest.',
62                 'upload_date': '20170604',
63                 'timestamp': 1496538000,
64                 'uploader': 'CBSI-NEW',
65             },
66             'params': {
67                 'skip_download': True,
68             },
69         },
70     ]
71
72     def _real_extract(self, url):
73         video_id = self._match_id(url)
74
75         webpage = self._download_webpage(url, video_id)
76
77         video_info = self._parse_json(self._html_search_regex(
78             r'(?:<ul class="media-list items" id="media-related-items"[^>]*><li data-video-info|<div id="cbsNewsVideoPlayer" data-video-player-options)=\'({.+?})\'',
79             webpage, 'video JSON info', default='{}'), video_id, fatal=False)
80
81         if video_info:
82             item = video_info['item'] if 'item' in video_info else video_info
83         else:
84             state = self._parse_json(self._search_regex(
85                 r'data-cbsvideoui-options=(["\'])(?P<json>{.+?})\1', webpage,
86                 'playlist JSON info', group='json'), video_id)['state']
87             item = state['playlist'][state['pid']]
88
89         return self._extract_video_info(item['mpxRefId'], 'cbsnews')
90
91
92 class CBSNewsLiveVideoIE(InfoExtractor):
93     IE_NAME = 'cbsnews:livevideo'
94     IE_DESC = 'CBS News Live Videos'
95     _VALID_URL = r'https?://(?:www\.)?cbsnews\.com/live/video/(?P<id>[^/?#]+)'
96
97     # Live videos get deleted soon. See http://www.cbsnews.com/live/ for the latest examples
98     _TEST = {
99         'url': 'http://www.cbsnews.com/live/video/clinton-sanders-prepare-to-face-off-in-nh/',
100         'info_dict': {
101             'id': 'clinton-sanders-prepare-to-face-off-in-nh',
102             'ext': 'mp4',
103             'title': 'Clinton, Sanders Prepare To Face Off In NH',
104             'duration': 334,
105         },
106         'skip': 'Video gone',
107     }
108
109     def _real_extract(self, url):
110         display_id = self._match_id(url)
111
112         video_info = self._download_json(
113             'http://feeds.cbsn.cbsnews.com/rundown/story', display_id, query={
114                 'device': 'desktop',
115                 'dvr_slug': display_id,
116             })
117
118         formats = self._extract_akamai_formats(video_info['url'], display_id)
119         self._sort_formats(formats)
120
121         return {
122             'id': display_id,
123             'display_id': display_id,
124             'title': video_info['headline'],
125             'thumbnail': video_info.get('thumbnail_url_hd') or video_info.get('thumbnail_url_sd'),
126             'duration': parse_duration(video_info.get('segmentDur')),
127             'formats': formats,
128         }