]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/libraryofcongress.py
[loc] Extract subtites
[yt-dlp.git] / youtube_dl / extractor / libraryofcongress.py
CommitLineData
9c3c447e
T
1# coding: utf-8
2from __future__ import unicode_literals
3
4from .common import InfoExtractor
5
7f3c3dfa
S
6from ..utils import (
7 determine_ext,
8 float_or_none,
9 int_or_none,
10)
9c3c447e
T
11
12
13class LibraryOfCongressIE(InfoExtractor):
7f3c3dfa
S
14 IE_NAME = 'loc'
15 IE_DESC = 'Library of Congress'
76e9cd7f
S
16 _VALID_URL = r'https?://(?:www\.)?loc\.gov/(?:item/|today/cyberlc/feature_wdesc\.php\?.*\brec=)(?P<id>[0-9]+)'
17 _TESTS = [{
c917106b 18 # embedded via <div class="media-player"
7f3c3dfa
S
19 'url': 'http://loc.gov/item/90716351/',
20 'md5': '353917ff7f0255aa6d4b80a034833de8',
9c3c447e
T
21 'info_dict': {
22 'id': '90716351',
23 'ext': 'mp4',
7f3c3dfa
S
24 'title': "Pa's trip to Mars",
25 'thumbnail': 're:^https?://.*\.jpg$',
26 'duration': 0,
27 'view_count': int,
9c3c447e 28 },
76e9cd7f 29 }, {
c917106b 30 # webcast embedded via mediaObjectId
76e9cd7f 31 'url': 'https://www.loc.gov/today/cyberlc/feature_wdesc.php?rec=5578',
c917106b
S
32 'info_dict': {
33 'id': '5578',
34 'ext': 'mp4',
35 'title': 'Help! Preservation Training Needs Here, There & Everywhere',
36 'duration': 3765,
37 'view_count': int,
38 'subtitles': 'mincount:1',
39 },
40 'params': {
41 'skip_download': True,
42 },
76e9cd7f 43 }]
9c3c447e
T
44
45 def _real_extract(self, url):
46 video_id = self._match_id(url)
47 webpage = self._download_webpage(url, video_id)
48
7f3c3dfa
S
49 media_id = self._search_regex(
50 (r'id=(["\'])media-player-(?P<id>.+?)\1',
51 r'<video[^>]+id=(["\'])uuid-(?P<id>.+?)\1',
76e9cd7f
S
52 r'<video[^>]+data-uuid=(["\'])(?P<id>.+?)\1',
53 r'mediaObjectId\s*:\s*(["\'])(?P<id>.+?)\1'),
7f3c3dfa
S
54 webpage, 'media id', group='id')
55
76e9cd7f
S
56 data = self._download_json(
57 'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id,
7f3c3dfa 58 video_id)['mediaObject']
9c3c447e 59
7f3c3dfa
S
60 derivative = data['derivatives'][0]
61 media_url = derivative['derivativeUrl']
9c3c447e 62
7f3c3dfa
S
63 # Following algorithm was extracted from setAVSource js function
64 # found in webpage
9c3c447e
T
65 media_url = media_url.replace('rtmp', 'https')
66
7f3c3dfa
S
67 is_video = data.get('mediaType', 'v').lower() == 'v'
68 ext = determine_ext(media_url)
69 if ext not in ('mp4', 'mp3'):
9c3c447e
T
70 media_url += '.mp4' if is_video else '.mp3'
71
7f3c3dfa
S
72 if 'vod/mp4:' in media_url:
73 formats = [{
74 'url': media_url.replace('vod/mp4:', 'hls-vod/media/') + '.m3u8',
75 'format_id': 'hls',
76 'ext': 'mp4',
77 'protocol': 'm3u8_native',
78 }]
79 elif 'vod/mp3:' in media_url:
80 formats = [{
81 'url': media_url.replace('vod/mp3:', ''),
82 'vcodec': 'none',
83 }]
84
85 self._sort_formats(formats)
9c3c447e 86
7f3c3dfa
S
87 title = derivative.get('shortName') or data.get('shortName') or self._og_search_title(webpage)
88 duration = float_or_none(data.get('duration'))
89 view_count = int_or_none(data.get('viewCount'))
9c3c447e 90
c917106b
S
91 subtitles = {}
92 cc_url = data.get('ccUrl')
93 if cc_url:
94 subtitles.setdefault('en', []).append({
95 'url': cc_url,
96 'ext': 'ttml',
97 })
98
9c3c447e
T
99 return {
100 'id': video_id,
7f3c3dfa 101 'title': title,
76e9cd7f 102 'thumbnail': self._og_search_thumbnail(webpage, default=None),
7f3c3dfa
S
103 'duration': duration,
104 'view_count': view_count,
9c3c447e 105 'formats': formats,
c917106b 106 'subtitles': subtitles,
9c3c447e 107 }