[yahoo] Remove md5sum from test case
[youtube-dl] / 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     unescapeHTML,
16     ExtractorError,
17     int_or_none,
18 )
19
20
21 class YahooIE(InfoExtractor):
22     IE_DESC = 'Yahoo screen and movies'
23     _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)'
24     _TESTS = [
25         {
26             'url': 'http://screen.yahoo.com/julian-smith-travis-legg-watch-214727115.html',
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/election-2014-askmayor/敢問市長-黃秀霜批賴清德-非常高傲-033009720.html',
59             'md5': '3a09cf59349cfaddae1797acc3c087fc',
60             'info_dict': {
61                 'id': 'cac903b3-fcf4-3c14-b632-643ab541712f',
62                 'ext': 'mp4',
63                 'title': '敢問市長/黃秀霜批賴清德「非常高傲」',
64                 'description': '直言台南沒捷運 交通居五都之末',
65                 'duration': 396,
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/hackers-sony-more-trouble-well-154609075.html',
91             'md5': '226a895aae7e21b0129e2a2006fe9690',
92             'info_dict': {
93                 'id': 'e624c4bc-3389-34de-9dfc-025f74943409',
94                 'ext': 'mp4',
95                 'title': '\'The Interview\' TV Spot: War',
96                 'description': 'The Interview',
97                 'duration': 30,
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://www.yahoo.com/movies/v/true-story-trailer-173000497.html',
121             'md5': '989396ae73d20c6f057746fb226aa215',
122             'info_dict': {
123                 'id': '071c4013-ce30-3a93-a5b2-e0413cd4a9d1',
124                 'ext': 'mp4',
125                 'title': '\'True Story\' Trailer',
126                 'description': 'True Story',
127                 'duration': 150,
128             },
129         }, {
130             'url': 'https://gma.yahoo.com/pizza-delivery-man-surprised-huge-tip-college-kids-195200785.html',
131             'only_matching': True,
132         }
133     ]
134
135     def _real_extract(self, url):
136         mobj = re.match(self._VALID_URL, url)
137         display_id = mobj.group('display_id')
138         page_id = mobj.group('id')
139         url = mobj.group('url')
140         host = mobj.group('host')
141         webpage = self._download_webpage(url, display_id)
142
143         # Look for iframed media first
144         iframe_m = re.search(r'<iframe[^>]+src="(/video/.+?-\d+\.html\?format=embed.*?)"', webpage)
145         if iframe_m:
146             iframepage = self._download_webpage(
147                 host + iframe_m.group(1), display_id, 'Downloading iframe webpage')
148             items_json = self._search_regex(
149                 r'mediaItems: (\[.+?\])$', iframepage, 'items', flags=re.MULTILINE, default=None)
150             if items_json:
151                 items = json.loads(items_json)
152                 video_id = items[0]['id']
153                 return self._get_info(video_id, display_id, webpage)
154
155         items_json = self._search_regex(
156             r'mediaItems: ({.*?})$', webpage, 'items', flags=re.MULTILINE,
157             default=None)
158         if items_json is None:
159             CONTENT_ID_REGEXES = [
160                 r'YUI\.namespace\("Media"\)\.CONTENT_ID\s*=\s*"([^"]+)"',
161                 r'root\.App\.Cache\.context\.videoCache\.curVideo = \{"([^"]+)"',
162                 r'"first_videoid"\s*:\s*"([^"]+)"',
163                 r'%s[^}]*"ccm_id"\s*:\s*"([^"]+)"' % re.escape(page_id),
164             ]
165             video_id = self._search_regex(CONTENT_ID_REGEXES, webpage, 'content ID')
166         else:
167             items = json.loads(items_json)
168             info = items['mediaItems']['query']['results']['mediaObj'][0]
169             # The 'meta' field is not always in the video webpage, we request it
170             # from another page
171             video_id = info['id']
172         return self._get_info(video_id, display_id, webpage)
173
174     def _get_info(self, video_id, display_id, webpage):
175         region = self._search_regex(
176             r'\\?"region\\?"\s*:\s*\\?"([^"]+?)\\?"',
177             webpage, 'region', fatal=False, default='US')
178         data = compat_urllib_parse.urlencode({
179             'protocol': 'http',
180             'region': region,
181         })
182         query_url = (
183             'https://video.media.yql.yahoo.com/v1/video/sapi/streams/'
184             '{id}?{data}'.format(id=video_id, data=data))
185         query_result = self._download_json(
186             query_url, display_id, 'Downloading video info')
187
188         info = query_result['query']['results']['mediaObj'][0]
189         meta = info.get('meta')
190
191         if not meta:
192             msg = info['status'].get('msg')
193             if msg:
194                 raise ExtractorError(
195                     '%s returned error: %s' % (self.IE_NAME, msg), expected=True)
196             raise ExtractorError('Unable to extract media object meta')
197
198         formats = []
199         for s in info['streams']:
200             format_info = {
201                 'width': int_or_none(s.get('width')),
202                 'height': int_or_none(s.get('height')),
203                 'tbr': int_or_none(s.get('bitrate')),
204             }
205
206             host = s['host']
207             path = s['path']
208             if host.startswith('rtmp'):
209                 format_info.update({
210                     'url': host,
211                     'play_path': path,
212                     'ext': 'flv',
213                 })
214             else:
215                 format_url = compat_urlparse.urljoin(host, path)
216                 format_info['url'] = format_url
217             formats.append(format_info)
218
219         self._sort_formats(formats)
220
221         return {
222             'id': video_id,
223             'display_id': display_id,
224             'title': unescapeHTML(meta['title']),
225             'formats': formats,
226             'description': clean_html(meta['description']),
227             'thumbnail': meta['thumbnail'] if meta.get('thumbnail') else self._og_search_thumbnail(webpage),
228             'duration': int_or_none(meta.get('duration')),
229         }
230
231
232 class YahooSearchIE(SearchInfoExtractor):
233     IE_DESC = 'Yahoo screen search'
234     _MAX_RESULTS = 1000
235     IE_NAME = 'screen.yahoo:search'
236     _SEARCH_KEY = 'yvsearch'
237
238     def _get_n_results(self, query, n):
239         """Get a specified number of results for a query"""
240         entries = []
241         for pagenum in itertools.count(0):
242             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)
243             info = self._download_json(result_url, query,
244                                        note='Downloading results page ' + str(pagenum + 1))
245             m = info['m']
246             results = info['results']
247
248             for (i, r) in enumerate(results):
249                 if (pagenum * 30) + i >= n:
250                     break
251                 mobj = re.search(r'(?P<url>screen\.yahoo\.com/.*?-\d*?\.html)"', r)
252                 e = self.url_result('http://' + mobj.group('url'), 'Yahoo')
253                 entries.append(e)
254             if (pagenum * 30 + i >= n) or (m['last'] >= (m['total'] - 1)):
255                 break
256
257         return {
258             '_type': 'playlist',
259             'id': query,
260             'entries': entries,
261         }