[loc] Extract subtites
[youtube-dl] / youtube_dl / extractor / libraryofcongress.py
index d311f994624d349d6c3e089753c9f01898bd6ec2..49351759ed318bda771a6648f7f33744fe62c4b7 100644 (file)
@@ -13,8 +13,9 @@ from ..utils import (
 class LibraryOfCongressIE(InfoExtractor):
     IE_NAME = 'loc'
     IE_DESC = 'Library of Congress'
-    _VALID_URL = r'https?://(?:www\.)?loc\.gov/item/(?P<id>[0-9]+)'
-    _TEST = {
+    _VALID_URL = r'https?://(?:www\.)?loc\.gov/(?:item/|today/cyberlc/feature_wdesc\.php\?.*\brec=)(?P<id>[0-9]+)'
+    _TESTS = [{
+        # embedded via <div class="media-player"
         'url': 'http://loc.gov/item/90716351/',
         'md5': '353917ff7f0255aa6d4b80a034833de8',
         'info_dict': {
@@ -25,7 +26,21 @@ class LibraryOfCongressIE(InfoExtractor):
             'duration': 0,
             'view_count': int,
         },
-    }
+    }, {
+        # webcast embedded via mediaObjectId
+        'url': 'https://www.loc.gov/today/cyberlc/feature_wdesc.php?rec=5578',
+        'info_dict': {
+            'id': '5578',
+            'ext': 'mp4',
+            'title': 'Help! Preservation Training Needs Here, There & Everywhere',
+            'duration': 3765,
+            'view_count': int,
+            'subtitles': 'mincount:1',
+        },
+        'params': {
+            'skip_download': True,
+        },
+    }]
 
     def _real_extract(self, url):
         video_id = self._match_id(url)
@@ -34,13 +49,12 @@ class LibraryOfCongressIE(InfoExtractor):
         media_id = self._search_regex(
             (r'id=(["\'])media-player-(?P<id>.+?)\1',
              r'<video[^>]+id=(["\'])uuid-(?P<id>.+?)\1',
-             r'<video[^>]+data-uuid=(["\'])(?P<id>.+?)\1'),
+             r'<video[^>]+data-uuid=(["\'])(?P<id>.+?)\1',
+             r'mediaObjectId\s*:\s*(["\'])(?P<id>.+?)\1'),
             webpage, 'media id', group='id')
 
-        data = self._parse_json(
-            self._download_webpage(
-                'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id,
-                video_id),
+        data = self._download_json(
+            'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id,
             video_id)['mediaObject']
 
         derivative = data['derivatives'][0]
@@ -74,11 +88,20 @@ class LibraryOfCongressIE(InfoExtractor):
         duration = float_or_none(data.get('duration'))
         view_count = int_or_none(data.get('viewCount'))
 
+        subtitles = {}
+        cc_url = data.get('ccUrl')
+        if cc_url:
+            subtitles.setdefault('en', []).append({
+                'url': cc_url,
+                'ext': 'ttml',
+            })
+
         return {
             'id': video_id,
             'title': title,
-            'thumbnail': self._og_search_thumbnail(webpage),
+            'thumbnail': self._og_search_thumbnail(webpage, default=None),
             'duration': duration,
             'view_count': view_count,
             'formats': formats,
+            'subtitles': subtitles,
         }