You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.8 KiB

10 years ago
10 years ago
10 years ago
  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. find_xpath_attr,
  5. int_or_none,
  6. js_to_json,
  7. unescapeHTML,
  8. )
  9. class HowStuffWorksIE(InfoExtractor):
  10. _VALID_URL = r'https?://[\da-z-]+\.howstuffworks\.com/(?:[^/]+/)*(?:\d+-)?(?P<id>.+?)-video\.htm'
  11. _TESTS = [
  12. {
  13. 'url': 'http://adventure.howstuffworks.com/5266-cool-jobs-iditarod-musher-video.htm',
  14. 'info_dict': {
  15. 'id': '450221',
  16. 'ext': 'flv',
  17. 'title': 'Cool Jobs - Iditarod Musher',
  18. 'description': 'Cold sleds, freezing temps and warm dog breath... an Iditarod musher\'s dream. Kasey-Dee Gardner jumps on a sled to find out what the big deal is.',
  19. 'display_id': 'cool-jobs-iditarod-musher',
  20. 'thumbnail': 're:^https?://.*\.jpg$',
  21. 'duration': 161,
  22. },
  23. },
  24. {
  25. 'url': 'http://adventure.howstuffworks.com/7199-survival-zone-food-and-water-in-the-savanna-video.htm',
  26. 'info_dict': {
  27. 'id': '453464',
  28. 'ext': 'mp4',
  29. 'title': 'Survival Zone: Food and Water In the Savanna',
  30. 'description': 'Learn how to find both food and water while trekking in the African savannah. In this video from the Discovery Channel.',
  31. 'display_id': 'survival-zone-food-and-water-in-the-savanna',
  32. 'thumbnail': 're:^https?://.*\.jpg$',
  33. },
  34. },
  35. {
  36. 'url': 'http://entertainment.howstuffworks.com/arts/2706-sword-swallowing-1-by-dan-meyer-video.htm',
  37. 'info_dict': {
  38. 'id': '440011',
  39. 'ext': 'flv',
  40. 'title': 'Sword Swallowing #1 by Dan Meyer',
  41. 'description': 'Video footage (1 of 3) used by permission of the owner Dan Meyer through Sword Swallowers Association International <www.swordswallow.org>',
  42. 'display_id': 'sword-swallowing-1-by-dan-meyer',
  43. 'thumbnail': 're:^https?://.*\.jpg$',
  44. },
  45. },
  46. {
  47. 'url': 'http://shows.howstuffworks.com/stuff-to-blow-your-mind/optical-illusions-video.htm',
  48. 'info_dict': {
  49. 'id': '855410',
  50. 'ext': 'mp4',
  51. 'title': 'Stuff to Blow Your Mind',
  52. 'description': 'When it comes to optical illusions it’s pretty easy to jailbreak the operating system of your mind and load a new program onto it. Find out why your visual system willingly makes errors in interpretation.',
  53. 'display_id': 'optical-illusions',
  54. 'thumbnail': 're:^https?://.*\.jpg$',
  55. },
  56. }
  57. ]
  58. def _real_extract(self, url):
  59. display_id = self._match_id(url)
  60. webpage = self._download_webpage(url, display_id)
  61. clip_js = self._search_regex(
  62. r'(?s)var clip = ({.*?});', webpage, 'clip info')
  63. clip_info = self._parse_json(
  64. clip_js, display_id, transform_source=js_to_json)
  65. video_id = clip_info['content_id']
  66. formats = []
  67. m3u8_url = clip_info.get('m3u8')
  68. if m3u8_url:
  69. formats += self._extract_m3u8_formats(m3u8_url, video_id, 'mp4')
  70. for video in clip_info.get('mp4', []):
  71. formats.append({
  72. 'url': video['src'],
  73. 'format_id': video['bitrate'],
  74. 'vbr': int(video['bitrate'].rstrip('k')),
  75. })
  76. if not formats:
  77. smil = self._download_xml(
  78. 'http://services.media.howstuffworks.com/videos/%s/smil-service.smil' % video_id,
  79. video_id, 'Downloading video SMIL')
  80. http_base = find_xpath_attr(
  81. smil,
  82. './{0}head/{0}meta'.format('{http://www.w3.org/2001/SMIL20/Language}'),
  83. 'name',
  84. 'httpBase').get('content')
  85. URL_SUFFIX = '?v=2.11.3&fp=LNX 11,2,202,356&r=A&g=A'
  86. for video in smil.findall(
  87. './{0}body/{0}switch/{0}video'.format('{http://www.w3.org/2001/SMIL20/Language}')):
  88. vbr = int_or_none(video.attrib['system-bitrate'], scale=1000)
  89. formats.append({
  90. 'url': '%s/%s%s' % (http_base, video.attrib['src'], URL_SUFFIX),
  91. 'format_id': '%dk' % vbr,
  92. 'vbr': vbr,
  93. })
  94. self._sort_formats(formats)
  95. return {
  96. 'id': '%s' % video_id,
  97. 'display_id': display_id,
  98. 'title': unescapeHTML(clip_info['clip_title']),
  99. 'description': unescapeHTML(clip_info.get('caption')),
  100. 'thumbnail': clip_info.get('video_still_url'),
  101. 'duration': clip_info.get('duration'),
  102. 'formats': formats,
  103. }