logo

youtube-dl

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

npr.py (5063B)


  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. int_or_none,
  5. qualities,
  6. url_or_none,
  7. )
  8. class NprIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:www\.)?npr\.org/(?:sections/[^/]+/)?\d{4}/\d{2}/\d{2}/(?P<id>\d+)'
  10. _TESTS = [{
  11. 'url': 'https://www.npr.org/sections/allsongs/2015/10/21/449974205/new-music-from-beach-house-chairlift-cmj-discoveries-and-more',
  12. 'info_dict': {
  13. 'id': '449974205',
  14. 'title': 'New Music From Beach House, Chairlift, CMJ Discoveries And More'
  15. },
  16. 'playlist_count': 7,
  17. }, {
  18. 'url': 'https://www.npr.org/sections/deceptivecadence/2015/10/09/446928052/music-from-the-shadows-ancient-armenian-hymns-and-piano-jazz',
  19. 'info_dict': {
  20. 'id': '446928052',
  21. 'title': "Songs We Love: Tigran Hamasyan, 'Your Mercy is Boundless'"
  22. },
  23. 'playlist': [{
  24. 'md5': '12fa60cb2d3ed932f53609d4aeceabf1',
  25. 'info_dict': {
  26. 'id': '446929930',
  27. 'ext': 'mp3',
  28. 'title': 'Your Mercy is Boundless (Bazum en Qo gtutyunqd)',
  29. 'duration': 402,
  30. },
  31. }],
  32. }, {
  33. # multimedia, not media title
  34. 'url': 'https://www.npr.org/2017/06/19/533198237/tigers-jaw-tiny-desk-concert',
  35. 'info_dict': {
  36. 'id': '533198237',
  37. 'title': 'Tigers Jaw: Tiny Desk Concert',
  38. },
  39. 'playlist': [{
  40. 'md5': '12fa60cb2d3ed932f53609d4aeceabf1',
  41. 'info_dict': {
  42. 'id': '533201718',
  43. 'ext': 'mp4',
  44. 'title': 'Tigers Jaw: Tiny Desk Concert',
  45. 'duration': 402,
  46. },
  47. }],
  48. 'expected_warnings': ['Failed to download m3u8 information'],
  49. }, {
  50. # multimedia, no formats, stream
  51. 'url': 'https://www.npr.org/2020/02/14/805476846/laura-stevenson-tiny-desk-concert',
  52. 'only_matching': True,
  53. }]
  54. def _real_extract(self, url):
  55. playlist_id = self._match_id(url)
  56. story = self._download_json(
  57. 'http://api.npr.org/query', playlist_id, query={
  58. 'id': playlist_id,
  59. 'fields': 'audio,multimedia,title',
  60. 'format': 'json',
  61. 'apiKey': 'MDAzMzQ2MjAyMDEyMzk4MTU1MDg3ZmM3MQ010',
  62. })['list']['story'][0]
  63. playlist_title = story.get('title', {}).get('$text')
  64. KNOWN_FORMATS = ('threegp', 'm3u8', 'smil', 'mp4', 'mp3')
  65. quality = qualities(KNOWN_FORMATS)
  66. entries = []
  67. for media in story.get('audio', []) + story.get('multimedia', []):
  68. media_id = media['id']
  69. formats = []
  70. for format_id, formats_entry in media.get('format', {}).items():
  71. if not formats_entry:
  72. continue
  73. if isinstance(formats_entry, list):
  74. formats_entry = formats_entry[0]
  75. format_url = formats_entry.get('$text')
  76. if not format_url:
  77. continue
  78. if format_id in KNOWN_FORMATS:
  79. if format_id == 'm3u8':
  80. formats.extend(self._extract_m3u8_formats(
  81. format_url, media_id, 'mp4', 'm3u8_native',
  82. m3u8_id='hls', fatal=False))
  83. elif format_id == 'smil':
  84. smil_formats = self._extract_smil_formats(
  85. format_url, media_id, transform_source=lambda s: s.replace(
  86. 'rtmp://flash.npr.org/ondemand/', 'https://ondemand.npr.org/'))
  87. self._check_formats(smil_formats, media_id)
  88. formats.extend(smil_formats)
  89. else:
  90. formats.append({
  91. 'url': format_url,
  92. 'format_id': format_id,
  93. 'quality': quality(format_id),
  94. })
  95. for stream_id, stream_entry in media.get('stream', {}).items():
  96. if not isinstance(stream_entry, dict):
  97. continue
  98. if stream_id != 'hlsUrl':
  99. continue
  100. stream_url = url_or_none(stream_entry.get('$text'))
  101. if not stream_url:
  102. continue
  103. formats.extend(self._extract_m3u8_formats(
  104. stream_url, stream_id, 'mp4', 'm3u8_native',
  105. m3u8_id='hls', fatal=False))
  106. self._sort_formats(formats)
  107. entries.append({
  108. 'id': media_id,
  109. 'title': media.get('title', {}).get('$text') or playlist_title,
  110. 'thumbnail': media.get('altImageUrl', {}).get('$text'),
  111. 'duration': int_or_none(media.get('duration', {}).get('$text')),
  112. 'formats': formats,
  113. })
  114. return self.playlist_result(entries, playlist_id, playlist_title)