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.

107 lines
3.4 KiB

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