]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/nbc.py
[generic] remove unused imports
[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/[^/]+/video/[^/]+/(?P<id>n?\d+)'
16
17     _TEST = {
18         'url': 'http://www.nbc.com/chicago-fire/video/i-am-a-firefighter/2734188',
19         'md5': '54d0fbc33e0b853a65d7b4de5c06d64e',
20         'info_dict': {
21             'id': 'u1RInQZRN7QJ',
22             'ext': 'flv',
23             'title': 'I Am a Firefighter',
24             'description': 'An emergency puts Dawson\'sf irefighter skills to the ultimate test in this four-part digital series.',
25         },
26     }
27
28     def _real_extract(self, url):
29         mobj = re.match(self._VALID_URL, url)
30         video_id = mobj.group('id')
31         webpage = self._download_webpage(url, video_id)
32         theplatform_url = self._search_regex('class="video-player video-player-full" data-mpx-url="(.*?)"', webpage, 'theplatform url')
33         if theplatform_url.startswith('//'):
34             theplatform_url = 'http:' + theplatform_url
35         return self.url_result(theplatform_url)
36
37
38 class NBCNewsIE(InfoExtractor):
39     _VALID_URL = r'''(?x)https?://www\.nbcnews\.com/
40         ((video/.+?/(?P<id>\d+))|
41         (feature/[^/]+/(?P<title>.+)))
42         '''
43
44     _TESTS = [
45         {
46             'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
47             'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
48             'info_dict': {
49                 'id': '52753292',
50                 'ext': 'flv',
51                 'title': 'Crew emerges after four-month Mars food study',
52                 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
53             },
54         },
55         {
56             'url': 'http://www.nbcnews.com/feature/edward-snowden-interview/how-twitter-reacted-snowden-interview-n117236',
57             'md5': 'b2421750c9f260783721d898f4c42063',
58             'info_dict': {
59                 'id': 'I1wpAI_zmhsQ',
60                 'ext': 'flv',
61                 'title': 'How Twitter Reacted To The Snowden Interview',
62                 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
63             },
64             'add_ie': ['ThePlatform'],
65         },
66     ]
67
68     def _real_extract(self, url):
69         mobj = re.match(self._VALID_URL, url)
70         video_id = mobj.group('id')
71         if video_id is not None:
72             all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
73             info = all_info.find('video')
74
75             return {
76                 'id': video_id,
77                 'title': info.find('headline').text,
78                 'ext': 'flv',
79                 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
80                 'description': compat_str(info.find('caption').text),
81                 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
82             }
83         else:
84             # "feature" pages use theplatform.com
85             title = mobj.group('title')
86             webpage = self._download_webpage(url, title)
87             bootstrap_json = self._search_regex(
88                 r'var bootstrapJson = ({.+})\s*$', webpage, 'bootstrap json',
89                 flags=re.MULTILINE)
90             bootstrap = json.loads(bootstrap_json)
91             info = bootstrap['results'][0]['video']
92             mpxid = info['mpxId']
93
94             base_urls = [
95                 info['fallbackPlaylistUrl'],
96                 info['associatedPlaylistUrl'],
97             ]
98
99             for base_url in base_urls:
100                 playlist_url = base_url + '?form=MPXNBCNewsAPI'
101                 all_videos = self._download_json(playlist_url, title)['videos']
102
103                 try:
104                     info = next(v for v in all_videos if v['mpxId'] == mpxid)
105                     break
106                 except StopIteration:
107                     continue
108
109             if info is None:
110                 raise ExtractorError('Could not find video in playlists')
111
112             return {
113                 '_type': 'url',
114                 # We get the best quality video
115                 'url': info['videoAssets'][-1]['publicUrl'],
116                 'ie_key': 'ThePlatform',
117             }