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.

82 lines
2.9 KiB

  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..compat import compat_urllib_parse_urlencode
  4. from ..utils import (
  5. int_or_none,
  6. qualities,
  7. )
  8. class NprIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:www\.)?npr\.org/player/v2/mediaPlayer\.html\?.*\bid=(?P<id>\d+)'
  10. _TESTS = [{
  11. 'url': 'http://www.npr.org/player/v2/mediaPlayer.html?id=449974205',
  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': 'http://www.npr.org/player/v2/mediaPlayer.html?action=1&t=1&islist=false&id=446928052&m=446929930&live=1',
  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. def _real_extract(self, url):
  34. playlist_id = self._match_id(url)
  35. config = self._download_json(
  36. 'http://api.npr.org/query?%s' % compat_urllib_parse_urlencode({
  37. 'id': playlist_id,
  38. 'fields': 'titles,audio,show',
  39. 'format': 'json',
  40. 'apiKey': 'MDAzMzQ2MjAyMDEyMzk4MTU1MDg3ZmM3MQ010',
  41. }), playlist_id)
  42. story = config['list']['story'][0]
  43. KNOWN_FORMATS = ('threegp', 'mp4', 'mp3')
  44. quality = qualities(KNOWN_FORMATS)
  45. entries = []
  46. for audio in story.get('audio', []):
  47. title = audio.get('title', {}).get('$text')
  48. duration = int_or_none(audio.get('duration', {}).get('$text'))
  49. formats = []
  50. for format_id, formats_entry in audio.get('format', {}).items():
  51. if not formats_entry:
  52. continue
  53. if isinstance(formats_entry, list):
  54. formats_entry = formats_entry[0]
  55. format_url = formats_entry.get('$text')
  56. if not format_url:
  57. continue
  58. if format_id in KNOWN_FORMATS:
  59. formats.append({
  60. 'url': format_url,
  61. 'format_id': format_id,
  62. 'ext': formats_entry.get('type'),
  63. 'quality': quality(format_id),
  64. })
  65. self._sort_formats(formats)
  66. entries.append({
  67. 'id': audio['id'],
  68. 'title': title,
  69. 'duration': duration,
  70. 'formats': formats,
  71. })
  72. playlist_title = story.get('title', {}).get('$text')
  73. return self.playlist_result(entries, playlist_id, playlist_title)