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': '353917ff7f0255aa6d4b80a034833de8',
27 'title': "Pa's trip to Mars",
28 'thumbnail': r're:^https?://.*\.jpg$',
33 # webcast embedded via mediaObjectId
34 'url': 'https://www.loc.gov/today/cyberlc/feature_wdesc.php?rec=5578',
38 'title': 'Help! Preservation Training Needs Here, There & Everywhere',
41 'subtitles': 'mincount:1',
44 'skip_download': True,
47 # with direct download links
48 'url': 'https://www.loc.gov/item/78710669/',
52 'title': 'La vie et la passion de Jesus-Christ',
55 'formats': 'mincount:4',
58 'skip_download': True,
61 'url': 'https://www.loc.gov/item/ihas.200197114/',
62 'only_matching': True,
64 'url': 'https://www.loc.gov/item/afc1981005_afs20503/',
65 'only_matching': True,
68 def _real_extract(self, url):
69 video_id = self._match_id(url)
70 webpage = self._download_webpage(url, video_id)
72 media_id = self._search_regex(
73 (r'id=(["\'])media-player-(?P<id>.+?)\1',
74 r'<video[^>]+id=(["\'])uuid-(?P<id>.+?)\1',
75 r'<video[^>]+data-uuid=(["\'])(?P<id>.+?)\1',
76 r'mediaObjectId\s*:\s*(["\'])(?P<id>.+?)\1',
77 r'data-tab="share-media-(?P<id>[0-9A-F]{32})"'),
78 webpage, 'media id', group='id')
80 data = self._download_json(
81 'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id,
82 media_id)['mediaObject']
84 derivative = data['derivatives'][0]
85 media_url = derivative['derivativeUrl']
87 title = derivative.get('shortName') or data.get('shortName') or self._og_search_title(
90 # Following algorithm was extracted from setAVSource js function
92 media_url = media_url.replace('rtmp', 'https')
94 is_video = data.get('mediaType', 'v').lower() == 'v'
95 ext = determine_ext(media_url)
96 if ext not in ('mp4', 'mp3'):
97 media_url += '.mp4' if is_video else '.mp3'
100 if '/vod/mp4:' in media_url:
102 'url': media_url.replace('/vod/mp4:', '/hls-vod/media/') + '.m3u8',
105 'protocol': 'm3u8_native',
109 'url': re.sub(r'(://[^/]+/)(?:[^/]+/)*(?:mp4|mp3):', r'\1', media_url),
114 http_format['vcodec'] = 'none'
115 formats.append(http_format)
117 download_urls = set()
118 for m in re.finditer(
119 r'<option[^>]+value=(["\'])(?P<url>.+?)\1[^>]+data-file-download=[^>]+>\s*(?P<id>.+?)(?:(?: |\s+)\((?P<size>.+?)\))?\s*<', webpage):
120 format_id = m.group('id').lower()
121 if format_id in ('gif', 'jpeg'):
123 download_url = m.group('url')
124 if download_url in download_urls:
126 download_urls.add(download_url)
129 'format_id': format_id,
130 'filesize_approx': parse_filesize(m.group('size')),
133 self._sort_formats(formats)
135 duration = float_or_none(data.get('duration'))
136 view_count = int_or_none(data.get('viewCount'))
139 cc_url = data.get('ccUrl')
141 subtitles.setdefault('en', []).append({
149 'thumbnail': self._og_search_thumbnail(webpage, default=None),
150 'duration': duration,
151 'view_count': view_count,
153 'subtitles': subtitles,