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.

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