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.

108 lines
4.2 KiB

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