[cbsnews] Extract subtitles
[youtube-dl] / youtube_dl / extractor / cbsnews.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import json
6
7 from .common import InfoExtractor
8 from ..utils import remove_start
9
10
11 class CBSNewsIE(InfoExtractor):
12     IE_DESC = 'CBS News'
13     _VALID_URL = r'http://(?:www\.)?cbsnews\.com/(?:[^/]+/)+(?P<id>[\da-z_-]+)'
14
15     _TESTS = [
16         {
17             'url': 'http://www.cbsnews.com/news/tesla-and-spacex-elon-musks-industrial-empire/',
18             'info_dict': {
19                 'id': 'tesla-and-spacex-elon-musks-industrial-empire',
20                 'ext': 'flv',
21                 'title': 'Tesla and SpaceX: Elon Musk\'s industrial empire',
22                 'thumbnail': 'http://beta.img.cbsnews.com/i/2014/03/30/60147937-2f53-4565-ad64-1bdd6eb64679/60-0330-pelley-640x360.jpg',
23                 'duration': 791,
24             },
25             'params': {
26                 # rtmp download
27                 'skip_download': True,
28             },
29         },
30         {
31             'url': 'http://www.cbsnews.com/videos/fort-hood-shooting-army-downplays-mental-illness-as-cause-of-attack/',
32             'info_dict': {
33                 'id': 'fort-hood-shooting-army-downplays-mental-illness-as-cause-of-attack',
34                 'ext': 'flv',
35                 'title': 'Fort Hood shooting: Army downplays mental illness as cause of attack',
36                 'thumbnail': 're:^https?://.*\.jpg$',
37                 'duration': 205,
38             },
39             'params': {
40                 # rtmp download
41                 'skip_download': True,
42             },
43         },
44         {
45             'url': 'http://www.cbsnews.com/videos/mountain-lions-of-l-a/',
46             'info_dict': {
47                 'id': 'Mountain Lions of L.A.',
48                 'ext': 'flv',
49                 'title': 'Fort Hood shooting: Army downplays mental illness as cause of attack',
50                 'thumbnail': 're:^http?://.*\.jpg$',
51                 'subtitles': 're:^http?://.*\.xml$',
52                 'duration': 787,
53             },
54             'params': {
55                 # rtmp download
56                 'skip_download': True,
57             },
58         },
59     ]
60
61     def _real_extract(self, url):
62         mobj = re.match(self._VALID_URL, url)
63         video_id = mobj.group('id')
64
65         webpage = self._download_webpage(url, video_id)
66
67         video_info = json.loads(self._html_search_regex(
68             r'(?:<ul class="media-list items" id="media-related-items"><li data-video-info|<div id="cbsNewsVideoPlayer" data-video-player-options)=\'({.+?})\'',
69             webpage, 'video JSON info'))
70
71         item = video_info['item'] if 'item' in video_info else video_info
72         title = item.get('articleTitle') or item.get('hed')
73         duration = item.get('duration')
74         thumbnail = item.get('mediaImage') or item.get('thumbnail')
75
76         formats = []
77         for format_id in ['RtmpMobileLow', 'RtmpMobileHigh', 'Hls', 'RtmpDesktop']:
78             uri = item.get('media' + format_id + 'URI')
79             if not uri:
80                 continue
81             uri = remove_start(uri, '{manifest:none}')
82             fmt = {
83                 'url': uri,
84                 'format_id': format_id,
85             }
86             if uri.startswith('rtmp'):
87                 play_path = re.sub(
88                     r'{slistFilePath}', '',
89                     uri.split('<break>')[-1].split('{break}')[-1])
90                 play_path = re.sub(
91                     r'{manifest:.+}.*$', '', play_path)
92                 fmt.update({
93                     'app': 'ondemand?auth=cbs',
94                     'play_path': 'mp4:' + play_path,
95                     'player_url': 'http://www.cbsnews.com/[[IMPORT]]/vidtech.cbsinteractive.com/player/3_3_0/CBSI_PLAYER_HD.swf',
96                     'page_url': 'http://www.cbsnews.com',
97                     'ext': 'flv',
98                 })
99             elif uri.endswith('.m3u8'):
100                 fmt['ext'] = 'mp4'
101             formats.append(fmt)
102
103         if 'mpxRefId' in video_info:
104             cap_url = 'http://www.cbsnews.com/videos/captions/%s.adb_xml' % video_info['mpxRefId']
105             subtitles = {
106                 'en': [{
107                     'url': cap_url,
108                     'ext': 'xml'
109                 }], }
110         else:
111             subtitles = {}
112
113         return {
114             'id': video_id,
115             'title': title,
116             'thumbnail': thumbnail,
117             'duration': duration,
118             'formats': formats,
119             'subtitles': subtitles,
120         }