Merge branch 'lecture2go' of https://github.com/nichdu/youtube-dl into nichdu-lecture2go
[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
9
10 class CBSNewsIE(InfoExtractor):
11     IE_DESC = 'CBS News'
12     _VALID_URL = r'http://(?:www\.)?cbsnews\.com/(?:[^/]+/)+(?P<id>[\da-z_-]+)'
13
14     _TESTS = [
15         {
16             'url': 'http://www.cbsnews.com/news/tesla-and-spacex-elon-musks-industrial-empire/',
17             'info_dict': {
18                 'id': 'tesla-and-spacex-elon-musks-industrial-empire',
19                 'ext': 'flv',
20                 'title': 'Tesla and SpaceX: Elon Musk\'s industrial empire',
21                 'thumbnail': 'http://beta.img.cbsnews.com/i/2014/03/30/60147937-2f53-4565-ad64-1bdd6eb64679/60-0330-pelley-640x360.jpg',
22                 'duration': 791,
23             },
24             'params': {
25                 # rtmp download
26                 'skip_download': True,
27             },
28         },
29         {
30             'url': 'http://www.cbsnews.com/videos/fort-hood-shooting-army-downplays-mental-illness-as-cause-of-attack/',
31             'info_dict': {
32                 'id': 'fort-hood-shooting-army-downplays-mental-illness-as-cause-of-attack',
33                 'ext': 'flv',
34                 'title': 'Fort Hood shooting: Army downplays mental illness as cause of attack',
35                 'thumbnail': 're:^https?://.*\.jpg$',
36                 'duration': 205,
37             },
38             'params': {
39                 # rtmp download
40                 'skip_download': True,
41             },
42         },
43     ]
44
45     def _real_extract(self, url):
46         mobj = re.match(self._VALID_URL, url)
47         video_id = mobj.group('id')
48
49         webpage = self._download_webpage(url, video_id)
50
51         video_info = json.loads(self._html_search_regex(
52             r'(?:<ul class="media-list items" id="media-related-items"><li data-video-info|<div id="cbsNewsVideoPlayer" data-video-player-options)=\'({.+?})\'',
53             webpage, 'video JSON info'))
54
55         item = video_info['item'] if 'item' in video_info else video_info
56         title = item.get('articleTitle') or item.get('hed')
57         duration = item.get('duration')
58         thumbnail = item.get('mediaImage') or item.get('thumbnail')
59
60         formats = []
61         for format_id in ['RtmpMobileLow', 'RtmpMobileHigh', 'Hls', 'RtmpDesktop']:
62             uri = item.get('media' + format_id + 'URI')
63             if not uri:
64                 continue
65             fmt = {
66                 'url': uri,
67                 'format_id': format_id,
68             }
69             if uri.startswith('rtmp'):
70                 fmt.update({
71                     'app': 'ondemand?auth=cbs',
72                     'play_path': 'mp4:' + uri.split('<break>')[-1],
73                     'player_url': 'http://www.cbsnews.com/[[IMPORT]]/vidtech.cbsinteractive.com/player/3_3_0/CBSI_PLAYER_HD.swf',
74                     'page_url': 'http://www.cbsnews.com',
75                     'ext': 'flv',
76                 })
77             elif uri.endswith('.m3u8'):
78                 fmt['ext'] = 'mp4'
79             formats.append(fmt)
80
81         return {
82             'id': video_id,
83             'title': title,
84             'thumbnail': thumbnail,
85             'duration': duration,
86             'formats': formats,
87         }