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.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. },
  25. {
  26. 'url': 'http://adventure.howstuffworks.com/7199-survival-zone-food-and-water-in-the-savanna-video.htm',
  27. 'info_dict': {
  28. 'id': '453464',
  29. 'ext': 'mp4',
  30. 'title': 'Survival Zone: Food and Water In the Savanna',
  31. 'description': 'Learn how to find both food and water while trekking in the African savannah. In this video from the Discovery Channel.',
  32. 'display_id': 'survival-zone-food-and-water-in-the-savanna',
  33. 'thumbnail': 're:^https?://.*\.jpg$',
  34. },
  35. },
  36. {
  37. 'url': 'http://entertainment.howstuffworks.com/arts/2706-sword-swallowing-1-by-dan-meyer-video.htm',
  38. 'info_dict': {
  39. 'id': '440011',
  40. 'ext': 'mp4',
  41. 'title': 'Sword Swallowing #1 by Dan Meyer',
  42. 'description': 'Video footage (1 of 3) used by permission of the owner Dan Meyer through Sword Swallowers Association International <www.swordswallow.org>',
  43. 'display_id': 'sword-swallowing-1-by-dan-meyer',
  44. 'thumbnail': 're:^https?://.*\.jpg$',
  45. },
  46. },
  47. {
  48. 'url': 'http://shows.howstuffworks.com/stuff-to-blow-your-mind/optical-illusions-video.htm',
  49. 'only_matching': True,
  50. }
  51. ]
  52. def _real_extract(self, url):
  53. display_id = self._match_id(url)
  54. webpage = self._download_webpage(url, display_id)
  55. clip_js = self._search_regex(
  56. r'(?s)var clip = ({.*?});', webpage, 'clip info')
  57. clip_info = self._parse_json(
  58. clip_js, display_id, transform_source=js_to_json)
  59. video_id = clip_info['content_id']
  60. formats = []
  61. m3u8_url = clip_info.get('m3u8')
  62. if m3u8_url and determine_ext(m3u8_url) == 'm3u8':
  63. formats.extend(self._extract_m3u8_formats(m3u8_url, video_id, 'mp4', format_id='hls', fatal=True))
  64. flv_url = clip_info.get('flv_url')
  65. if flv_url:
  66. formats.append({
  67. 'url': flv_url,
  68. 'format_id': 'flv',
  69. })
  70. for video in clip_info.get('mp4', []):
  71. formats.append({
  72. 'url': video['src'],
  73. 'format_id': 'mp4-%s' % video['bitrate'],
  74. 'vbr': int_or_none(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': int_or_none(clip_info.get('duration')),
  102. 'formats': formats,
  103. }