]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/libraryofcongress.py
[loc] Improve (Closes #9521)
[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'
9c3c447e 16 _VALID_URL = r'https?://(?:www\.)?loc\.gov/item/(?P<id>[0-9]+)'
7f3c3dfa
S
17 _TEST = {
18 'url': 'http://loc.gov/item/90716351/',
19 'md5': '353917ff7f0255aa6d4b80a034833de8',
9c3c447e
T
20 'info_dict': {
21 'id': '90716351',
22 'ext': 'mp4',
7f3c3dfa
S
23 'title': "Pa's trip to Mars",
24 'thumbnail': 're:^https?://.*\.jpg$',
25 'duration': 0,
26 'view_count': int,
9c3c447e 27 },
7f3c3dfa 28 }
9c3c447e
T
29
30 def _real_extract(self, url):
31 video_id = self._match_id(url)
32 webpage = self._download_webpage(url, video_id)
33
7f3c3dfa
S
34 media_id = self._search_regex(
35 (r'id=(["\'])media-player-(?P<id>.+?)\1',
36 r'<video[^>]+id=(["\'])uuid-(?P<id>.+?)\1',
37 r'<video[^>]+data-uuid=(["\'])(?P<id>.+?)\1'),
38 webpage, 'media id', group='id')
39
40 data = self._parse_json(
41 self._download_webpage(
42 'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id,
43 video_id),
44 video_id)['mediaObject']
9c3c447e 45
7f3c3dfa
S
46 derivative = data['derivatives'][0]
47 media_url = derivative['derivativeUrl']
9c3c447e 48
7f3c3dfa
S
49 # Following algorithm was extracted from setAVSource js function
50 # found in webpage
9c3c447e
T
51 media_url = media_url.replace('rtmp', 'https')
52
7f3c3dfa
S
53 is_video = data.get('mediaType', 'v').lower() == 'v'
54 ext = determine_ext(media_url)
55 if ext not in ('mp4', 'mp3'):
9c3c447e
T
56 media_url += '.mp4' if is_video else '.mp3'
57
7f3c3dfa
S
58 if 'vod/mp4:' in media_url:
59 formats = [{
60 'url': media_url.replace('vod/mp4:', 'hls-vod/media/') + '.m3u8',
61 'format_id': 'hls',
62 'ext': 'mp4',
63 'protocol': 'm3u8_native',
64 }]
65 elif 'vod/mp3:' in media_url:
66 formats = [{
67 'url': media_url.replace('vod/mp3:', ''),
68 'vcodec': 'none',
69 }]
70
71 self._sort_formats(formats)
9c3c447e 72
7f3c3dfa
S
73 title = derivative.get('shortName') or data.get('shortName') or self._og_search_title(webpage)
74 duration = float_or_none(data.get('duration'))
75 view_count = int_or_none(data.get('viewCount'))
9c3c447e
T
76
77 return {
78 'id': video_id,
7f3c3dfa 79 'title': title,
9c3c447e 80 'thumbnail': self._og_search_thumbnail(webpage),
7f3c3dfa
S
81 'duration': duration,
82 'view_count': view_count,
9c3c447e
T
83 'formats': formats,
84 }