]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/libraryofcongress.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / libraryofcongress.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 determine_ext,
6 float_or_none,
7 int_or_none,
8 parse_filesize,
9 )
10
11
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,
26 },
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',
37 },
38 'params': {
39 'skip_download': True,
40 },
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 },
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,
61 }]
62
63 def _real_extract(self, url):
64 video_id = self._match_id(url)
65 webpage = self._download_webpage(url, video_id)
66
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')
74
75 data = self._download_json(
76 'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id,
77 media_id)['mediaObject']
78
79 derivative = data['derivatives'][0]
80 media_url = derivative['derivativeUrl']
81
82 title = derivative.get('shortName') or data.get('shortName') or self._og_search_title(
83 webpage)
84
85 # Following algorithm was extracted from setAVSource js function
86 # found in webpage
87 media_url = media_url.replace('rtmp', 'https')
88
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'
93
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,
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)
111
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')),
126 })
127
128 duration = float_or_none(data.get('duration'))
129 view_count = int_or_none(data.get('viewCount'))
130
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
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,
147 }