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