libraryofcongress.py 4.8 KB

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