]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/libraryofcongress.py
[README.md] Make download URLs consistent
[youtube-dl] / youtube_dl / extractor / libraryofcongress.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7
8 from ..utils import (
9     determine_ext,
10     float_or_none,
11     int_or_none,
12     parse_filesize,
13 )
14
15
16 class LibraryOfCongressIE(InfoExtractor):
17     IE_NAME = 'loc'
18     IE_DESC = 'Library of Congress'
19     _VALID_URL = r'https?://(?:www\.)?loc\.gov/(?:item/|today/cyberlc/feature_wdesc\.php\?.*\brec=)(?P<id>[0-9]+)'
20     _TESTS = [{
21         # embedded via <div class="media-player"
22         'url': 'http://loc.gov/item/90716351/',
23         'md5': '353917ff7f0255aa6d4b80a034833de8',
24         'info_dict': {
25             'id': '90716351',
26             'ext': 'mp4',
27             'title': "Pa's trip to Mars",
28             'thumbnail': 're:^https?://.*\.jpg$',
29             'duration': 0,
30             'view_count': int,
31         },
32     }, {
33         # webcast embedded via mediaObjectId
34         'url': 'https://www.loc.gov/today/cyberlc/feature_wdesc.php?rec=5578',
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         },
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         },
60     }]
61
62     def _real_extract(self, url):
63         video_id = self._match_id(url)
64         webpage = self._download_webpage(url, video_id)
65
66         media_id = self._search_regex(
67             (r'id=(["\'])media-player-(?P<id>.+?)\1',
68              r'<video[^>]+id=(["\'])uuid-(?P<id>.+?)\1',
69              r'<video[^>]+data-uuid=(["\'])(?P<id>.+?)\1',
70              r'mediaObjectId\s*:\s*(["\'])(?P<id>.+?)\1'),
71             webpage, 'media id', group='id')
72
73         data = self._download_json(
74             'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id,
75             video_id)['mediaObject']
76
77         derivative = data['derivatives'][0]
78         media_url = derivative['derivativeUrl']
79
80         title = derivative.get('shortName') or data.get('shortName') or self._og_search_title(
81             webpage)
82
83         # Following algorithm was extracted from setAVSource js function
84         # found in webpage
85         media_url = media_url.replace('rtmp', 'https')
86
87         is_video = data.get('mediaType', 'v').lower() == 'v'
88         ext = determine_ext(media_url)
89         if ext not in ('mp4', 'mp3'):
90             media_url += '.mp4' if is_video else '.mp3'
91
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',
98                 'quality': 1,
99             }]
100         elif 'vod/mp3:' in media_url:
101             formats = [{
102                 'url': media_url.replace('vod/mp3:', ''),
103                 'vcodec': 'none',
104             }]
105
106         download_urls = set()
107         for m in re.finditer(
108                 r'<option[^>]+value=(["\'])(?P<url>.+?)\1[^>]+data-file-download=[^>]+>\s*(?P<id>.+?)(?:(?:&nbsp;|\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
122         self._sort_formats(formats)
123
124         duration = float_or_none(data.get('duration'))
125         view_count = int_or_none(data.get('viewCount'))
126
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
135         return {
136             'id': video_id,
137             'title': title,
138             'thumbnail': self._og_search_thumbnail(webpage, default=None),
139             'duration': duration,
140             'view_count': view_count,
141             'formats': formats,
142             'subtitles': subtitles,
143         }