libraryofcongress.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. 'url': 'http://loc.gov/item/90716351/',
  15. 'md5': '353917ff7f0255aa6d4b80a034833de8',
  16. 'info_dict': {
  17. 'id': '90716351',
  18. 'ext': 'mp4',
  19. 'title': "Pa's trip to Mars",
  20. 'thumbnail': 're:^https?://.*\.jpg$',
  21. 'duration': 0,
  22. 'view_count': int,
  23. },
  24. }, {
  25. 'url': 'https://www.loc.gov/today/cyberlc/feature_wdesc.php?rec=5578',
  26. 'only_matching': True,
  27. }]
  28. def _real_extract(self, url):
  29. video_id = self._match_id(url)
  30. webpage = self._download_webpage(url, video_id)
  31. media_id = self._search_regex(
  32. (r'id=(["\'])media-player-(?P<id>.+?)\1',
  33. r'<video[^>]+id=(["\'])uuid-(?P<id>.+?)\1',
  34. r'<video[^>]+data-uuid=(["\'])(?P<id>.+?)\1',
  35. r'mediaObjectId\s*:\s*(["\'])(?P<id>.+?)\1'),
  36. webpage, 'media id', group='id')
  37. data = self._download_json(
  38. 'https://media.loc.gov/services/v1/media?id=%s&context=json' % media_id,
  39. video_id)['mediaObject']
  40. derivative = data['derivatives'][0]
  41. media_url = derivative['derivativeUrl']
  42. # Following algorithm was extracted from setAVSource js function
  43. # found in webpage
  44. media_url = media_url.replace('rtmp', 'https')
  45. is_video = data.get('mediaType', 'v').lower() == 'v'
  46. ext = determine_ext(media_url)
  47. if ext not in ('mp4', 'mp3'):
  48. media_url += '.mp4' if is_video else '.mp3'
  49. if 'vod/mp4:' in media_url:
  50. formats = [{
  51. 'url': media_url.replace('vod/mp4:', 'hls-vod/media/') + '.m3u8',
  52. 'format_id': 'hls',
  53. 'ext': 'mp4',
  54. 'protocol': 'm3u8_native',
  55. }]
  56. elif 'vod/mp3:' in media_url:
  57. formats = [{
  58. 'url': media_url.replace('vod/mp3:', ''),
  59. 'vcodec': 'none',
  60. }]
  61. self._sort_formats(formats)
  62. title = derivative.get('shortName') or data.get('shortName') or self._og_search_title(webpage)
  63. duration = float_or_none(data.get('duration'))
  64. view_count = int_or_none(data.get('viewCount'))
  65. return {
  66. 'id': video_id,
  67. 'title': title,
  68. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  69. 'duration': duration,
  70. 'view_count': view_count,
  71. 'formats': formats,
  72. }