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