[loc] relax _VALID_URL regex and improve formats extraction

This commit is contained in:
Remita Amine 2018-11-21 09:44:10 +01:00
parent 183417a50f
commit 6c882aa899

View file

@ -16,7 +16,7 @@
class LibraryOfCongressIE(InfoExtractor): class LibraryOfCongressIE(InfoExtractor):
IE_NAME = 'loc' IE_NAME = 'loc'
IE_DESC = 'Library of Congress' IE_DESC = 'Library of Congress'
_VALID_URL = r'https?://(?:www\.)?loc\.gov/(?:item/|today/cyberlc/feature_wdesc\.php\?.*\brec=)(?P<id>[0-9]+)' _VALID_URL = r'https?://(?:www\.)?loc\.gov/(?:item/|today/cyberlc/feature_wdesc\.php\?.*\brec=)(?P<id>[0-9a-z_.]+)'
_TESTS = [{ _TESTS = [{
# embedded via <div class="media-player" # embedded via <div class="media-player"
'url': 'http://loc.gov/item/90716351/', 'url': 'http://loc.gov/item/90716351/',
@ -57,6 +57,12 @@ class LibraryOfCongressIE(InfoExtractor):
'params': { 'params': {
'skip_download': True, 'skip_download': True,
}, },
}, {
'url': 'https://www.loc.gov/item/ihas.200197114/',
'only_matching': True,
}, {
'url': 'https://www.loc.gov/item/afc1981005_afs20503/',
'only_matching': True,
}] }]
def _real_extract(self, url): def _real_extract(self, url):
@ -67,12 +73,13 @@ def _real_extract(self, url):
(r'id=(["\'])media-player-(?P<id>.+?)\1', (r'id=(["\'])media-player-(?P<id>.+?)\1',
r'<video[^>]+id=(["\'])uuid-(?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'), r'mediaObjectId\s*:\s*(["\'])(?P<id>.+?)\1',
r'data-tab="share-media-(?P<id>[0-9A-F]{32})"'),
webpage, 'media id', group='id') webpage, 'media id', group='id')
data = self._download_json( data = self._download_json(
'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id, 'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id,
video_id)['mediaObject'] media_id)['mediaObject']
derivative = data['derivatives'][0] derivative = data['derivatives'][0]
media_url = derivative['derivativeUrl'] media_url = derivative['derivativeUrl']
@ -89,25 +96,29 @@ def _real_extract(self, url):
if ext not in ('mp4', 'mp3'): if ext not in ('mp4', 'mp3'):
media_url += '.mp4' if is_video else '.mp3' media_url += '.mp4' if is_video else '.mp3'
if 'vod/mp4:' in media_url: formats = []
formats = [{ if '/vod/mp4:' in media_url:
'url': media_url.replace('vod/mp4:', 'hls-vod/media/') + '.m3u8', formats.append({
'url': media_url.replace('/vod/mp4:', '/hls-vod/media/') + '.m3u8',
'format_id': 'hls', 'format_id': 'hls',
'ext': 'mp4', 'ext': 'mp4',
'protocol': 'm3u8_native', 'protocol': 'm3u8_native',
'quality': 1, 'quality': 1,
}] })
elif 'vod/mp3:' in media_url: http_format = {
formats = [{ 'url': re.sub(r'(://[^/]+/)(?:[^/]+/)*(?:mp4|mp3):', r'\1', media_url),
'url': media_url.replace('vod/mp3:', ''), 'format_id': 'http',
'vcodec': 'none', 'quality': 1,
}] }
if not is_video:
http_format['vcodec'] = 'none'
formats.append(http_format)
download_urls = set() download_urls = set()
for m in re.finditer( for m in re.finditer(
r'<option[^>]+value=(["\'])(?P<url>.+?)\1[^>]+data-file-download=[^>]+>\s*(?P<id>.+?)(?:(?:&nbsp;|\s+)\((?P<size>.+?)\))?\s*<', webpage): r'<option[^>]+value=(["\'])(?P<url>.+?)\1[^>]+data-file-download=[^>]+>\s*(?P<id>.+?)(?:(?:&nbsp;|\s+)\((?P<size>.+?)\))?\s*<', webpage):
format_id = m.group('id').lower() format_id = m.group('id').lower()
if format_id == 'gif': if format_id in ('gif', 'jpeg'):
continue continue
download_url = m.group('url') download_url = m.group('url')
if download_url in download_urls: if download_url in download_urls: