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