libsyn.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. clean_html,
  4. get_element_by_class,
  5. parse_duration,
  6. strip_or_none,
  7. unified_strdate,
  8. )
  9. class LibsynIE(InfoExtractor):
  10. _VALID_URL = r'(?P<mainurl>https?://html5-player\.libsyn\.com/embed/episode/id/(?P<id>[0-9]+))'
  11. _EMBED_REGEX = [r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//html5-player\.libsyn\.com/embed/.+?)\1']
  12. _TESTS = [{
  13. 'url': 'http://html5-player.libsyn.com/embed/episode/id/6385796/',
  14. 'md5': '2a55e75496c790cdeb058e7e6c087746',
  15. 'info_dict': {
  16. 'id': '6385796',
  17. 'ext': 'mp3',
  18. 'title': 'Champion Minded - Developing a Growth Mindset',
  19. # description fetched using another request:
  20. # http://html5-player.libsyn.com/embed/getitemdetails?item_id=6385796
  21. # 'description': 'In this episode, Allistair talks about the importance of developing a growth mindset, not only in sports, but in life too.',
  22. 'upload_date': '20180320',
  23. 'thumbnail': 're:^https?://.*',
  24. },
  25. }, {
  26. 'url': 'https://html5-player.libsyn.com/embed/episode/id/3727166/height/75/width/200/theme/standard/direction/no/autoplay/no/autonext/no/thumbnail/no/preload/no/no_addthis/no/',
  27. 'md5': '6c5cb21acd622d754d3b1a92b582ce42',
  28. 'info_dict': {
  29. 'id': '3727166',
  30. 'ext': 'mp3',
  31. 'title': 'Clients From Hell Podcast - How a Sex Toy Company Kickstarted my Freelance Career',
  32. 'upload_date': '20150818',
  33. 'thumbnail': 're:^https?://.*',
  34. },
  35. }]
  36. def _real_extract(self, url):
  37. url, video_id = self._match_valid_url(url).groups()
  38. webpage = self._download_webpage(url, video_id)
  39. data = self._parse_json(self._search_regex(
  40. r'var\s+playlistItem\s*=\s*({.+?});',
  41. webpage, 'JSON data block'), video_id)
  42. episode_title = data.get('item_title') or get_element_by_class('episode-title', webpage)
  43. if not episode_title:
  44. self._search_regex(
  45. [r'data-title="([^"]+)"', r'<title>(.+?)</title>'],
  46. webpage, 'episode title')
  47. episode_title = episode_title.strip()
  48. podcast_title = strip_or_none(clean_html(self._search_regex(
  49. r'<h3>([^<]+)</h3>', webpage, 'podcast title',
  50. default=None) or get_element_by_class('podcast-title', webpage)))
  51. title = f'{podcast_title} - {episode_title}' if podcast_title else episode_title
  52. formats = []
  53. for k, format_id in (('media_url_libsyn', 'libsyn'), ('media_url', 'main'), ('download_link', 'download')):
  54. f_url = data.get(k)
  55. if not f_url:
  56. continue
  57. formats.append({
  58. 'url': f_url,
  59. 'format_id': format_id,
  60. })
  61. description = self._html_search_regex(
  62. r'<p\s+id="info_text_body">(.+?)</p>', webpage,
  63. 'description', default=None)
  64. if description:
  65. # Strip non-breaking and normal spaces
  66. description = description.replace('\u00A0', ' ').strip()
  67. release_date = unified_strdate(self._search_regex(
  68. r'<div class="release_date">Released: ([^<]+)<',
  69. webpage, 'release date', default=None) or data.get('release_date'))
  70. return {
  71. 'id': video_id,
  72. 'title': title,
  73. 'description': description,
  74. 'thumbnail': data.get('thumbnail_url'),
  75. 'upload_date': release_date,
  76. 'duration': parse_duration(data.get('duration')),
  77. 'formats': formats,
  78. }