googlepodcasts.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import json
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. clean_podcast_url,
  5. int_or_none,
  6. try_get,
  7. urlencode_postdata,
  8. )
  9. class GooglePodcastsBaseIE(InfoExtractor):
  10. _VALID_URL_BASE = r'https?://podcasts\.google\.com/feed/'
  11. def _batch_execute(self, func_id, video_id, params):
  12. return json.loads(self._download_json(
  13. 'https://podcasts.google.com/_/PodcastsUi/data/batchexecute',
  14. video_id, data=urlencode_postdata({
  15. 'f.req': json.dumps([[[func_id, json.dumps(params), None, '1']]]),
  16. }), transform_source=lambda x: self._search_regex(r'(?s)(\[.+\])', x, 'data'))[0][2])
  17. def _extract_episode(self, episode):
  18. return {
  19. 'id': episode[4][3],
  20. 'title': episode[8],
  21. 'url': clean_podcast_url(episode[13]),
  22. 'thumbnail': episode[2],
  23. 'description': episode[9],
  24. 'creator': try_get(episode, lambda x: x[14]),
  25. 'timestamp': int_or_none(episode[11]),
  26. 'duration': int_or_none(episode[12]),
  27. 'series': episode[1],
  28. }
  29. class GooglePodcastsIE(GooglePodcastsBaseIE):
  30. IE_NAME = 'google:podcasts'
  31. _VALID_URL = GooglePodcastsBaseIE._VALID_URL_BASE + r'(?P<feed_url>[^/]+)/episode/(?P<id>[^/?&#]+)'
  32. _TEST = {
  33. 'url': 'https://podcasts.google.com/feed/aHR0cHM6Ly9mZWVkcy5ucHIub3JnLzM0NDA5ODUzOS9wb2RjYXN0LnhtbA/episode/MzBlNWRlN2UtOWE4Yy00ODcwLTk2M2MtM2JlMmUyNmViOTRh',
  34. 'md5': 'fa56b2ee8bd0703e27e42d4b104c4766',
  35. 'info_dict': {
  36. 'id': '30e5de7e-9a8c-4870-963c-3be2e26eb94a',
  37. 'ext': 'mp3',
  38. 'title': 'WWDTM New Year 2021',
  39. 'description': 'We say goodbye to 2020 with Christine Baranksi, Doug Jones, Jonna Mendez, and Kellee Edwards.',
  40. 'upload_date': '20210102',
  41. 'timestamp': 1609606800,
  42. 'duration': 2901,
  43. 'series': "Wait Wait... Don't Tell Me!",
  44. },
  45. }
  46. def _real_extract(self, url):
  47. b64_feed_url, b64_guid = self._match_valid_url(url).groups()
  48. episode = self._batch_execute(
  49. 'oNjqVe', b64_guid, [b64_feed_url, b64_guid])[1]
  50. return self._extract_episode(episode)
  51. class GooglePodcastsFeedIE(GooglePodcastsBaseIE):
  52. IE_NAME = 'google:podcasts:feed'
  53. _VALID_URL = GooglePodcastsBaseIE._VALID_URL_BASE + r'(?P<id>[^/?&#]+)/?(?:[?#&]|$)'
  54. _TEST = {
  55. 'url': 'https://podcasts.google.com/feed/aHR0cHM6Ly9mZWVkcy5ucHIub3JnLzM0NDA5ODUzOS9wb2RjYXN0LnhtbA',
  56. 'info_dict': {
  57. 'title': "Wait Wait... Don't Tell Me!",
  58. 'description': "NPR's weekly current events quiz. Have a laugh and test your news knowledge while figuring out what's real and what we've made up.",
  59. },
  60. 'playlist_mincount': 20,
  61. }
  62. def _real_extract(self, url):
  63. b64_feed_url = self._match_id(url)
  64. data = self._batch_execute('ncqJEe', b64_feed_url, [b64_feed_url])
  65. entries = []
  66. for episode in (try_get(data, lambda x: x[1][0]) or []):
  67. entries.append(self._extract_episode(episode))
  68. feed = try_get(data, lambda x: x[3]) or []
  69. return self.playlist_result(
  70. entries, playlist_title=try_get(feed, lambda x: x[0]),
  71. playlist_description=try_get(feed, lambda x: x[2]))