logo

youtube-dl

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

iheart.py (3465B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. clean_html,
  6. clean_podcast_url,
  7. int_or_none,
  8. str_or_none,
  9. )
  10. class IHeartRadioBaseIE(InfoExtractor):
  11. def _call_api(self, path, video_id, fatal=True, query=None):
  12. return self._download_json(
  13. 'https://api.iheart.com/api/v3/podcast/' + path,
  14. video_id, fatal=fatal, query=query)
  15. def _extract_episode(self, episode):
  16. return {
  17. 'thumbnail': episode.get('imageUrl'),
  18. 'description': clean_html(episode.get('description')),
  19. 'timestamp': int_or_none(episode.get('startDate'), 1000),
  20. 'duration': int_or_none(episode.get('duration')),
  21. }
  22. class IHeartRadioIE(IHeartRadioBaseIE):
  23. IENAME = 'iheartradio'
  24. _VALID_URL = r'(?:https?://(?:www\.)?iheart\.com/podcast/[^/]+/episode/(?P<display_id>[^/?&#]+)-|iheartradio:)(?P<id>\d+)'
  25. _TEST = {
  26. 'url': 'https://www.iheart.com/podcast/105-behind-the-bastards-29236323/episode/part-one-alexander-lukashenko-the-dictator-70346499/?embed=true',
  27. 'md5': 'c8609c92c8688dcb69d8541042b8abca',
  28. 'info_dict': {
  29. 'id': '70346499',
  30. 'ext': 'mp3',
  31. 'title': 'Part One: Alexander Lukashenko: The Dictator of Belarus',
  32. 'description': 'md5:96cc7297b3a5a9ebae28643801c96fae',
  33. 'timestamp': 1597741200,
  34. 'upload_date': '20200818',
  35. }
  36. }
  37. def _real_extract(self, url):
  38. episode_id = self._match_id(url)
  39. episode = self._call_api(
  40. 'episodes/' + episode_id, episode_id)['episode']
  41. info = self._extract_episode(episode)
  42. info.update({
  43. 'id': episode_id,
  44. 'title': episode['title'],
  45. 'url': clean_podcast_url(episode['mediaUrl']),
  46. })
  47. return info
  48. class IHeartRadioPodcastIE(IHeartRadioBaseIE):
  49. IE_NAME = 'iheartradio:podcast'
  50. _VALID_URL = r'https?://(?:www\.)?iheart(?:podcastnetwork)?\.com/podcast/[^/?&#]+-(?P<id>\d+)/?(?:[?#&]|$)'
  51. _TESTS = [{
  52. 'url': 'https://www.iheart.com/podcast/1119-it-could-happen-here-30717896/',
  53. 'info_dict': {
  54. 'id': '30717896',
  55. 'title': 'It Could Happen Here',
  56. 'description': 'md5:5842117412a967eb0b01f8088eb663e2',
  57. },
  58. 'playlist_mincount': 11,
  59. }, {
  60. 'url': 'https://www.iheartpodcastnetwork.com/podcast/105-stuff-you-should-know-26940277',
  61. 'only_matching': True,
  62. }]
  63. def _real_extract(self, url):
  64. podcast_id = self._match_id(url)
  65. path = 'podcasts/' + podcast_id
  66. episodes = self._call_api(
  67. path + '/episodes', podcast_id, query={'limit': 1000000000})['data']
  68. entries = []
  69. for episode in episodes:
  70. episode_id = str_or_none(episode.get('id'))
  71. if not episode_id:
  72. continue
  73. info = self._extract_episode(episode)
  74. info.update({
  75. '_type': 'url',
  76. 'id': episode_id,
  77. 'title': episode.get('title'),
  78. 'url': 'iheartradio:' + episode_id,
  79. 'ie_key': IHeartRadioIE.ie_key(),
  80. })
  81. entries.append(info)
  82. podcast = self._call_api(path, podcast_id, False) or {}
  83. return self.playlist_result(
  84. entries, podcast_id, podcast.get('title'), podcast.get('description'))