logo

youtube-dl

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

stitcher.py (5371B)


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