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.

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