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