[loc] Add support for another URL schema and simplify
[youtube-dl] / youtube_dl / extractor / libraryofcongress.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5
6 from ..utils import (
7     determine_ext,
8     float_or_none,
9     int_or_none,
10 )
11
12
13 class LibraryOfCongressIE(InfoExtractor):
14     IE_NAME = 'loc'
15     IE_DESC = 'Library of Congress'
16     _VALID_URL = r'https?://(?:www\.)?loc\.gov/(?:item/|today/cyberlc/feature_wdesc\.php\?.*\brec=)(?P<id>[0-9]+)'
17     _TESTS = [{
18         'url': 'http://loc.gov/item/90716351/',
19         'md5': '353917ff7f0255aa6d4b80a034833de8',
20         'info_dict': {
21             'id': '90716351',
22             'ext': 'mp4',
23             'title': "Pa's trip to Mars",
24             'thumbnail': 're:^https?://.*\.jpg$',
25             'duration': 0,
26             'view_count': int,
27         },
28     }, {
29         'url': 'https://www.loc.gov/today/cyberlc/feature_wdesc.php?rec=5578',
30         'only_matching': True,
31     }]
32
33     def _real_extract(self, url):
34         video_id = self._match_id(url)
35         webpage = self._download_webpage(url, video_id)
36
37         media_id = self._search_regex(
38             (r'id=(["\'])media-player-(?P<id>.+?)\1',
39              r'<video[^>]+id=(["\'])uuid-(?P<id>.+?)\1',
40              r'<video[^>]+data-uuid=(["\'])(?P<id>.+?)\1',
41              r'mediaObjectId\s*:\s*(["\'])(?P<id>.+?)\1'),
42             webpage, 'media id', group='id')
43
44         data = self._download_json(
45             'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id,
46             video_id)['mediaObject']
47
48         derivative = data['derivatives'][0]
49         media_url = derivative['derivativeUrl']
50
51         # Following algorithm was extracted from setAVSource js function
52         # found in webpage
53         media_url = media_url.replace('rtmp', 'https')
54
55         is_video = data.get('mediaType', 'v').lower() == 'v'
56         ext = determine_ext(media_url)
57         if ext not in ('mp4', 'mp3'):
58             media_url += '.mp4' if is_video else '.mp3'
59
60         if 'vod/mp4:' in media_url:
61             formats = [{
62                 'url': media_url.replace('vod/mp4:', 'hls-vod/media/') + '.m3u8',
63                 'format_id': 'hls',
64                 'ext': 'mp4',
65                 'protocol': 'm3u8_native',
66             }]
67         elif 'vod/mp3:' in media_url:
68             formats = [{
69                 'url': media_url.replace('vod/mp3:', ''),
70                 'vcodec': 'none',
71             }]
72
73         self._sort_formats(formats)
74
75         title = derivative.get('shortName') or data.get('shortName') or self._og_search_title(webpage)
76         duration = float_or_none(data.get('duration'))
77         view_count = int_or_none(data.get('viewCount'))
78
79         return {
80             'id': video_id,
81             'title': title,
82             'thumbnail': self._og_search_thumbnail(webpage, default=None),
83             'duration': duration,
84             'view_count': view_count,
85             'formats': formats,
86         }