]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/nbc.py
Fix imports and general cleanup
[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 ..compat import (
8     compat_str,
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/[^/]+/(?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
83     def _real_extract(self, url):
84         mobj = re.match(self._VALID_URL, url)
85         video_id = mobj.group('id')
86         if video_id is not None:
87             all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
88             info = all_info.find('video')
89
90             return {
91                 'id': video_id,
92                 'title': info.find('headline').text,
93                 'ext': 'flv',
94                 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
95                 'description': compat_str(info.find('caption').text),
96                 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
97             }
98         else:
99             # "feature" pages use theplatform.com
100             title = mobj.group('title')
101             webpage = self._download_webpage(url, title)
102             bootstrap_json = self._search_regex(
103                 r'var bootstrapJson = ({.+})\s*$', webpage, 'bootstrap json',
104                 flags=re.MULTILINE)
105             bootstrap = json.loads(bootstrap_json)
106             info = bootstrap['results'][0]['video']
107             mpxid = info['mpxId']
108
109             base_urls = [
110                 info['fallbackPlaylistUrl'],
111                 info['associatedPlaylistUrl'],
112             ]
113
114             for base_url in base_urls:
115                 if not base_url:
116                     continue
117                 playlist_url = base_url + '?form=MPXNBCNewsAPI'
118                 all_videos = self._download_json(playlist_url, title)['videos']
119
120                 try:
121                     info = next(v for v in all_videos if v['mpxId'] == mpxid)
122                     break
123                 except StopIteration:
124                     continue
125
126             if info is None:
127                 raise ExtractorError('Could not find video in playlists')
128
129             return {
130                 '_type': 'url',
131                 # We get the best quality video
132                 'url': info['videoAssets'][-1]['publicUrl'],
133                 'ie_key': 'ThePlatform',
134             }