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