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