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