[nowness] add api abstration function adn extend _VALID_URL regex
authorremitamine <remitamine@gmail.com>
Sat, 5 Sep 2015 19:32:31 +0000 (20:32 +0100)
committerremitamine <remitamine@gmail.com>
Sat, 5 Sep 2015 19:32:31 +0000 (20:32 +0100)
youtube_dl/extractor/nowness.py

index 401d3b64616f1d4d6478f3e88203d5a902e428b4..04d614c9c2d91c504ca414c685ba0caf7a96ac9d 100644 (file)
@@ -30,10 +30,20 @@ class NownessBaseIE(InfoExtractor):
                     elif source == 'cinematique':
                         return self.url_result('http://cinematique.com/embed/%s' % video_id, 'Cinematique')
 
+    def api_request(self, url, request_url):
+        id = self._match_id(url)
+
+        lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us'
+        request = compat_urllib_request.Request(request_url % id, headers={
+            'X-Nowness-Language': lang,
+        })
+        json_data = self._download_json(request, id)
+        return id, json_data
+
 
 class NownessIE(NownessBaseIE):
     IE_NAME = 'nowness'
-    _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/(story|series/[^/]+)/(?P<id>[0-9a-z-]+)'
+    _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/(?:story|(?:series|category)/[^/]+)/(?P<id>[^/]+?)(?:$|[?#])'
     _TESTS = [
         {
             'url': 'https://www.nowness.com/story/candor-the-art-of-gesticulation',
@@ -62,19 +72,13 @@ class NownessIE(NownessBaseIE):
     ]
 
     def _real_extract(self, url):
-        display_id = self._match_id(url)
-
-        lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us'
-        request = compat_urllib_request.Request('http://api.nowness.com/api/post/getBySlug/%s' % display_id, headers={
-            'X-Nowness-Language': lang,
-        })
-        post = self._download_json(request, display_id)
+        display_id, post = self.api_request(url, 'http://api.nowness.com/api/post/getBySlug/%s')
         return self.extract_url_result(post)
 
 
 class NownessPlaylistIE(NownessBaseIE):
     IE_NAME = 'nowness:playlist'
-    _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/playlist/(?P<id>\d+)/[0-9a-z-]+'
+    _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/playlist/(?P<id>\d+)'
     _TEST = {
         'url': 'https://www.nowness.com/playlist/3286/i-guess-thats-why-they-call-it-the-blues',
         'info_dict':
@@ -85,20 +89,14 @@ class NownessPlaylistIE(NownessBaseIE):
     }
 
     def _real_extract(self, url):
-        playlist_id = self._match_id(url)
-
-        lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us'
-        request = compat_urllib_request.Request('http://api.nowness.com/api/post?PlaylistId=%s' % playlist_id, headers={
-            'X-Nowness-Language': lang,
-        })
-        playlist = self._download_json(request, playlist_id)
+        playlist_id, playlist = self.api_request(url, 'http://api.nowness.com/api/post?PlaylistId=%s')
         entries = [self.extract_url_result(item) for item in playlist['items']]
         return self.playlist_result(entries, playlist_id)
 
 
 class NownessSerieIE(NownessBaseIE):
     IE_NAME = 'nowness:serie'
-    _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/series/(?P<id>[0-9a-z-]+)'
+    _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/series/(?P<id>[^/]+?)(?:$|[?#])'
     _TEST = {
         'url': 'https://www.nowness.com/series/60-seconds',
         'info_dict':
@@ -109,13 +107,7 @@ class NownessSerieIE(NownessBaseIE):
     }
 
     def _real_extract(self, url):
-        display_id = self._match_id(url)
-
-        lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us'
-        request = compat_urllib_request.Request('http://api.nowness.com/api/series/getBySlug/%s' % display_id, headers={
-            'X-Nowness-Language': lang,
-        })
-        serie = self._download_json(request, display_id)
+        display_id, serie = self.api_request(url, 'http://api.nowness.com/api/series/getBySlug/%s')
         serie_id = str(serie['id'])
         entries = [self.extract_url_result(post) for post in serie['posts']]
         return self.playlist_result(entries, serie_id)