[youtube] fix hd720 format position
[youtube-dl] / youtube_dl / extractor / nationalgeographic.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from .adobepass import AdobePassIE
7 from .theplatform import ThePlatformIE
8 from ..utils import (
9     smuggle_url,
10     url_basename,
11     update_url_query,
12     get_element_by_class,
13 )
14
15
16 class NationalGeographicVideoIE(InfoExtractor):
17     IE_NAME = 'natgeo:video'
18     _VALID_URL = r'https?://video\.nationalgeographic\.com/.*?'
19
20     _TESTS = [
21         {
22             'url': 'http://video.nationalgeographic.com/video/news/150210-news-crab-mating-vin?source=featuredvideo',
23             'md5': '730855d559abbad6b42c2be1fa584917',
24             'info_dict': {
25                 'id': '0000014b-70a1-dd8c-af7f-f7b559330001',
26                 'ext': 'mp4',
27                 'title': 'Mating Crabs Busted by Sharks',
28                 'description': 'md5:16f25aeffdeba55aaa8ec37e093ad8b3',
29                 'timestamp': 1423523799,
30                 'upload_date': '20150209',
31                 'uploader': 'NAGS',
32             },
33             'add_ie': ['ThePlatform'],
34         },
35         {
36             'url': 'http://video.nationalgeographic.com/wild/when-sharks-attack/the-real-jaws',
37             'md5': '6a3105eb448c070503b3105fb9b320b5',
38             'info_dict': {
39                 'id': 'ngc-I0IauNSWznb_UV008GxSbwY35BZvgi2e',
40                 'ext': 'mp4',
41                 'title': 'The Real Jaws',
42                 'description': 'md5:8d3e09d9d53a85cd397b4b21b2c77be6',
43                 'timestamp': 1433772632,
44                 'upload_date': '20150608',
45                 'uploader': 'NAGS',
46             },
47             'add_ie': ['ThePlatform'],
48         },
49     ]
50
51     def _real_extract(self, url):
52         name = url_basename(url)
53
54         webpage = self._download_webpage(url, name)
55         guid = self._search_regex(
56             r'id="(?:videoPlayer|player-container)"[^>]+data-guid="([^"]+)"',
57             webpage, 'guid')
58
59         return {
60             '_type': 'url_transparent',
61             'ie_key': 'ThePlatform',
62             'url': smuggle_url(
63                 'http://link.theplatform.com/s/ngs/media/guid/2423130747/%s?mbr=true' % guid,
64                 {'force_smil_url': True}),
65             'id': guid,
66         }
67
68
69 class NationalGeographicIE(ThePlatformIE, AdobePassIE):
70     IE_NAME = 'natgeo'
71     _VALID_URL = r'https?://channel\.nationalgeographic\.com/(?:(?:(?:wild/)?[^/]+/)?(?:videos|episodes)|u)/(?P<id>[^/?]+)'
72
73     _TESTS = [
74         {
75             'url': 'http://channel.nationalgeographic.com/u/kdi9Ld0PN2molUUIMSBGxoeDhD729KRjQcnxtetilWPMevo8ZwUBIDuPR0Q3D2LVaTsk0MPRkRWDB8ZhqWVeyoxfsZZm36yRp1j-zPfsHEyI_EgAeFY/',
76             'md5': '518c9aa655686cf81493af5cc21e2a04',
77             'info_dict': {
78                 'id': 'vKInpacll2pC',
79                 'ext': 'mp4',
80                 'title': 'Uncovering a Universal Knowledge',
81                 'description': 'md5:1a89148475bf931b3661fcd6ddb2ae3a',
82                 'timestamp': 1458680907,
83                 'upload_date': '20160322',
84                 'uploader': 'NEWA-FNG-NGTV',
85             },
86             'add_ie': ['ThePlatform'],
87         },
88         {
89             'url': 'http://channel.nationalgeographic.com/u/kdvOstqYaBY-vSBPyYgAZRUL4sWUJ5XUUPEhc7ISyBHqoIO4_dzfY3K6EjHIC0hmFXoQ7Cpzm6RkET7S3oMlm6CFnrQwSUwo/',
90             'md5': 'c4912f656b4cbe58f3e000c489360989',
91             'info_dict': {
92                 'id': 'Pok5lWCkiEFA',
93                 'ext': 'mp4',
94                 'title': 'The Stunning Red Bird of Paradise',
95                 'description': 'md5:7bc8cd1da29686be4d17ad1230f0140c',
96                 'timestamp': 1459362152,
97                 'upload_date': '20160330',
98                 'uploader': 'NEWA-FNG-NGTV',
99             },
100             'add_ie': ['ThePlatform'],
101         },
102         {
103             'url': 'http://channel.nationalgeographic.com/the-story-of-god-with-morgan-freeman/episodes/the-power-of-miracles/',
104             'only_matching': True,
105         },
106         {
107             'url': 'http://channel.nationalgeographic.com/videos/treasures-rediscovered/',
108             'only_matching': True,
109         },
110         {
111             'url': 'http://channel.nationalgeographic.com/the-story-of-god-with-morgan-freeman/videos/uncovering-a-universal-knowledge/',
112             'only_matching': True,
113         },
114         {
115             'url': 'http://channel.nationalgeographic.com/wild/destination-wild/videos/the-stunning-red-bird-of-paradise/',
116             'only_matching': True,
117         }
118     ]
119
120     def _real_extract(self, url):
121         display_id = self._match_id(url)
122         webpage = self._download_webpage(url, display_id)
123         release_url = self._search_regex(
124             r'video_auth_playlist_url\s*=\s*"([^"]+)"',
125             webpage, 'release url')
126         theplatform_path = self._search_regex(r'https?://link\.theplatform\.com/s/([^?]+)', release_url, 'theplatform path')
127         video_id = theplatform_path.split('/')[-1]
128         query = {
129             'mbr': 'true',
130         }
131         is_auth = self._search_regex(r'video_is_auth\s*=\s*"([^"]+)"', webpage, 'is auth', fatal=False)
132         if is_auth == 'auth':
133             auth_resource_id = self._search_regex(
134                 r"video_auth_resourceId\s*=\s*'([^']+)'",
135                 webpage, 'auth resource id')
136             query['auth'] = self._extract_mvpd_auth(url, video_id, 'natgeo', auth_resource_id)
137
138         formats = []
139         subtitles = {}
140         for key, value in (('switch', 'http'), ('manifest', 'm3u')):
141             tp_query = query.copy()
142             tp_query.update({
143                 key: value,
144             })
145             tp_formats, tp_subtitles = self._extract_theplatform_smil(
146                 update_url_query(release_url, tp_query), video_id, 'Downloading %s SMIL data' % value)
147             formats.extend(tp_formats)
148             subtitles = self._merge_subtitles(subtitles, tp_subtitles)
149         self._sort_formats(formats)
150
151         info = self._extract_theplatform_metadata(theplatform_path, display_id)
152         info.update({
153             'id': video_id,
154             'formats': formats,
155             'subtitles': subtitles,
156             'display_id': display_id,
157         })
158         return info
159
160
161 class NationalGeographicEpisodeGuideIE(InfoExtractor):
162     IE_NAME = 'natgeo:episodeguide'
163     _VALID_URL = r'https?://channel\.nationalgeographic\.com/(?:wild/)?(?P<id>[^/]+)/episode-guide'
164     _TESTS = [
165         {
166             'url': 'http://channel.nationalgeographic.com/the-story-of-god-with-morgan-freeman/episode-guide/',
167             'info_dict': {
168                 'id': 'the-story-of-god-with-morgan-freeman-season-1',
169                 'title': 'The Story of God with Morgan Freeman - Season 1',
170             },
171             'playlist_mincount': 6,
172         },
173         {
174             'url': 'http://channel.nationalgeographic.com/underworld-inc/episode-guide/?s=2',
175             'info_dict': {
176                 'id': 'underworld-inc-season-2',
177                 'title': 'Underworld, Inc. - Season 2',
178             },
179             'playlist_mincount': 7,
180         },
181     ]
182
183     def _real_extract(self, url):
184         display_id = self._match_id(url)
185         webpage = self._download_webpage(url, display_id)
186         show = get_element_by_class('show', webpage)
187         selected_season = self._search_regex(
188             r'<div[^>]+class="select-seasons[^"]*".*?<a[^>]*>(.*?)</a>',
189             webpage, 'selected season')
190         entries = [
191             self.url_result(self._proto_relative_url(entry_url), 'NationalGeographic')
192             for entry_url in re.findall('(?s)<div[^>]+class="col-inner"[^>]*?>.*?<a[^>]+href="([^"]+)"', webpage)]
193         return self.playlist_result(
194             entries, '%s-%s' % (display_id, selected_season.lower().replace(' ', '-')),
195             '%s - %s' % (show, selected_season))