]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/nbc.py
bf5132721ecfe03d5ced4e637c512fe23ff6791c
[youtube-dl.git] / youtube_dl / extractor / nbc.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_str,
9     ExtractorError,
10     find_xpath_attr,
11 )
12
13
14 class NBCIE(InfoExtractor):
15     _VALID_URL = r'http://www\.nbc\.com/(?:[^/]+/)+(?P<id>n?\d+)'
16
17     _TESTS = [
18         {
19             'url': 'http://www.nbc.com/chicago-fire/video/i-am-a-firefighter/2734188',
20             # md5 checksum is not stable
21             'info_dict': {
22                 'id': 'bTmnLCvIbaaH',
23                 'ext': 'flv',
24                 'title': 'I Am a Firefighter',
25                 'description': 'An emergency puts Dawson\'sf irefighter skills to the ultimate test in this four-part digital series.',
26             },
27         },
28         {
29             'url': 'http://www.nbc.com/the-tonight-show/episodes/176',
30             'info_dict': {
31                 'id': 'XwU9KZkp98TH',
32                 'ext': 'flv',
33                 'title': 'Ricky Gervais, Steven Van Zandt, ILoveMakonnen',
34                 'description': 'A brand new episode of The Tonight Show welcomes Ricky Gervais, Steven Van Zandt and ILoveMakonnen.',
35             },
36             'skip': 'Only works from US',
37         },
38     ]
39
40     def _real_extract(self, url):
41         video_id = self._match_id(url)
42         webpage = self._download_webpage(url, video_id)
43         theplatform_url = self._search_regex(
44             '(?:class="video-player video-player-full" data-mpx-url|class="player" src)="(.*?)"',
45             webpage, 'theplatform url').replace('_no_endcard', '')
46         if theplatform_url.startswith('//'):
47             theplatform_url = 'http:' + theplatform_url
48         return self.url_result(theplatform_url)
49
50
51 class NBCNewsIE(InfoExtractor):
52     _VALID_URL = r'''(?x)https?://www\.nbcnews\.com/
53         ((video/.+?/(?P<id>\d+))|
54         (feature/[^/]+/(?P<title>.+)))
55         '''
56
57     _TESTS = [
58         {
59             'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
60             'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
61             'info_dict': {
62                 'id': '52753292',
63                 'ext': 'flv',
64                 'title': 'Crew emerges after four-month Mars food study',
65                 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
66             },
67         },
68         {
69             'url': 'http://www.nbcnews.com/feature/edward-snowden-interview/how-twitter-reacted-snowden-interview-n117236',
70             'md5': 'b2421750c9f260783721d898f4c42063',
71             'info_dict': {
72                 'id': 'I1wpAI_zmhsQ',
73                 'ext': 'mp4',
74                 'title': 'How Twitter Reacted To The Snowden Interview',
75                 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
76             },
77             'add_ie': ['ThePlatform'],
78         },
79     ]
80
81     def _real_extract(self, url):
82         mobj = re.match(self._VALID_URL, url)
83         video_id = mobj.group('id')
84         if video_id is not None:
85             all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
86             info = all_info.find('video')
87
88             return {
89                 'id': video_id,
90                 'title': info.find('headline').text,
91                 'ext': 'flv',
92                 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
93                 'description': compat_str(info.find('caption').text),
94                 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
95             }
96         else:
97             # "feature" pages use theplatform.com
98             title = mobj.group('title')
99             webpage = self._download_webpage(url, title)
100             bootstrap_json = self._search_regex(
101                 r'var bootstrapJson = ({.+})\s*$', webpage, 'bootstrap json',
102                 flags=re.MULTILINE)
103             bootstrap = json.loads(bootstrap_json)
104             info = bootstrap['results'][0]['video']
105             mpxid = info['mpxId']
106
107             base_urls = [
108                 info['fallbackPlaylistUrl'],
109                 info['associatedPlaylistUrl'],
110             ]
111
112             for base_url in base_urls:
113                 if not base_url:
114                     continue
115                 playlist_url = base_url + '?form=MPXNBCNewsAPI'
116                 all_videos = self._download_json(playlist_url, title)['videos']
117
118                 try:
119                     info = next(v for v in all_videos if v['mpxId'] == mpxid)
120                     break
121                 except StopIteration:
122                     continue
123
124             if info is None:
125                 raise ExtractorError('Could not find video in playlists')
126
127             return {
128                 '_type': 'url',
129                 # We get the best quality video
130                 'url': info['videoAssets'][-1]['publicUrl'],
131                 'ie_key': 'ThePlatform',
132             }