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.

90 lines
3.4 KiB

  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|stuff(?:(?:youshould|theydontwantyouto)know|toblowyourmind|momnevertoldyou)|(?:brain|car)stuffshow|fwthinking|geniusstuff)\.com/(?:[^/]+/)*(?:\d+-)?(?P<id>.+?)-video\.htm'
  12. _TESTS = [
  13. {
  14. 'url': 'http://www.stufftoblowyourmind.com/videos/optical-illusions-video.htm',
  15. 'md5': '76646a5acc0c92bf7cd66751ca5db94d',
  16. 'info_dict': {
  17. 'id': '855410',
  18. 'ext': 'mp4',
  19. 'title': 'Your Trickster Brain: Optical Illusions -- Science on the Web',
  20. 'description': 'md5:e374ff9561f6833ad076a8cc0a5ab2fb',
  21. },
  22. },
  23. {
  24. 'url': 'http://shows.howstuffworks.com/more-shows/why-does-balloon-stick-to-hair-video.htm',
  25. 'only_matching': True,
  26. }
  27. ]
  28. def _real_extract(self, url):
  29. display_id = self._match_id(url)
  30. webpage = self._download_webpage(url, display_id)
  31. clip_js = self._search_regex(
  32. r'(?s)var clip = ({.*?});', webpage, 'clip info')
  33. clip_info = self._parse_json(
  34. clip_js, display_id, transform_source=js_to_json)
  35. video_id = clip_info['content_id']
  36. formats = []
  37. m3u8_url = clip_info.get('m3u8')
  38. if m3u8_url and determine_ext(m3u8_url) == 'm3u8':
  39. formats.extend(self._extract_m3u8_formats(m3u8_url, video_id, 'mp4', format_id='hls', fatal=True))
  40. flv_url = clip_info.get('flv_url')
  41. if flv_url:
  42. formats.append({
  43. 'url': flv_url,
  44. 'format_id': 'flv',
  45. })
  46. for video in clip_info.get('mp4', []):
  47. formats.append({
  48. 'url': video['src'],
  49. 'format_id': 'mp4-%s' % video['bitrate'],
  50. 'vbr': int_or_none(video['bitrate'].rstrip('k')),
  51. })
  52. if not formats:
  53. smil = self._download_xml(
  54. 'http://services.media.howstuffworks.com/videos/%s/smil-service.smil' % video_id,
  55. video_id, 'Downloading video SMIL')
  56. http_base = find_xpath_attr(
  57. smil,
  58. './{0}head/{0}meta'.format('{http://www.w3.org/2001/SMIL20/Language}'),
  59. 'name',
  60. 'httpBase').get('content')
  61. URL_SUFFIX = '?v=2.11.3&fp=LNX 11,2,202,356&r=A&g=A'
  62. for video in smil.findall(
  63. './{0}body/{0}switch/{0}video'.format('{http://www.w3.org/2001/SMIL20/Language}')):
  64. vbr = int_or_none(video.attrib['system-bitrate'], scale=1000)
  65. formats.append({
  66. 'url': '%s/%s%s' % (http_base, video.attrib['src'], URL_SUFFIX),
  67. 'format_id': '%dk' % vbr,
  68. 'vbr': vbr,
  69. })
  70. self._sort_formats(formats)
  71. return {
  72. 'id': '%s' % video_id,
  73. 'display_id': display_id,
  74. 'title': unescapeHTML(clip_info['clip_title']),
  75. 'description': unescapeHTML(clip_info.get('caption')),
  76. 'thumbnail': clip_info.get('video_still_url'),
  77. 'duration': int_or_none(clip_info.get('duration')),
  78. 'formats': formats,
  79. }