howstuffworks.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. class HowStuffWorksIE(InfoExtractor):
  4. _VALID_URL = r'https?://[\da-z-]+\.howstuffworks\.com/(?:[^/]+/)*\d+-(?P<id>.+?)-video\.htm'
  5. _TESTS = [
  6. {
  7. 'url': 'http://adventure.howstuffworks.com/39521-deadliest-catch-nautical-collision-video.htm',
  8. 'info_dict': {
  9. 'id': '553475',
  10. 'ext': 'mp4',
  11. 'title': 'Deadliest Catch: Nautical Collision',
  12. 'description': 'Check out this clip to get an exclusive look at the Deadliest Catch crew back in action.',
  13. 'thumbnail': 'http://s.hswstatic.com/gif/videos/480x360/39521.jpg',
  14. },
  15. },
  16. {
  17. 'url': 'http://adventure.howstuffworks.com/7199-survival-zone-food-and-water-in-the-savanna-video.htm',
  18. 'info_dict': {
  19. 'id': '453464',
  20. 'ext': 'mp4',
  21. 'title': 'Survival Zone: Food and Water In the Savanna',
  22. 'description': 'Learn how to find both food and water while trekking in the African savannah. In this video from the Discovery Channel.',
  23. 'thumbnail': 'http://s.hswstatic.com/gif/videos/480x360/7199.jpg',
  24. },
  25. },
  26. {
  27. 'url': 'http://entertainment.howstuffworks.com/arts/34476-time-warp-fire-breathing-disaster-video.htm',
  28. 'info_dict': {
  29. 'id': '487036',
  30. 'ext': 'mp4',
  31. 'title': 'Time Warp: Fire-Breathing Disaster',
  32. 'description': 'Fire-breathers are safe from flames when they angle their heads up as they blow -- see what would happen if they looked down instead. Check out this clip from Discovery\'s "Time Warp" series to learn more.',
  33. 'thumbnail': 'http://s.hswstatic.com/gif/videos/480x360/34476.jpg',
  34. },
  35. },
  36. ]
  37. def _extract_clip_info(self, key, clip_info, name=None, **kargs):
  38. if name is None:
  39. name = key
  40. return self._html_search_regex(
  41. r"\s*%s\s*: '?([^'\n]*[^'\n,])" % key, clip_info, name, **kargs)
  42. def _real_extract(self, url):
  43. display_id = self._match_id(url)
  44. webpage = self._download_webpage(url, display_id)
  45. clip_info = self._search_regex('(?s)var clip = {(.*?)};', webpage, 'clip info')
  46. video_id = self._extract_clip_info('content_id', clip_info, 'video id')
  47. formats = []
  48. m3u8_url = self._extract_clip_info('m3u8', clip_info, 'm3u8 url', default=None)
  49. if m3u8_url is not None:
  50. formats += self._extract_m3u8_formats(m3u8_url , video_id, 'mp4')
  51. mp4 = self._parse_json(
  52. self._extract_clip_info(
  53. 'mp4', clip_info, 'formats').replace('},]','}]'), video_id)
  54. for video in mp4:
  55. formats.append({
  56. 'url': video['src'],
  57. 'format_id': video['bitrate'],
  58. 'tbr': int(video['bitrate'].rstrip('k')),
  59. })
  60. self._sort_formats(formats)
  61. return {
  62. 'id': video_id,
  63. 'display_id': display_id,
  64. 'title': self._extract_clip_info('clip_title', clip_info, 'title'),
  65. 'description': self._extract_clip_info('caption', clip_info, 'description', fatal=False),
  66. 'thumbnail': self._extract_clip_info('video_still_url', clip_info, 'thumbnail'),
  67. 'duration': self._extract_clip_info('duration', clip_info),
  68. 'formats': formats,
  69. }