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.

107 lines
4.3 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. 'only_matching': True,
  49. }
  50. ]
  51. def _real_extract(self, url):
  52. display_id = self._match_id(url)
  53. webpage = self._download_webpage(url, display_id)
  54. clip_js = self._search_regex(
  55. r'(?s)var clip = ({.*?});', webpage, 'clip info')
  56. clip_info = self._parse_json(
  57. clip_js, display_id, transform_source=js_to_json)
  58. video_id = clip_info['content_id']
  59. formats = []
  60. m3u8_url = clip_info.get('m3u8')
  61. if m3u8_url:
  62. formats += self._extract_m3u8_formats(m3u8_url, video_id, 'mp4')
  63. for video in clip_info.get('mp4', []):
  64. formats.append({
  65. 'url': video['src'],
  66. 'format_id': video['bitrate'],
  67. 'vbr': int(video['bitrate'].rstrip('k')),
  68. })
  69. if not formats:
  70. smil = self._download_xml(
  71. 'http://services.media.howstuffworks.com/videos/%s/smil-service.smil' % video_id,
  72. video_id, 'Downloading video SMIL')
  73. http_base = find_xpath_attr(
  74. smil,
  75. './{0}head/{0}meta'.format('{http://www.w3.org/2001/SMIL20/Language}'),
  76. 'name',
  77. 'httpBase').get('content')
  78. URL_SUFFIX = '?v=2.11.3&fp=LNX 11,2,202,356&r=A&g=A'
  79. for video in smil.findall(
  80. './{0}body/{0}switch/{0}video'.format('{http://www.w3.org/2001/SMIL20/Language}')):
  81. vbr = int_or_none(video.attrib['system-bitrate'], scale=1000)
  82. formats.append({
  83. 'url': '%s/%s%s' % (http_base, video.attrib['src'], URL_SUFFIX),
  84. 'format_id': '%dk' % vbr,
  85. 'vbr': vbr,
  86. })
  87. self._sort_formats(formats)
  88. return {
  89. 'id': '%s' % video_id,
  90. 'display_id': display_id,
  91. 'title': unescapeHTML(clip_info['clip_title']),
  92. 'description': unescapeHTML(clip_info.get('caption')),
  93. 'thumbnail': clip_info.get('video_still_url'),
  94. 'duration': clip_info.get('duration'),
  95. 'formats': formats,
  96. }