acast.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import functools
  5. from .common import InfoExtractor
  6. from ..compat import compat_str
  7. from ..utils import (
  8. float_or_none,
  9. int_or_none,
  10. try_get,
  11. unified_timestamp,
  12. OnDemandPagedList,
  13. )
  14. class ACastIE(InfoExtractor):
  15. IE_NAME = 'acast'
  16. _VALID_URL = r'https?://(?:(?:embed|www)\.)?acast\.com/(?P<channel>[^/]+)/(?P<id>[^/#?]+)'
  17. _TESTS = [{
  18. 'url': 'https://www.acast.com/sparpodcast/2.raggarmordet-rosterurdetforflutna',
  19. 'md5': 'a02393c74f3bdb1801c3ec2695577ce0',
  20. 'info_dict': {
  21. 'id': '2a92b283-1a75-4ad8-8396-499c641de0d9',
  22. 'ext': 'mp3',
  23. 'title': '2. Raggarmordet - Röster ur det förflutna',
  24. 'description': 'md5:4f81f6d8cf2e12ee21a321d8bca32db4',
  25. 'timestamp': 1477346700,
  26. 'upload_date': '20161024',
  27. 'duration': 2766.602563,
  28. 'creator': 'Anton Berg & Martin Johnson',
  29. 'series': 'Spår',
  30. 'episode': '2. Raggarmordet - Röster ur det förflutna',
  31. }
  32. }, {
  33. 'url': 'http://embed.acast.com/adambuxton/ep.12-adam-joeschristmaspodcast2015',
  34. 'only_matching': True,
  35. }]
  36. def _real_extract(self, url):
  37. channel, display_id = re.match(self._VALID_URL, url).groups()
  38. s = self._download_json(
  39. 'https://play-api.acast.com/stitch/%s/%s' % (channel, display_id),
  40. display_id)['result']
  41. media_url = s['url']
  42. cast_data = self._download_json(
  43. 'https://play-api.acast.com/splash/%s/%s' % (channel, display_id),
  44. display_id)['result']
  45. e = cast_data['episode']
  46. title = e['name']
  47. return {
  48. 'id': compat_str(e['id']),
  49. 'display_id': display_id,
  50. 'url': media_url,
  51. 'title': title,
  52. 'description': e.get('description') or e.get('summary'),
  53. 'thumbnail': e.get('image'),
  54. 'timestamp': unified_timestamp(e.get('publishingDate')),
  55. 'duration': float_or_none(s.get('duration') or e.get('duration')),
  56. 'filesize': int_or_none(e.get('contentLength')),
  57. 'creator': try_get(cast_data, lambda x: x['show']['author'], compat_str),
  58. 'series': try_get(cast_data, lambda x: x['show']['name'], compat_str),
  59. 'season_number': int_or_none(e.get('seasonNumber')),
  60. 'episode': title,
  61. 'episode_number': int_or_none(e.get('episodeNumber')),
  62. }
  63. class ACastChannelIE(InfoExtractor):
  64. IE_NAME = 'acast:channel'
  65. _VALID_URL = r'https?://(?:www\.)?acast\.com/(?P<id>[^/#?]+)'
  66. _TEST = {
  67. 'url': 'https://www.acast.com/condenasttraveler',
  68. 'info_dict': {
  69. 'id': '50544219-29bb-499e-a083-6087f4cb7797',
  70. 'title': 'Condé Nast Traveler Podcast',
  71. 'description': 'md5:98646dee22a5b386626ae31866638fbd',
  72. },
  73. 'playlist_mincount': 20,
  74. }
  75. _API_BASE_URL = 'https://www.acast.com/api/'
  76. _PAGE_SIZE = 10
  77. @classmethod
  78. def suitable(cls, url):
  79. return False if ACastIE.suitable(url) else super(ACastChannelIE, cls).suitable(url)
  80. def _fetch_page(self, channel_slug, page):
  81. casts = self._download_json(
  82. self._API_BASE_URL + 'channels/%s/acasts?page=%s' % (channel_slug, page),
  83. channel_slug, note='Download page %d of channel data' % page)
  84. for cast in casts:
  85. yield self.url_result(
  86. 'https://www.acast.com/%s/%s' % (channel_slug, cast['url']),
  87. 'ACast', cast['id'])
  88. def _real_extract(self, url):
  89. channel_slug = self._match_id(url)
  90. channel_data = self._download_json(
  91. self._API_BASE_URL + 'channels/%s' % channel_slug, channel_slug)
  92. entries = OnDemandPagedList(functools.partial(
  93. self._fetch_page, channel_slug), self._PAGE_SIZE)
  94. return self.playlist_result(entries, compat_str(
  95. channel_data['id']), channel_data['name'], channel_data.get('description'))