webofstories.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 = '{0:}lives/{1:}/{2:}.mp4'.format(
  73. self._VIDEO_DOMAIN, speaker_id, story_filename)
  74. rtmp_ext = 'flv'
  75. streamer = self._GREAT_LIFE_STREAMER
  76. play_path = 'stories/{0:}/{1:}'.format(
  77. speaker_id, story_filename)
  78. else:
  79. mp4_url = '{0:}{1:}{2:}/{3:}.mp4'.format(
  80. self._VIDEO_DOMAIN, ms_prefix, speaker_id, story_filename)
  81. rtmp_ext = 'mp4'
  82. streamer = self._USER_STREAMER
  83. play_path = 'mp4:{0:}{1:}/{2}.mp4'.format(
  84. ms_prefix, speaker_id, story_filename)
  85. formats = [{
  86. 'format_id': 'mp4_sd',
  87. 'url': mp4_url,
  88. }, {
  89. 'format_id': 'rtmp_sd',
  90. 'page_url': url,
  91. 'url': streamer,
  92. 'ext': rtmp_ext,
  93. 'play_path': play_path,
  94. }]
  95. return {
  96. 'id': story_id,
  97. 'title': title,
  98. 'formats': formats,
  99. 'thumbnail': thumbnail,
  100. 'description': description,
  101. 'duration': duration,
  102. }
  103. class WebOfStoriesPlaylistIE(InfoExtractor):
  104. _VALID_URL = r'https?://(?:www\.)?webofstories\.com/playAll/(?P<id>[^/]+)'
  105. _TEST = {
  106. 'url': 'http://www.webofstories.com/playAll/donald.knuth',
  107. 'info_dict': {
  108. 'id': 'donald.knuth',
  109. 'title': 'Donald Knuth (Scientist)',
  110. },
  111. 'playlist_mincount': 97,
  112. }
  113. def _real_extract(self, url):
  114. playlist_id = self._match_id(url)
  115. webpage = self._download_webpage(url, playlist_id)
  116. entries = [
  117. self.url_result(
  118. 'http://www.webofstories.com/play/%s' % video_id,
  119. 'WebOfStories', video_id=video_id)
  120. for video_id in orderedSet(re.findall(r'\bid=["\']td_(\d+)', webpage))
  121. ]
  122. title = self._search_regex(
  123. r'<div id="speakerName">\s*<span>([^<]+)</span>',
  124. webpage, 'speaker', default=None)
  125. if title:
  126. field = self._search_regex(
  127. r'<span id="primaryField">([^<]+)</span>',
  128. webpage, 'field', default=None)
  129. if field:
  130. title += ' (%s)' % field
  131. if not title:
  132. title = self._search_regex(
  133. r'<title>Play\s+all\s+stories\s*-\s*([^<]+)\s*-\s*Web\s+of\s+Stories</title>',
  134. webpage, 'title')
  135. return self.playlist_result(entries, playlist_id, title)