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