[youtube] Fix 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|stuff(?:(?:youshould|theydontwantyouto)know|toblowyourmind|momnevertoldyou)|(?:brain|car)stuffshow|fwthinking|geniusstuff)\.com/(?:[^/]+/)*(?:\d+-)?(?P<id>.+?)-video\.htm'
15     _TESTS = [
16         {
17             'url': 'http://www.stufftoblowyourmind.com/videos/optical-illusions-video.htm',
18             'md5': '76646a5acc0c92bf7cd66751ca5db94d',
19             'info_dict': {
20                 'id': '855410',
21                 'ext': 'mp4',
22                 'title': 'Your Trickster Brain: Optical Illusions -- Science on the Web',
23                 'description': 'md5:e374ff9561f6833ad076a8cc0a5ab2fb',
24             },
25         },
26         {
27             'url': 'http://shows.howstuffworks.com/more-shows/why-does-balloon-stick-to-hair-video.htm',
28             'only_matching': True,
29         }
30     ]
31
32     def _real_extract(self, url):
33         display_id = self._match_id(url)
34         webpage = self._download_webpage(url, display_id)
35         clip_js = self._search_regex(
36             r'(?s)var clip = ({.*?});', webpage, 'clip info')
37         clip_info = self._parse_json(
38             clip_js, display_id, transform_source=js_to_json)
39
40         video_id = clip_info['content_id']
41         formats = []
42         m3u8_url = clip_info.get('m3u8')
43         if m3u8_url and determine_ext(m3u8_url) == 'm3u8':
44             formats.extend(self._extract_m3u8_formats(m3u8_url, video_id, 'mp4', format_id='hls', fatal=True))
45         flv_url = clip_info.get('flv_url')
46         if flv_url:
47             formats.append({
48                 'url': flv_url,
49                 'format_id': 'flv',
50             })
51         for video in clip_info.get('mp4', []):
52             formats.append({
53                 'url': video['src'],
54                 'format_id': 'mp4-%s' % video['bitrate'],
55                 'vbr': int_or_none(video['bitrate'].rstrip('k')),
56             })
57
58         if not formats:
59             smil = self._download_xml(
60                 'http://services.media.howstuffworks.com/videos/%s/smil-service.smil' % video_id,
61                 video_id, 'Downloading video SMIL')
62
63             http_base = find_xpath_attr(
64                 smil,
65                 './{0}head/{0}meta'.format('{http://www.w3.org/2001/SMIL20/Language}'),
66                 'name',
67                 'httpBase').get('content')
68
69             URL_SUFFIX = '?v=2.11.3&fp=LNX 11,2,202,356&r=A&g=A'
70
71             for video in smil.findall(
72                     './{0}body/{0}switch/{0}video'.format('{http://www.w3.org/2001/SMIL20/Language}')):
73                 vbr = int_or_none(video.attrib['system-bitrate'], scale=1000)
74                 formats.append({
75                     'url': '%s/%s%s' % (http_base, video.attrib['src'], URL_SUFFIX),
76                     'format_id': '%dk' % vbr,
77                     'vbr': vbr,
78                 })
79
80         self._sort_formats(formats)
81
82         return {
83             'id': '%s' % video_id,
84             'display_id': display_id,
85             'title': unescapeHTML(clip_info['clip_title']),
86             'description': unescapeHTML(clip_info.get('caption')),
87             'thumbnail': clip_info.get('video_still_url'),
88             'duration': int_or_none(clip_info.get('duration')),
89             'formats': formats,
90         }