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.

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