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