[NBCSports] Add a test case for extended _VALID_URL
[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'https?://www\.nbc\.com/(?:[^/]+/)+(?P<id>n?\d+)'
18
19     _TESTS = [
20         {
21             'url': 'http://www.nbc.com/the-tonight-show/segments/112966',
22             # md5 checksum is not stable
23             'info_dict': {
24                 'id': 'c9xnCo0YPOPH',
25                 'ext': 'flv',
26                 'title': 'Jimmy Fallon Surprises Fans at Ben & Jerry\'s',
27                 'description': 'Jimmy gives out free scoops of his new "Tonight Dough" ice cream flavor by surprising customers at the Ben & Jerry\'s scoop shop.',
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 NBCSportsVPlayerIE(InfoExtractor):
54     _VALID_URL = r'https?://vplayer\.nbcsports\.com/(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
55
56     _TESTS = [{
57         'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_share/select/9CsDKds0kvHI',
58         'md5': 'ceae8dced5c14a1c1ffcb7a32194cca5',
59         'info_dict': {
60             'id': '9CsDKds0kvHI',
61             'ext': 'flv',
62             'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
63             'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
64         }
65     }, {
66         'note': 'This video is already expired. It\'s for testing _VALID_URL',
67         'url': 'http://vplayer.nbcsports.com/p/BxmELC/nbc_embedshare/select/_hqLjQ95yx8Z',
68         'only_matching': True,
69     }]
70
71     @staticmethod
72     def _extract_url(webpage):
73         iframe_m = re.search(
74             r'<iframe[^>]+src="(?P<url>https?://vplayer\.nbcsports\.com/[^"]+)"', webpage)
75         if iframe_m:
76             return iframe_m.group('url')
77
78     def _real_extract(self, url):
79         video_id = self._match_id(url)
80         webpage = self._download_webpage(url, video_id)
81         theplatform_url = self._og_search_video_url(webpage)
82         return self.url_result(theplatform_url, 'ThePlatform')
83
84
85 class NBCSportsIE(InfoExtractor):
86     # Does not include https becuase its certificate is invalid
87     _VALID_URL = r'http://www\.nbcsports\.com//?(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
88
89     _TEST = {
90         'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
91         'md5': 'ba6c93f96b67bf05344f78bd523dac0f',
92         'info_dict': {
93             'id': 'PHJSaFWbrTY9',
94             'ext': 'flv',
95             'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
96             'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
97         }
98     }
99
100     def _real_extract(self, url):
101         video_id = self._match_id(url)
102         webpage = self._download_webpage(url, video_id)
103         return self.url_result(
104             NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
105
106
107 class NBCNewsIE(InfoExtractor):
108     _VALID_URL = r'''(?x)https?://(?:www\.)?nbcnews\.com/
109         (?:video/.+?/(?P<id>\d+)|
110         (?:feature|nightly-news)/[^/]+/(?P<title>.+))
111         '''
112
113     _TESTS = [
114         {
115             'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
116             'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
117             'info_dict': {
118                 'id': '52753292',
119                 'ext': 'flv',
120                 'title': 'Crew emerges after four-month Mars food study',
121                 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
122             },
123         },
124         {
125             'url': 'http://www.nbcnews.com/feature/edward-snowden-interview/how-twitter-reacted-snowden-interview-n117236',
126             'md5': 'b2421750c9f260783721d898f4c42063',
127             'info_dict': {
128                 'id': 'I1wpAI_zmhsQ',
129                 'ext': 'mp4',
130                 'title': 'How Twitter Reacted To The Snowden Interview',
131                 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
132             },
133             'add_ie': ['ThePlatform'],
134         },
135         {
136             'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
137             'md5': 'fdbf39ab73a72df5896b6234ff98518a',
138             'info_dict': {
139                 'id': 'Wjf9EDR3A_60',
140                 'ext': 'mp4',
141                 'title': 'FULL EPISODE: Family Business',
142                 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
143             },
144         },
145         {
146             'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
147             'md5': 'b5dda8cddd8650baa0dcb616dd2cf60d',
148             'info_dict': {
149                 'id': 'sekXqyTVnmN3',
150                 'ext': 'mp4',
151                 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
152                 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
153             },
154         },
155     ]
156
157     def _real_extract(self, url):
158         mobj = re.match(self._VALID_URL, url)
159         video_id = mobj.group('id')
160         if video_id is not None:
161             all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
162             info = all_info.find('video')
163
164             return {
165                 'id': video_id,
166                 'title': info.find('headline').text,
167                 'ext': 'flv',
168                 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
169                 'description': compat_str(info.find('caption').text),
170                 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
171             }
172         else:
173             # "feature" and "nightly-news" pages use theplatform.com
174             title = mobj.group('title')
175             webpage = self._download_webpage(url, title)
176             bootstrap_json = self._search_regex(
177                 r'var\s+(?:bootstrapJson|playlistData)\s*=\s*({.+});?\s*$',
178                 webpage, 'bootstrap json', flags=re.MULTILINE)
179             bootstrap = self._parse_json(bootstrap_json, video_id)
180             info = bootstrap['results'][0]['video']
181             mpxid = info['mpxId']
182
183             base_urls = [
184                 info['fallbackPlaylistUrl'],
185                 info['associatedPlaylistUrl'],
186             ]
187
188             for base_url in base_urls:
189                 if not base_url:
190                     continue
191                 playlist_url = base_url + '?form=MPXNBCNewsAPI'
192
193                 try:
194                     all_videos = self._download_json(playlist_url, title)
195                 except ExtractorError as ee:
196                     if isinstance(ee.cause, compat_HTTPError):
197                         continue
198                     raise
199
200                 if not all_videos or 'videos' not in all_videos:
201                     continue
202
203                 try:
204                     info = next(v for v in all_videos['videos'] if v['mpxId'] == mpxid)
205                     break
206                 except StopIteration:
207                     continue
208
209             if info is None:
210                 raise ExtractorError('Could not find video in playlists')
211
212             return {
213                 '_type': 'url',
214                 # We get the best quality video
215                 'url': info['videoAssets'][-1]['publicUrl'],
216                 'ie_key': 'ThePlatform',
217             }