stitcher.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. clean_html,
  5. clean_podcast_url,
  6. int_or_none,
  7. str_or_none,
  8. try_get,
  9. url_or_none,
  10. )
  11. class StitcherBaseIE(InfoExtractor):
  12. _VALID_URL_BASE = r'https?://(?:www\.)?stitcher\.com/(?:podcast|show)/'
  13. def _call_api(self, path, video_id, query):
  14. resp = self._download_json(
  15. 'https://api.prod.stitcher.com/' + path,
  16. video_id, query=query)
  17. error_massage = try_get(resp, lambda x: x['errors'][0]['message'])
  18. if error_massage:
  19. raise ExtractorError(error_massage, expected=True)
  20. return resp['data']
  21. def _extract_description(self, data):
  22. return clean_html(data.get('html_description') or data.get('description'))
  23. def _extract_audio_url(self, episode):
  24. return url_or_none(episode.get('audio_url') or episode.get('guid'))
  25. def _extract_show_info(self, show):
  26. return {
  27. 'thumbnail': show.get('image_base_url'),
  28. 'series': show.get('title'),
  29. }
  30. def _extract_episode(self, episode, audio_url, show_info):
  31. info = {
  32. 'id': str(episode['id']),
  33. 'display_id': episode.get('slug'),
  34. 'title': episode['title'].strip(),
  35. 'description': self._extract_description(episode),
  36. 'duration': int_or_none(episode.get('duration')),
  37. 'url': clean_podcast_url(audio_url),
  38. 'vcodec': 'none',
  39. 'timestamp': int_or_none(episode.get('date_published')),
  40. 'season_number': int_or_none(episode.get('season')),
  41. 'season_id': str_or_none(episode.get('season_id')),
  42. }
  43. info.update(show_info)
  44. return info
  45. class StitcherIE(StitcherBaseIE):
  46. _VALID_URL = StitcherBaseIE._VALID_URL_BASE + r'(?:[^/]+/)+e(?:pisode)?/(?:[^/#?&]+-)?(?P<id>\d+)'
  47. _TESTS = [{
  48. 'url': 'http://www.stitcher.com/podcast/the-talking-machines/e/40789481?autoplay=true',
  49. 'md5': 'e9635098e0da10b21a0e2b85585530f6',
  50. 'info_dict': {
  51. 'id': '40789481',
  52. 'ext': 'mp3',
  53. 'title': 'Machine Learning Mastery and Cancer Clusters',
  54. 'description': 'md5:547adb4081864be114ae3831b4c2b42f',
  55. 'duration': 1604,
  56. 'thumbnail': r're:^https?://.*\.jpg',
  57. 'upload_date': '20151008',
  58. 'timestamp': 1444285800,
  59. 'series': 'Talking Machines',
  60. },
  61. }, {
  62. 'url': 'http://www.stitcher.com/podcast/panoply/vulture-tv/e/the-rare-hourlong-comedy-plus-40846275?autoplay=true',
  63. 'info_dict': {
  64. 'id': '40846275',
  65. 'display_id': 'the-rare-hourlong-comedy-plus',
  66. 'ext': 'mp3',
  67. 'title': "The CW's 'Crazy Ex-Girlfriend'",
  68. 'description': 'md5:04f1e2f98eb3f5cbb094cea0f9e19b17',
  69. 'duration': 2235,
  70. 'thumbnail': r're:^https?://.*\.jpg',
  71. },
  72. 'params': {
  73. 'skip_download': True,
  74. },
  75. 'skip': 'Page Not Found',
  76. }, {
  77. # escaped title
  78. 'url': 'http://www.stitcher.com/podcast/marketplace-on-stitcher/e/40910226?autoplay=true',
  79. 'only_matching': True,
  80. }, {
  81. 'url': 'http://www.stitcher.com/podcast/panoply/getting-in/e/episode-2a-how-many-extracurriculars-should-i-have-40876278?autoplay=true',
  82. 'only_matching': True,
  83. }, {
  84. 'url': 'https://www.stitcher.com/show/threedom/episode/circles-on-a-stick-200212584',
  85. 'only_matching': True,
  86. }]
  87. def _real_extract(self, url):
  88. audio_id = self._match_id(url)
  89. data = self._call_api(
  90. 'shows/episodes', audio_id, {'episode_ids': audio_id})
  91. episode = data['episodes'][0]
  92. audio_url = self._extract_audio_url(episode)
  93. if not audio_url:
  94. self.raise_login_required()
  95. show = try_get(data, lambda x: x['shows'][0], dict) or {}
  96. return self._extract_episode(
  97. episode, audio_url, self._extract_show_info(show))
  98. class StitcherShowIE(StitcherBaseIE):
  99. _VALID_URL = StitcherBaseIE._VALID_URL_BASE + r'(?P<id>[^/#?&]+)/?(?:[?#&]|$)'
  100. _TESTS = [{
  101. 'url': 'http://www.stitcher.com/podcast/the-talking-machines',
  102. 'info_dict': {
  103. 'id': 'the-talking-machines',
  104. 'title': 'Talking Machines',
  105. 'description': 'md5:831f0995e40f26c10231af39cf1ebf0b',
  106. },
  107. 'playlist_mincount': 106,
  108. }, {
  109. 'url': 'https://www.stitcher.com/show/the-talking-machines',
  110. 'only_matching': True,
  111. }]
  112. def _real_extract(self, url):
  113. show_slug = self._match_id(url)
  114. data = self._call_api(
  115. f'search/show/{show_slug}/allEpisodes', show_slug, {'count': 10000})
  116. show = try_get(data, lambda x: x['shows'][0], dict) or {}
  117. show_info = self._extract_show_info(show)
  118. entries = []
  119. for episode in (data.get('episodes') or []):
  120. audio_url = self._extract_audio_url(episode)
  121. if not audio_url:
  122. continue
  123. entries.append(self._extract_episode(episode, audio_url, show_info))
  124. return self.playlist_result(
  125. entries, show_slug, show.get('title'),
  126. self._extract_description(show))