applepodcasts.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. clean_html,
  4. clean_podcast_url,
  5. get_element_by_class,
  6. int_or_none,
  7. parse_iso8601,
  8. try_get,
  9. )
  10. class ApplePodcastsIE(InfoExtractor):
  11. _VALID_URL = r'https?://podcasts\.apple\.com/(?:[^/]+/)?podcast(?:/[^/]+){1,2}.*?\bi=(?P<id>\d+)'
  12. _TESTS = [{
  13. 'url': 'https://podcasts.apple.com/us/podcast/207-whitney-webb-returns/id1135137367?i=1000482637777',
  14. 'md5': '41dc31cd650143e530d9423b6b5a344f',
  15. 'info_dict': {
  16. 'id': '1000482637777',
  17. 'ext': 'mp3',
  18. 'title': '207 - Whitney Webb Returns',
  19. 'description': 'md5:75ef4316031df7b41ced4e7b987f79c6',
  20. 'upload_date': '20200705',
  21. 'timestamp': 1593932400,
  22. 'duration': 6454,
  23. 'series': 'The Tim Dillon Show',
  24. 'thumbnail': 're:.+[.](png|jpe?g|webp)',
  25. },
  26. }, {
  27. 'url': 'https://podcasts.apple.com/podcast/207-whitney-webb-returns/id1135137367?i=1000482637777',
  28. 'only_matching': True,
  29. }, {
  30. 'url': 'https://podcasts.apple.com/podcast/207-whitney-webb-returns?i=1000482637777',
  31. 'only_matching': True,
  32. }, {
  33. 'url': 'https://podcasts.apple.com/podcast/id1135137367?i=1000482637777',
  34. 'only_matching': True,
  35. }]
  36. def _real_extract(self, url):
  37. episode_id = self._match_id(url)
  38. webpage = self._download_webpage(url, episode_id)
  39. episode_data = {}
  40. ember_data = {}
  41. # new page type 2021-11
  42. amp_data = self._parse_json(self._search_regex(
  43. r'(?s)id="shoebox-media-api-cache-amp-podcasts"[^>]*>\s*({.+?})\s*<',
  44. webpage, 'AMP data', default='{}'), episode_id, fatal=False) or {}
  45. amp_data = try_get(amp_data,
  46. lambda a: self._parse_json(
  47. next(a[x] for x in iter(a) if episode_id in x),
  48. episode_id),
  49. dict) or {}
  50. amp_data = amp_data.get('d') or []
  51. episode_data = try_get(
  52. amp_data,
  53. lambda a: next(x for x in a
  54. if x['type'] == 'podcast-episodes' and x['id'] == episode_id),
  55. dict)
  56. if not episode_data:
  57. # try pre 2021-11 page type: TODO: consider deleting if no longer used
  58. ember_data = self._parse_json(self._search_regex(
  59. r'(?s)id="shoebox-ember-data-store"[^>]*>\s*({.+?})\s*<',
  60. webpage, 'ember data'), episode_id) or {}
  61. ember_data = ember_data.get(episode_id) or ember_data
  62. episode_data = try_get(ember_data, lambda x: x['data'], dict)
  63. episode = episode_data['attributes']
  64. description = episode.get('description') or {}
  65. series = None
  66. for inc in (amp_data or ember_data.get('included') or []):
  67. if inc.get('type') == 'media/podcast':
  68. series = try_get(inc, lambda x: x['attributes']['name'])
  69. series = series or clean_html(get_element_by_class('podcast-header__identity', webpage))
  70. return {
  71. 'id': episode_id,
  72. 'title': episode.get('name'),
  73. 'url': clean_podcast_url(episode['assetUrl']),
  74. 'description': description.get('standard') or description.get('short'),
  75. 'timestamp': parse_iso8601(episode.get('releaseDateTime')),
  76. 'duration': int_or_none(episode.get('durationInMilliseconds'), 1000),
  77. 'series': series,
  78. 'thumbnail': self._og_search_thumbnail(webpage),
  79. 'vcodec': 'none',
  80. }