stitcher.py 5.2 KB

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