acast.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. int_or_none,
  9. parse_iso8601,
  10. OnDemandPagedList,
  11. )
  12. class ACastIE(InfoExtractor):
  13. IE_NAME = 'acast'
  14. _VALID_URL = r'https?://(?:www\.)?acast\.com/(?P<channel>[^/]+)/(?P<id>[^/#?]+)'
  15. _TEST = {
  16. 'url': 'https://www.acast.com/condenasttraveler/-where-are-you-taipei-101-taiwan',
  17. 'md5': 'ada3de5a1e3a2a381327d749854788bb',
  18. 'info_dict': {
  19. 'id': '57de3baa-4bb0-487e-9418-2692c1277a34',
  20. 'ext': 'mp3',
  21. 'title': '"Where Are You?": Taipei 101, Taiwan',
  22. 'timestamp': 1196172000,
  23. 'upload_date': '20071127',
  24. 'description': 'md5:a0b4ef3634e63866b542e5b1199a1a0e',
  25. 'duration': 211,
  26. }
  27. }
  28. def _real_extract(self, url):
  29. channel, display_id = re.match(self._VALID_URL, url).groups()
  30. cast_data = self._download_json(
  31. 'https://embed.acast.com/api/acasts/%s/%s' % (channel, display_id), display_id)
  32. return {
  33. 'id': compat_str(cast_data['id']),
  34. 'display_id': display_id,
  35. 'url': [b['audio'] for b in cast_data['blings'] if b['type'] == 'BlingAudio'][0],
  36. 'title': cast_data['name'],
  37. 'description': cast_data.get('description'),
  38. 'thumbnail': cast_data.get('image'),
  39. 'timestamp': parse_iso8601(cast_data.get('publishingDate')),
  40. 'duration': int_or_none(cast_data.get('duration')),
  41. }
  42. class ACastChannelIE(InfoExtractor):
  43. IE_NAME = 'acast:channel'
  44. _VALID_URL = r'https?://(?:www\.)?acast\.com/(?P<id>[^/#?]+)'
  45. _TEST = {
  46. 'url': 'https://www.acast.com/condenasttraveler',
  47. 'info_dict': {
  48. 'id': '50544219-29bb-499e-a083-6087f4cb7797',
  49. 'title': 'Condé Nast Traveler Podcast',
  50. 'description': 'md5:98646dee22a5b386626ae31866638fbd',
  51. },
  52. 'playlist_mincount': 20,
  53. }
  54. _API_BASE_URL = 'https://www.acast.com/api/'
  55. _PAGE_SIZE = 10
  56. @classmethod
  57. def suitable(cls, url):
  58. return False if ACastIE.suitable(url) else super(ACastChannelIE, cls).suitable(url)
  59. def _fetch_page(self, channel_slug, page):
  60. casts = self._download_json(
  61. self._API_BASE_URL + 'channels/%s/acasts?page=%s' % (channel_slug, page),
  62. channel_slug, note='Download page %d of channel data' % page)
  63. for cast in casts:
  64. yield self.url_result(
  65. 'https://www.acast.com/%s/%s' % (channel_slug, cast['url']),
  66. 'ACast', cast['id'])
  67. def _real_extract(self, url):
  68. channel_slug = self._match_id(url)
  69. channel_data = self._download_json(
  70. self._API_BASE_URL + 'channels/%s' % channel_slug, channel_slug)
  71. entries = OnDemandPagedList(functools.partial(
  72. self._fetch_page, channel_slug), self._PAGE_SIZE)
  73. return self.playlist_result(entries, compat_str(
  74. channel_data['id']), channel_data['name'], channel_data.get('description'))