]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/howstuffworks.py
Merge remote-tracking branch 'liudongmiao/patch-subtitle'
[youtube-dl] / youtube_dl / extractor / howstuffworks.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5 import random
6 import string
7
8 from .common import InfoExtractor
9 from ..utils import find_xpath_attr
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                 'display_id': 'cool-jobs-iditarod-musher',
20                 'ext': 'flv',
21                 'title': 'Cool Jobs - Iditarod Musher',
22                 'description': 'md5:82bb58438a88027b8186a1fccb365f90',
23                 'thumbnail': 're:^https?://.*\.jpg$',
24             },
25             'params': {
26                 # md5 is not consistent
27                 'skip_download': True
28             }
29         },
30         {
31             'url': 'http://adventure.howstuffworks.com/39516-deadliest-catch-jakes-farewell-pots-video.htm',
32             'info_dict': {
33                 'id': '553470',
34                 'display_id': 'deadliest-catch-jakes-farewell-pots',
35                 'ext': 'mp4',
36                 'title': 'Deadliest Catch: Jake\'s Farewell Pots',
37                 'description': 'md5:9632c346d5e43ee238028c9cefd8dbbc',
38                 'thumbnail': 're:^https?://.*\.jpg$',
39             },
40             'params': {
41                 # md5 is not consistent
42                 'skip_download': True
43             }
44         },
45         {
46             'url': 'http://entertainment.howstuffworks.com/arts/2706-sword-swallowing-1-by-dan-meyer-video.htm',
47             'info_dict': {
48                 'id': '440011',
49                 'display_id': 'sword-swallowing-1-by-dan-meyer',
50                 'ext': 'flv',
51                 'title': 'Sword Swallowing #1 by Dan Meyer',
52                 'description': 'md5:b2409e88172913e2e7d3d1159b0ef735',
53                 'thumbnail': 're:^https?://.*\.jpg$',
54             },
55             'params': {
56                 # md5 is not consistent
57                 'skip_download': True
58             }
59         },
60     ]
61
62     def _real_extract(self, url):
63         mobj = re.match(self._VALID_URL, url)
64         display_id = mobj.group('id')
65         webpage = self._download_webpage(url, display_id)
66
67         content_id = self._search_regex(r'var siteSectionId="(\d+)";', webpage, 'content id')
68
69         mp4 = self._search_regex(
70             r'''(?xs)var\s+clip\s*=\s*{\s*
71                 .+?\s*
72                 content_id\s*:\s*%s\s*,\s*
73                 .+?\s*
74                 mp4\s*:\s*\[(.*?),?\]\s*
75                 };\s*
76                 videoData\.push\(clip\);''' % content_id,
77             webpage, 'mp4', fatal=False, default=None)
78
79         smil = self._download_xml(
80             'http://services.media.howstuffworks.com/videos/%s/smil-service.smil' % content_id,
81             content_id, 'Downloading video SMIL')
82
83         http_base = find_xpath_attr(
84             smil,
85             './{0}head/{0}meta'.format('{http://www.w3.org/2001/SMIL20/Language}'),
86             'name',
87             'httpBase').get('content')
88
89         def random_string(str_len=0):
90             return ''.join([random.choice(string.ascii_uppercase) for _ in range(str_len)])
91
92         URL_SUFFIX = '?v=2.11.3&fp=LNX 11,2,202,356&r=%s&g=%s' % (random_string(5), random_string(12))
93
94         formats = []
95
96         if mp4:
97             for video in json.loads('[%s]' % mp4):
98                 bitrate = video['bitrate']
99                 fmt = {
100                     'url': video['src'].replace('http://pmd.video.howstuffworks.com', http_base) + URL_SUFFIX,
101                     'format_id': bitrate,
102                 }
103                 m = re.search(r'(?P<vbr>\d+)[Kk]', bitrate)
104                 if m:
105                     fmt['vbr'] = int(m.group('vbr'))
106                 formats.append(fmt)
107         else:
108             for video in smil.findall(
109                     './/{0}body/{0}switch/{0}video'.format('{http://www.w3.org/2001/SMIL20/Language}')):
110                 vbr = int(video.attrib['system-bitrate']) / 1000
111                 formats.append({
112                     'url': '%s/%s%s' % (http_base, video.attrib['src'], URL_SUFFIX),
113                     'format_id': '%dk' % vbr,
114                     'vbr': vbr,
115                 })
116
117         self._sort_formats(formats)
118
119         title = self._og_search_title(webpage)
120         TITLE_SUFFIX = ' : HowStuffWorks'
121         if title.endswith(TITLE_SUFFIX):
122             title = title[:-len(TITLE_SUFFIX)]
123
124         description = self._og_search_description(webpage)
125         thumbnail = self._og_search_thumbnail(webpage)
126
127         return {
128             'id': content_id,
129             'display_id': display_id,
130             'title': title,
131             'description': description,
132             'thumbnail': thumbnail,
133             'formats': formats,
134         }