spreaker.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import itertools
  2. from .common import InfoExtractor
  3. from ..compat import compat_str
  4. from ..utils import (
  5. float_or_none,
  6. int_or_none,
  7. str_or_none,
  8. try_get,
  9. unified_timestamp,
  10. url_or_none,
  11. )
  12. def _extract_episode(data, episode_id=None):
  13. title = data['title']
  14. download_url = data['download_url']
  15. series = try_get(data, lambda x: x['show']['title'], compat_str)
  16. uploader = try_get(data, lambda x: x['author']['fullname'], compat_str)
  17. thumbnails = []
  18. for image in ('image_original', 'image_medium', 'image'):
  19. image_url = url_or_none(data.get('%s_url' % image))
  20. if image_url:
  21. thumbnails.append({'url': image_url})
  22. def stats(key):
  23. return int_or_none(try_get(
  24. data,
  25. (lambda x: x['%ss_count' % key],
  26. lambda x: x['stats']['%ss' % key])))
  27. def duration(key):
  28. return float_or_none(data.get(key), scale=1000)
  29. return {
  30. 'id': compat_str(episode_id or data['episode_id']),
  31. 'url': download_url,
  32. 'display_id': data.get('permalink'),
  33. 'title': title,
  34. 'description': data.get('description'),
  35. 'timestamp': unified_timestamp(data.get('published_at')),
  36. 'uploader': uploader,
  37. 'uploader_id': str_or_none(data.get('author_id')),
  38. 'creator': uploader,
  39. 'duration': duration('duration') or duration('length'),
  40. 'view_count': stats('play'),
  41. 'like_count': stats('like'),
  42. 'comment_count': stats('message'),
  43. 'format': 'MPEG Layer 3',
  44. 'format_id': 'mp3',
  45. 'container': 'mp3',
  46. 'ext': 'mp3',
  47. 'thumbnails': thumbnails,
  48. 'series': series,
  49. 'extractor_key': SpreakerIE.ie_key(),
  50. }
  51. class SpreakerIE(InfoExtractor):
  52. _VALID_URL = r'''(?x)
  53. https?://
  54. api\.spreaker\.com/
  55. (?:
  56. (?:download/)?episode|
  57. v2/episodes
  58. )/
  59. (?P<id>\d+)
  60. '''
  61. _TESTS = [{
  62. 'url': 'https://api.spreaker.com/episode/12534508',
  63. 'info_dict': {
  64. 'id': '12534508',
  65. 'display_id': 'swm-ep15-how-to-market-your-music-part-2',
  66. 'ext': 'mp3',
  67. 'title': 'EP:15 | Music Marketing (Likes) - Part 2',
  68. 'description': 'md5:0588c43e27be46423e183076fa071177',
  69. 'timestamp': 1502250336,
  70. 'upload_date': '20170809',
  71. 'uploader': 'SWM',
  72. 'uploader_id': '9780658',
  73. 'duration': 1063.42,
  74. 'view_count': int,
  75. 'like_count': int,
  76. 'comment_count': int,
  77. 'series': 'Success With Music (SWM)',
  78. },
  79. }, {
  80. 'url': 'https://api.spreaker.com/download/episode/12534508/swm_ep15_how_to_market_your_music_part_2.mp3',
  81. 'only_matching': True,
  82. }, {
  83. 'url': 'https://api.spreaker.com/v2/episodes/12534508?export=episode_segments',
  84. 'only_matching': True,
  85. }]
  86. def _real_extract(self, url):
  87. episode_id = self._match_id(url)
  88. data = self._download_json(
  89. 'https://api.spreaker.com/v2/episodes/%s' % episode_id,
  90. episode_id)['response']['episode']
  91. return _extract_episode(data, episode_id)
  92. class SpreakerPageIE(InfoExtractor):
  93. _VALID_URL = r'https?://(?:www\.)?spreaker\.com/user/[^/]+/(?P<id>[^/?#&]+)'
  94. _TESTS = [{
  95. 'url': 'https://www.spreaker.com/user/9780658/swm-ep15-how-to-market-your-music-part-2',
  96. 'only_matching': True,
  97. }]
  98. def _real_extract(self, url):
  99. display_id = self._match_id(url)
  100. webpage = self._download_webpage(url, display_id)
  101. episode_id = self._search_regex(
  102. (r'data-episode_id=["\'](?P<id>\d+)',
  103. r'episode_id\s*:\s*(?P<id>\d+)'), webpage, 'episode id')
  104. return self.url_result(
  105. 'https://api.spreaker.com/episode/%s' % episode_id,
  106. ie=SpreakerIE.ie_key(), video_id=episode_id)
  107. class SpreakerShowIE(InfoExtractor):
  108. _VALID_URL = r'https?://api\.spreaker\.com/show/(?P<id>\d+)'
  109. _TESTS = [{
  110. 'url': 'https://api.spreaker.com/show/4652058',
  111. 'info_dict': {
  112. 'id': '4652058',
  113. },
  114. 'playlist_mincount': 118,
  115. }]
  116. def _entries(self, show_id):
  117. for page_num in itertools.count(1):
  118. episodes = self._download_json(
  119. 'https://api.spreaker.com/show/%s/episodes' % show_id,
  120. show_id, note='Downloading JSON page %d' % page_num, query={
  121. 'page': page_num,
  122. 'max_per_page': 100,
  123. })
  124. pager = try_get(episodes, lambda x: x['response']['pager'], dict)
  125. if not pager:
  126. break
  127. results = pager.get('results')
  128. if not results or not isinstance(results, list):
  129. break
  130. for result in results:
  131. if not isinstance(result, dict):
  132. continue
  133. yield _extract_episode(result)
  134. if page_num == pager.get('last_page'):
  135. break
  136. def _real_extract(self, url):
  137. show_id = self._match_id(url)
  138. return self.playlist_result(self._entries(show_id), playlist_id=show_id)
  139. class SpreakerShowPageIE(InfoExtractor):
  140. _VALID_URL = r'https?://(?:www\.)?spreaker\.com/show/(?P<id>[^/?#&]+)'
  141. _TESTS = [{
  142. 'url': 'https://www.spreaker.com/show/success-with-music',
  143. 'only_matching': True,
  144. }]
  145. def _real_extract(self, url):
  146. display_id = self._match_id(url)
  147. webpage = self._download_webpage(url, display_id)
  148. show_id = self._search_regex(
  149. r'show_id\s*:\s*(?P<id>\d+)', webpage, 'show id')
  150. return self.url_result(
  151. 'https://api.spreaker.com/show/%s' % show_id,
  152. ie=SpreakerShowIE.ie_key(), video_id=show_id)