2 from __future__ import unicode_literals
6 from .common import InfoExtractor
16 class LibraryOfCongressIE(InfoExtractor):
18 IE_DESC = 'Library of Congress'
19 _VALID_URL = r'https?://(?:www\.)?loc\.gov/(?:item/|today/cyberlc/feature_wdesc\.php\?.*\brec=)(?P<id>[0-9a-z_.]+)'
21 # embedded via <div class="media-player"
22 'url': 'http://loc.gov/item/90716351/',
23 'md5': '6ec0ae8f07f86731b1b2ff70f046210a',
27 'title': "Pa's trip to Mars",
32 # webcast embedded via mediaObjectId
33 'url': 'https://www.loc.gov/today/cyberlc/feature_wdesc.php?rec=5578',
37 'title': 'Help! Preservation Training Needs Here, There & Everywhere',
40 'subtitles': 'mincount:1',
43 'skip_download': True,
46 # with direct download links
47 'url': 'https://www.loc.gov/item/78710669/',
51 'title': 'La vie et la passion de Jesus-Christ',
54 'formats': 'mincount:4',
57 'skip_download': True,
60 'url': 'https://www.loc.gov/item/ihas.200197114/',
61 'only_matching': True,
63 'url': 'https://www.loc.gov/item/afc1981005_afs20503/',
64 'only_matching': True,
67 def _real_extract(self, url):
68 video_id = self._match_id(url)
69 webpage = self._download_webpage(url, video_id)
71 media_id = self._search_regex(
72 (r'id=(["\'])media-player-(?P<id>.+?)\1',
73 r'<video[^>]+id=(["\'])uuid-(?P<id>.+?)\1',
74 r'<video[^>]+data-uuid=(["\'])(?P<id>.+?)\1',
75 r'mediaObjectId\s*:\s*(["\'])(?P<id>.+?)\1',
76 r'data-tab="share-media-(?P<id>[0-9A-F]{32})"'),
77 webpage, 'media id', group='id')
79 data = self._download_json(
80 'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id,
81 media_id)['mediaObject']
83 derivative = data['derivatives'][0]
84 media_url = derivative['derivativeUrl']
86 title = derivative.get('shortName') or data.get('shortName') or self._og_search_title(
89 # Following algorithm was extracted from setAVSource js function
91 media_url = media_url.replace('rtmp', 'https')
93 is_video = data.get('mediaType', 'v').lower() == 'v'
94 ext = determine_ext(media_url)
95 if ext not in ('mp4', 'mp3'):
96 media_url += '.mp4' if is_video else '.mp3'
99 if '/vod/mp4:' in media_url:
101 'url': media_url.replace('/vod/mp4:', '/hls-vod/media/') + '.m3u8',
104 'protocol': 'm3u8_native',
108 'url': re.sub(r'(://[^/]+/)(?:[^/]+/)*(?:mp4|mp3):', r'\1', media_url),
113 http_format['vcodec'] = 'none'
114 formats.append(http_format)
116 download_urls = set()
117 for m in re.finditer(
118 r'<option[^>]+value=(["\'])(?P<url>.+?)\1[^>]+data-file-download=[^>]+>\s*(?P<id>.+?)(?:(?: |\s+)\((?P<size>.+?)\))?\s*<', webpage):
119 format_id = m.group('id').lower()
120 if format_id in ('gif', 'jpeg'):
122 download_url = m.group('url')
123 if download_url in download_urls:
125 download_urls.add(download_url)
128 'format_id': format_id,
129 'filesize_approx': parse_filesize(m.group('size')),
132 self._sort_formats(formats)
134 duration = float_or_none(data.get('duration'))
135 view_count = int_or_none(data.get('viewCount'))
138 cc_url = data.get('ccUrl')
140 subtitles.setdefault('en', []).append({
148 'thumbnail': self._og_search_thumbnail(webpage, default=None),
149 'duration': duration,
150 'view_count': view_count,
152 'subtitles': subtitles,