]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/yahoo.py
117f0856a261e8f520bd6ac593ca75f843f6c7a7
[youtubedl] / youtube_dl / extractor / yahoo.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import itertools
5 import json
6 import re
7
8 from .common import InfoExtractor, SearchInfoExtractor
9 from ..utils import (
10 ExtractorError,
11 compat_urllib_parse,
12 compat_urlparse,
13 clean_html,
14 int_or_none,
15 )
16
17
18 class YahooIE(InfoExtractor):
19 IE_DESC = 'Yahoo screen and movies'
20 _VALID_URL = r'(?P<url>(?P<host>https?://(?:[a-zA-Z]{2}\.)?[\da-zA-Z_-]+\.yahoo\.com)/(?:[^/]+/)*(?P<display_id>.+?)-(?P<id>[0-9]+)(?:-[a-z]+)?\.html)'
21 _TESTS = [
22 {
23 'url': 'http://screen.yahoo.com/julian-smith-travis-legg-watch-214727115.html',
24 'md5': '4962b075c08be8690a922ee026d05e69',
25 'info_dict': {
26 'id': '2d25e626-2378-391f-ada0-ddaf1417e588',
27 'ext': 'mp4',
28 'title': 'Julian Smith & Travis Legg Watch Julian Smith',
29 'description': 'Julian and Travis watch Julian Smith',
30 'duration': 6863,
31 },
32 },
33 {
34 'url': 'http://screen.yahoo.com/wired/codefellas-s1-ep12-cougar-lies-103000935.html',
35 'md5': 'd6e6fc6e1313c608f316ddad7b82b306',
36 'info_dict': {
37 'id': 'd1dedf8c-d58c-38c3-8963-e899929ae0a9',
38 'ext': 'mp4',
39 'title': 'Codefellas - The Cougar Lies with Spanish Moss',
40 'description': 'md5:66b627ab0a282b26352136ca96ce73c1',
41 'duration': 151,
42 },
43 },
44 {
45 'url': 'https://screen.yahoo.com/community/community-sizzle-reel-203225340.html?format=embed',
46 'md5': '60e8ac193d8fb71997caa8fce54c6460',
47 'info_dict': {
48 'id': '4fe78544-8d48-39d8-97cd-13f205d9fcdb',
49 'ext': 'mp4',
50 'title': "Yahoo Saves 'Community'",
51 'description': 'md5:4d4145af2fd3de00cbb6c1d664105053',
52 'duration': 170,
53 }
54 },
55 {
56 'url': 'https://tw.screen.yahoo.com/taipei-opinion-poll/選情站報-街頭民調-台北市篇-102823042.html',
57 'md5': '92a7fdd8a08783c68a174d7aa067dde8',
58 'info_dict': {
59 'id': '7a23b569-7bea-36cb-85b9-bd5301a0a1fb',
60 'ext': 'mp4',
61 'title': '選情站報 街頭民調 台北市篇',
62 'description': '選情站報 街頭民調 台北市篇',
63 'duration': 429,
64 }
65 },
66 {
67 'url': 'https://uk.screen.yahoo.com/editor-picks/cute-raccoon-freed-drain-using-091756545.html',
68 'md5': '0b51660361f0e27c9789e7037ef76f4b',
69 'info_dict': {
70 'id': 'b3affa53-2e14-3590-852b-0e0db6cd1a58',
71 'ext': 'mp4',
72 'title': 'Cute Raccoon Freed From Drain\u00a0Using Angle Grinder',
73 'description': 'md5:f66c890e1490f4910a9953c941dee944',
74 'duration': 97,
75 }
76 },
77 {
78 'url': 'https://ca.sports.yahoo.com/video/program-makes-hockey-more-affordable-013127711.html',
79 'md5': '57e06440778b1828a6079d2f744212c4',
80 'info_dict': {
81 'id': 'c9fa2a36-0d4d-3937-b8f6-cc0fb1881e73',
82 'ext': 'mp4',
83 'title': 'Program that makes hockey more affordable not offered in Manitoba',
84 'description': 'md5:c54a609f4c078d92b74ffb9bf1f496f4',
85 'duration': 121,
86 }
87 }, {
88 'url': 'https://ca.finance.yahoo.com/news/20-most-valuable-brands-world-112600775.html',
89 'md5': '3e401e4eed6325aa29d9b96125fd5b4f',
90 'info_dict': {
91 'id': 'c1b4c09c-8ed8-3b65-8b05-169c55358a83',
92 'ext': 'mp4',
93 'title': "Apple Is The World's Most Valuable Brand",
94 'description': 'md5:73eabc1a11c6f59752593b2ceefa1262',
95 'duration': 21,
96 }
97 }, {
98 'url': 'http://news.yahoo.com/video/china-moses-crazy-blues-104538833.html',
99 'md5': '67010fdf3a08d290e060a4dd96baa07b',
100 'info_dict': {
101 'id': 'f885cf7f-43d4-3450-9fac-46ac30ece521',
102 'ext': 'mp4',
103 'title': 'China Moses Is Crazy About the Blues',
104 'description': 'md5:9900ab8cd5808175c7b3fe55b979bed0',
105 'duration': 128,
106 }
107 }, {
108 'url': 'https://in.lifestyle.yahoo.com/video/connect-dots-dark-side-virgo-090247395.html',
109 'md5': 'd9a083ccf1379127bf25699d67e4791b',
110 'info_dict': {
111 'id': '52aeeaa3-b3d1-30d8-9ef8-5d0cf05efb7c',
112 'ext': 'mp4',
113 'title': 'Connect the Dots: Dark Side of Virgo',
114 'description': 'md5:1428185051cfd1949807ad4ff6d3686a',
115 'duration': 201,
116 }
117 }, {
118 'url': 'https://gma.yahoo.com/pizza-delivery-man-surprised-huge-tip-college-kids-195200785.html',
119 'only_matching': True,
120 }
121 ]
122
123 def _real_extract(self, url):
124 mobj = re.match(self._VALID_URL, url)
125 display_id = mobj.group('display_id')
126 url = mobj.group('url')
127 host = mobj.group('host')
128 webpage = self._download_webpage(url, display_id)
129
130 # Look for iframed media first
131 iframe_m = re.search(r'<iframe[^>]+src="(/video/.+?-\d+\.html\?format=embed.*?)"', webpage)
132 if iframe_m:
133 iframepage = self._download_webpage(
134 host + iframe_m.group(1), display_id, 'Downloading iframe webpage')
135 items_json = self._search_regex(
136 r'mediaItems: (\[.+?\])$', iframepage, 'items', flags=re.MULTILINE, default=None)
137 if items_json:
138 items = json.loads(items_json)
139 video_id = items[0]['id']
140 return self._get_info(video_id, display_id, webpage)
141
142 items_json = self._search_regex(
143 r'mediaItems: ({.*?})$', webpage, 'items', flags=re.MULTILINE,
144 default=None)
145 if items_json is None:
146 CONTENT_ID_REGEXES = [
147 r'YUI\.namespace\("Media"\)\.CONTENT_ID\s*=\s*"([^"]+)"',
148 r'root\.App\.Cache\.context\.videoCache\.curVideo = \{"([^"]+)"',
149 r'"first_videoid"\s*:\s*"([^"]+)"',
150 ]
151 video_id = self._search_regex(CONTENT_ID_REGEXES, webpage, 'content ID')
152 else:
153 items = json.loads(items_json)
154 info = items['mediaItems']['query']['results']['mediaObj'][0]
155 # The 'meta' field is not always in the video webpage, we request it
156 # from another page
157 video_id = info['id']
158 return self._get_info(video_id, display_id, webpage)
159
160 def _get_info(self, video_id, display_id, webpage):
161 region = self._search_regex(
162 r'\\?"region\\?"\s*:\s*\\?"([^"]+?)\\?"',
163 webpage, 'region', fatal=False, default='US')
164 query = ('SELECT * FROM yahoo.media.video.streams WHERE id="%s"'
165 ' AND plrs="86Gj0vCaSzV_Iuf6hNylf2" AND region="%s"'
166 ' AND protocol="http"' % (video_id, region))
167 data = compat_urllib_parse.urlencode({
168 'q': query,
169 'env': 'prod',
170 'format': 'json',
171 })
172 query_result = self._download_json(
173 'http://video.query.yahoo.com/v1/public/yql?' + data,
174 display_id, 'Downloading video info')
175
176 info = query_result['query']['results']['mediaObj'][0]
177 meta = info.get('meta')
178
179 if not meta:
180 msg = info['status'].get('msg')
181 if msg:
182 raise ExtractorError(
183 '%s returned error: %s' % (self.IE_NAME, msg), expected=True)
184 raise ExtractorError('Unable to extract media object meta')
185
186 formats = []
187 for s in info['streams']:
188 format_info = {
189 'width': int_or_none(s.get('width')),
190 'height': int_or_none(s.get('height')),
191 'tbr': int_or_none(s.get('bitrate')),
192 }
193
194 host = s['host']
195 path = s['path']
196 if host.startswith('rtmp'):
197 format_info.update({
198 'url': host,
199 'play_path': path,
200 'ext': 'flv',
201 })
202 else:
203 format_url = compat_urlparse.urljoin(host, path)
204 format_info['url'] = format_url
205 formats.append(format_info)
206
207 self._sort_formats(formats)
208
209 return {
210 'id': video_id,
211 'display_id': display_id,
212 'title': meta['title'],
213 'formats': formats,
214 'description': clean_html(meta['description']),
215 'thumbnail': meta['thumbnail'] if meta.get('thumbnail') else self._og_search_thumbnail(webpage),
216 'duration': int_or_none(meta.get('duration')),
217 }
218
219
220 class YahooSearchIE(SearchInfoExtractor):
221 IE_DESC = 'Yahoo screen search'
222 _MAX_RESULTS = 1000
223 IE_NAME = 'screen.yahoo:search'
224 _SEARCH_KEY = 'yvsearch'
225
226 def _get_n_results(self, query, n):
227 """Get a specified number of results for a query"""
228 entries = []
229 for pagenum in itertools.count(0):
230 result_url = 'http://video.search.yahoo.com/search/?p=%s&fr=screen&o=js&gs=0&b=%d' % (compat_urllib_parse.quote_plus(query), pagenum * 30)
231 info = self._download_json(result_url, query,
232 note='Downloading results page '+str(pagenum+1))
233 m = info['m']
234 results = info['results']
235
236 for (i, r) in enumerate(results):
237 if (pagenum * 30) + i >= n:
238 break
239 mobj = re.search(r'(?P<url>screen\.yahoo\.com/.*?-\d*?\.html)"', r)
240 e = self.url_result('http://' + mobj.group('url'), 'Yahoo')
241 entries.append(e)
242 if (pagenum * 30 + i >= n) or (m['last'] >= (m['total'] - 1)):
243 break
244
245 return {
246 '_type': 'playlist',
247 'id': query,
248 'entries': entries,
249 }