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