[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / libraryofcongress.py
blob6185605744ca8a0eee19d52ffd2e9f819a1c632a
1 import re
3 from .common import InfoExtractor
4 from ..utils import (
5 determine_ext,
6 float_or_none,
7 int_or_none,
8 parse_filesize,
12 class LibraryOfCongressIE(InfoExtractor):
13 IE_NAME = 'loc'
14 IE_DESC = 'Library of Congress'
15 _VALID_URL = r'https?://(?:www\.)?loc\.gov/(?:item/|today/cyberlc/feature_wdesc\.php\?.*\brec=)(?P<id>[0-9a-z_.]+)'
16 _TESTS = [{
17 # embedded via <div class="media-player"
18 'url': 'http://loc.gov/item/90716351/',
19 'md5': '6ec0ae8f07f86731b1b2ff70f046210a',
20 'info_dict': {
21 'id': '90716351',
22 'ext': 'mp4',
23 'title': "Pa's trip to Mars",
24 'duration': 0,
25 'view_count': int,
27 }, {
28 # webcast embedded via mediaObjectId
29 'url': 'https://www.loc.gov/today/cyberlc/feature_wdesc.php?rec=5578',
30 'info_dict': {
31 'id': '5578',
32 'ext': 'mp4',
33 'title': 'Help! Preservation Training Needs Here, There & Everywhere',
34 'duration': 3765,
35 'view_count': int,
36 'subtitles': 'mincount:1',
38 'params': {
39 'skip_download': True,
41 }, {
42 # with direct download links
43 'url': 'https://www.loc.gov/item/78710669/',
44 'info_dict': {
45 'id': '78710669',
46 'ext': 'mp4',
47 'title': 'La vie et la passion de Jesus-Christ',
48 'duration': 0,
49 'view_count': int,
50 'formats': 'mincount:4',
52 'params': {
53 'skip_download': True,
55 }, {
56 'url': 'https://www.loc.gov/item/ihas.200197114/',
57 'only_matching': True,
58 }, {
59 'url': 'https://www.loc.gov/item/afc1981005_afs20503/',
60 'only_matching': True,
63 def _real_extract(self, url):
64 video_id = self._match_id(url)
65 webpage = self._download_webpage(url, video_id)
67 media_id = self._search_regex(
68 (r'id=(["\'])media-player-(?P<id>.+?)\1',
69 r'<video[^>]+id=(["\'])uuid-(?P<id>.+?)\1',
70 r'<video[^>]+data-uuid=(["\'])(?P<id>.+?)\1',
71 r'mediaObjectId\s*:\s*(["\'])(?P<id>.+?)\1',
72 r'data-tab="share-media-(?P<id>[0-9A-F]{32})"'),
73 webpage, 'media id', group='id')
75 data = self._download_json(
76 f'https://media.loc.gov/services/v1/media?id={media_id}&context=json',
77 media_id)['mediaObject']
79 derivative = data['derivatives'][0]
80 media_url = derivative['derivativeUrl']
82 title = derivative.get('shortName') or data.get('shortName') or self._og_search_title(
83 webpage)
85 # Following algorithm was extracted from setAVSource js function
86 # found in webpage
87 media_url = media_url.replace('rtmp', 'https')
89 is_video = data.get('mediaType', 'v').lower() == 'v'
90 ext = determine_ext(media_url)
91 if ext not in ('mp4', 'mp3'):
92 media_url += '.mp4' if is_video else '.mp3'
94 formats = []
95 if '/vod/mp4:' in media_url:
96 formats.append({
97 'url': media_url.replace('/vod/mp4:', '/hls-vod/media/') + '.m3u8',
98 'format_id': 'hls',
99 'ext': 'mp4',
100 'protocol': 'm3u8_native',
101 'quality': 1,
103 http_format = {
104 'url': re.sub(r'(://[^/]+/)(?:[^/]+/)*(?:mp4|mp3):', r'\1', media_url),
105 'format_id': 'http',
106 'quality': 1,
108 if not is_video:
109 http_format['vcodec'] = 'none'
110 formats.append(http_format)
112 download_urls = set()
113 for m in re.finditer(
114 r'<option[^>]+value=(["\'])(?P<url>.+?)\1[^>]+data-file-download=[^>]+>\s*(?P<id>.+?)(?:(?:&nbsp;|\s+)\((?P<size>.+?)\))?\s*<', webpage):
115 format_id = m.group('id').lower()
116 if format_id in ('gif', 'jpeg'):
117 continue
118 download_url = m.group('url')
119 if download_url in download_urls:
120 continue
121 download_urls.add(download_url)
122 formats.append({
123 'url': download_url,
124 'format_id': format_id,
125 'filesize_approx': parse_filesize(m.group('size')),
128 duration = float_or_none(data.get('duration'))
129 view_count = int_or_none(data.get('viewCount'))
131 subtitles = {}
132 cc_url = data.get('ccUrl')
133 if cc_url:
134 subtitles.setdefault('en', []).append({
135 'url': cc_url,
136 'ext': 'ttml',
139 return {
140 'id': video_id,
141 'title': title,
142 'thumbnail': self._og_search_thumbnail(webpage, default=None),
143 'duration': duration,
144 'view_count': view_count,
145 'formats': formats,
146 'subtitles': subtitles,