logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

libraryofcongress.py (5029B)


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