webofstories.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. int_or_none,
  5. orderedSet,
  6. )
  7. class WebOfStoriesIE(InfoExtractor):
  8. _VALID_URL = r'https?://(?:www\.)?webofstories\.com/play/(?:[^/]+/)?(?P<id>[0-9]+)'
  9. _VIDEO_DOMAIN = 'http://eu-mobile.webofstories.com/'
  10. _GREAT_LIFE_STREAMER = 'rtmp://eu-cdn1.webofstories.com/cfx/st/'
  11. _USER_STREAMER = 'rtmp://eu-users.webofstories.com/cfx/st/'
  12. _TESTS = [{
  13. 'url': 'http://www.webofstories.com/play/hans.bethe/71',
  14. 'md5': '373e4dd915f60cfe3116322642ddf364',
  15. 'info_dict': {
  16. 'id': '4536',
  17. 'ext': 'mp4',
  18. 'title': 'The temperature of the sun',
  19. 'thumbnail': r're:^https?://.*\.jpg$',
  20. 'description': 'Hans Bethe talks about calculating the temperature of the sun',
  21. 'duration': 238,
  22. },
  23. }, {
  24. 'url': 'http://www.webofstories.com/play/55908',
  25. 'md5': '2985a698e1fe3211022422c4b5ed962c',
  26. 'info_dict': {
  27. 'id': '55908',
  28. 'ext': 'mp4',
  29. 'title': 'The story of Gemmata obscuriglobus',
  30. 'thumbnail': r're:^https?://.*\.jpg$',
  31. 'description': 'Planctomycete talks about The story of Gemmata obscuriglobus',
  32. 'duration': 169,
  33. },
  34. 'skip': 'notfound',
  35. }, {
  36. # malformed og:title meta
  37. 'url': 'http://www.webofstories.com/play/54215?o=MS',
  38. 'info_dict': {
  39. 'id': '54215',
  40. 'ext': 'mp4',
  41. 'title': '"A Leg to Stand On"',
  42. 'thumbnail': r're:^https?://.*\.jpg$',
  43. 'description': 'Oliver Sacks talks about the death and resurrection of a limb',
  44. 'duration': 97,
  45. },
  46. 'params': {
  47. 'skip_download': True,
  48. },
  49. }]
  50. def _real_extract(self, url):
  51. video_id = self._match_id(url)
  52. webpage = self._download_webpage(url, video_id)
  53. # Sometimes og:title meta is malformed
  54. title = self._og_search_title(webpage, default=None) or self._html_search_regex(
  55. r'(?s)<strong>Title:\s*</strong>(.+?)<', webpage, 'title')
  56. description = self._html_search_meta('description', webpage)
  57. thumbnail = self._og_search_thumbnail(webpage)
  58. embed_params = [s.strip(" \r\n\t'") for s in self._search_regex(
  59. r'(?s)\$\("#embedCode"\).html\(getEmbedCode\((.*?)\)',
  60. webpage, 'embed params').split(',')]
  61. (
  62. _, speaker_id, story_id, story_duration,
  63. speaker_type, great_life, _thumbnail, _has_subtitles,
  64. story_filename, _story_order) = embed_params
  65. is_great_life_series = great_life == 'true'
  66. duration = int_or_none(story_duration)
  67. # URL building, see: http://www.webofstories.com/scripts/player.js
  68. ms_prefix = ''
  69. if speaker_type.lower() == 'ms':
  70. ms_prefix = 'mini_sites/'
  71. if is_great_life_series:
  72. mp4_url = f'{self._VIDEO_DOMAIN}lives/{speaker_id}/{story_filename}.mp4'
  73. rtmp_ext = 'flv'
  74. streamer = self._GREAT_LIFE_STREAMER
  75. play_path = f'stories/{speaker_id}/{story_filename}'
  76. else:
  77. mp4_url = f'{self._VIDEO_DOMAIN}{ms_prefix}{speaker_id}/{story_filename}.mp4'
  78. rtmp_ext = 'mp4'
  79. streamer = self._USER_STREAMER
  80. play_path = f'mp4:{ms_prefix}{speaker_id}/{story_filename}.mp4'
  81. formats = [{
  82. 'format_id': 'mp4_sd',
  83. 'url': mp4_url,
  84. }, {
  85. 'format_id': 'rtmp_sd',
  86. 'page_url': url,
  87. 'url': streamer,
  88. 'ext': rtmp_ext,
  89. 'play_path': play_path,
  90. }]
  91. return {
  92. 'id': story_id,
  93. 'title': title,
  94. 'formats': formats,
  95. 'thumbnail': thumbnail,
  96. 'description': description,
  97. 'duration': duration,
  98. }
  99. class WebOfStoriesPlaylistIE(InfoExtractor):
  100. _VALID_URL = r'https?://(?:www\.)?webofstories\.com/playAll/(?P<id>[^/]+)'
  101. _TEST = {
  102. 'url': 'http://www.webofstories.com/playAll/donald.knuth',
  103. 'info_dict': {
  104. 'id': 'donald.knuth',
  105. 'title': 'Donald Knuth (Scientist)',
  106. },
  107. 'playlist_mincount': 97,
  108. }
  109. def _real_extract(self, url):
  110. playlist_id = self._match_id(url)
  111. webpage = self._download_webpage(url, playlist_id)
  112. entries = [
  113. self.url_result(
  114. f'http://www.webofstories.com/play/{video_id}',
  115. 'WebOfStories', video_id=video_id)
  116. for video_id in orderedSet(re.findall(r'\bid=["\']td_(\d+)', webpage))
  117. ]
  118. title = self._search_regex(
  119. r'<div id="speakerName">\s*<span>([^<]+)</span>',
  120. webpage, 'speaker', default=None)
  121. if title:
  122. field = self._search_regex(
  123. r'<span id="primaryField">([^<]+)</span>',
  124. webpage, 'field', default=None)
  125. if field:
  126. title += f' ({field})'
  127. if not title:
  128. title = self._search_regex(
  129. r'<title>Play\s+all\s+stories\s*-\s*([^<]+)\s*-\s*Web\s+of\s+Stories</title>',
  130. webpage, 'title')
  131. return self.playlist_result(entries, playlist_id, title)