howstuffworks.py 3.3 KB

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