]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/libraryofcongress.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / libraryofcongress.py
CommitLineData
4d8856d5
S
1import re
2
9c3c447e 3from .common import InfoExtractor
7f3c3dfa
S
4from ..utils import (
5 determine_ext,
6 float_or_none,
7 int_or_none,
4d8856d5 8 parse_filesize,
7f3c3dfa 9)
9c3c447e
T
10
11
12class LibraryOfCongressIE(InfoExtractor):
7f3c3dfa
S
13 IE_NAME = 'loc'
14 IE_DESC = 'Library of Congress'
6c882aa8 15 _VALID_URL = r'https?://(?:www\.)?loc\.gov/(?:item/|today/cyberlc/feature_wdesc\.php\?.*\brec=)(?P<id>[0-9a-z_.]+)'
76e9cd7f 16 _TESTS = [{
c917106b 17 # embedded via <div class="media-player"
7f3c3dfa 18 'url': 'http://loc.gov/item/90716351/',
4e33e079 19 'md5': '6ec0ae8f07f86731b1b2ff70f046210a',
9c3c447e
T
20 'info_dict': {
21 'id': '90716351',
22 'ext': 'mp4',
7f3c3dfa 23 'title': "Pa's trip to Mars",
7f3c3dfa
S
24 'duration': 0,
25 'view_count': int,
9c3c447e 26 },
76e9cd7f 27 }, {
c917106b 28 # webcast embedded via mediaObjectId
76e9cd7f 29 'url': 'https://www.loc.gov/today/cyberlc/feature_wdesc.php?rec=5578',
c917106b
S
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',
37 },
38 'params': {
39 'skip_download': True,
40 },
4d8856d5
S
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',
51 },
52 'params': {
53 'skip_download': True,
54 },
6c882aa8
RA
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,
76e9cd7f 61 }]
9c3c447e
T
62
63 def _real_extract(self, url):
64 video_id = self._match_id(url)
65 webpage = self._download_webpage(url, video_id)
66
7f3c3dfa
S
67 media_id = self._search_regex(
68 (r'id=(["\'])media-player-(?P<id>.+?)\1',
69 r'<video[^>]+id=(["\'])uuid-(?P<id>.+?)\1',
76e9cd7f 70 r'<video[^>]+data-uuid=(["\'])(?P<id>.+?)\1',
6c882aa8
RA
71 r'mediaObjectId\s*:\s*(["\'])(?P<id>.+?)\1',
72 r'data-tab="share-media-(?P<id>[0-9A-F]{32})"'),
7f3c3dfa
S
73 webpage, 'media id', group='id')
74
76e9cd7f
S
75 data = self._download_json(
76 'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id,
6c882aa8 77 media_id)['mediaObject']
9c3c447e 78
7f3c3dfa
S
79 derivative = data['derivatives'][0]
80 media_url = derivative['derivativeUrl']
9c3c447e 81
4d8856d5
S
82 title = derivative.get('shortName') or data.get('shortName') or self._og_search_title(
83 webpage)
84
7f3c3dfa
S
85 # Following algorithm was extracted from setAVSource js function
86 # found in webpage
9c3c447e
T
87 media_url = media_url.replace('rtmp', 'https')
88
7f3c3dfa
S
89 is_video = data.get('mediaType', 'v').lower() == 'v'
90 ext = determine_ext(media_url)
91 if ext not in ('mp4', 'mp3'):
9c3c447e
T
92 media_url += '.mp4' if is_video else '.mp3'
93
6c882aa8
RA
94 formats = []
95 if '/vod/mp4:' in media_url:
96 formats.append({
97 'url': media_url.replace('/vod/mp4:', '/hls-vod/media/') + '.m3u8',
7f3c3dfa
S
98 'format_id': 'hls',
99 'ext': 'mp4',
100 'protocol': 'm3u8_native',
4d8856d5 101 'quality': 1,
6c882aa8
RA
102 })
103 http_format = {
104 'url': re.sub(r'(://[^/]+/)(?:[^/]+/)*(?:mp4|mp3):', r'\1', media_url),
105 'format_id': 'http',
106 'quality': 1,
107 }
108 if not is_video:
109 http_format['vcodec'] = 'none'
110 formats.append(http_format)
7f3c3dfa 111
4d8856d5
S
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()
6c882aa8 116 if format_id in ('gif', 'jpeg'):
4d8856d5
S
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')),
126 })
127
7f3c3dfa
S
128 duration = float_or_none(data.get('duration'))
129 view_count = int_or_none(data.get('viewCount'))
9c3c447e 130
c917106b
S
131 subtitles = {}
132 cc_url = data.get('ccUrl')
133 if cc_url:
134 subtitles.setdefault('en', []).append({
135 'url': cc_url,
136 'ext': 'ttml',
137 })
138
9c3c447e
T
139 return {
140 'id': video_id,
7f3c3dfa 141 'title': title,
76e9cd7f 142 'thumbnail': self._og_search_thumbnail(webpage, default=None),
7f3c3dfa
S
143 'duration': duration,
144 'view_count': view_count,
9c3c447e 145 'formats': formats,
c917106b 146 'subtitles': subtitles,
9c3c447e 147 }