You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.6 KiB

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