[yahoo] Make thumbnail optional (Closes #3043)
[youtube-dl] / youtube_dl / extractor / yahoo.py
1 from __future__ import unicode_literals
2
3 import itertools
4 import json
5 import re
6
7 from .common import InfoExtractor, SearchInfoExtractor
8 from ..utils import (
9     compat_urllib_parse,
10     compat_urlparse,
11     clean_html,
12     int_or_none,
13 )
14
15
16 class YahooIE(InfoExtractor):
17     IE_DESC = 'Yahoo screen and movies'
18     _VALID_URL = r'https?://(?:screen|movies)\.yahoo\.com/.*?-(?P<id>[0-9]+)(?:-[a-z]+)?\.html'
19     _TESTS = [
20         {
21             'url': 'http://screen.yahoo.com/julian-smith-travis-legg-watch-214727115.html',
22             'md5': '4962b075c08be8690a922ee026d05e69',
23             'info_dict': {
24                 'id': '214727115',
25                 'ext': 'mp4',
26                 'title': 'Julian Smith & Travis Legg Watch Julian Smith',
27                 'description': 'Julian and Travis watch Julian Smith',
28             },
29         },
30         {
31             'url': 'http://screen.yahoo.com/wired/codefellas-s1-ep12-cougar-lies-103000935.html',
32             'md5': 'd6e6fc6e1313c608f316ddad7b82b306',
33             'info_dict': {
34                 'id': '103000935',
35                 'ext': 'mp4',
36                 'title': 'Codefellas - The Cougar Lies with Spanish Moss',
37                 'description': 'Agent Topple\'s mustache does its dirty work, and Nicole brokers a deal for peace. But why is the NSA collecting millions of Instagram brunch photos? And if your waffles have nothing to hide, what are they so worried about?',
38             },
39         },
40         {
41             'url': 'https://movies.yahoo.com/video/world-loves-spider-man-190819223.html',
42             'md5': '410b7104aa9893b765bc22787a22f3d9',
43             'info_dict': {
44                 'id': '516ed8e2-2c4f-339f-a211-7a8b49d30845',
45                 'ext': 'mp4',
46                 'title': 'The World Loves Spider-Man',
47                 'description': '''People all over the world are celebrating the release of \"The Amazing Spider-Man 2.\" We're taking a look at the enthusiastic response Spider-Man has received from viewers all over the world.''',
48             }
49         }
50     ]
51
52     def _real_extract(self, url):
53         mobj = re.match(self._VALID_URL, url)
54         video_id = mobj.group('id')
55         webpage = self._download_webpage(url, video_id)
56
57         items_json = self._search_regex(
58             r'mediaItems: ({.*?})$', webpage, 'items', flags=re.MULTILINE,
59             default=None)
60         if items_json is None:
61             long_id = self._search_regex(
62                 r'YUI\.namespace\("Media"\)\.CONTENT_ID\s*=\s*"([^"]+)"',
63                 webpage, 'content ID')
64             video_id = long_id
65         else:
66             items = json.loads(items_json)
67             info = items['mediaItems']['query']['results']['mediaObj'][0]
68             # The 'meta' field is not always in the video webpage, we request it
69             # from another page
70             long_id = info['id']
71         return self._get_info(long_id, video_id)
72
73     def _get_info(self, long_id, video_id):
74         query = ('SELECT * FROM yahoo.media.video.streams WHERE id="%s"'
75                  ' AND plrs="86Gj0vCaSzV_Iuf6hNylf2" AND region="US"'
76                  ' AND protocol="http"' % long_id)
77         data = compat_urllib_parse.urlencode({
78             'q': query,
79             'env': 'prod',
80             'format': 'json',
81         })
82         query_result = self._download_json(
83             'http://video.query.yahoo.com/v1/public/yql?' + data,
84             video_id, 'Downloading video info')
85         info = query_result['query']['results']['mediaObj'][0]
86         meta = info['meta']
87
88         formats = []
89         for s in info['streams']:
90             format_info = {
91                 'width': int_or_none(s.get('width')),
92                 'height': int_or_none(s.get('height')),
93                 'tbr': int_or_none(s.get('bitrate')),
94             }
95
96             host = s['host']
97             path = s['path']
98             if host.startswith('rtmp'):
99                 format_info.update({
100                     'url': host,
101                     'play_path': path,
102                     'ext': 'flv',
103                 })
104             else:
105                 format_url = compat_urlparse.urljoin(host, path)
106                 format_info['url'] = format_url
107             formats.append(format_info)
108
109         self._sort_formats(formats)
110
111         return {
112             'id': video_id,
113             'title': meta['title'],
114             'formats': formats,
115             'description': clean_html(meta['description']),
116             'thumbnail': meta.get('thumbnail'),
117         }
118
119
120 class YahooNewsIE(YahooIE):
121     IE_NAME = 'yahoo:news'
122     _VALID_URL = r'http://news\.yahoo\.com/video/.*?-(?P<id>\d*?)\.html'
123
124     _TESTS = [{
125         'url': 'http://news.yahoo.com/video/china-moses-crazy-blues-104538833.html',
126         'md5': '67010fdf3a08d290e060a4dd96baa07b',
127         'info_dict': {
128             'id': '104538833',
129             'ext': 'mp4',
130             'title': 'China Moses Is Crazy About the Blues',
131             'description': 'md5:9900ab8cd5808175c7b3fe55b979bed0',
132         },
133     }]
134
135     def _real_extract(self, url):
136         mobj = re.match(self._VALID_URL, url)
137         video_id = mobj.group('id')
138         webpage = self._download_webpage(url, video_id)
139         long_id = self._search_regex(r'contentId: \'(.+?)\',', webpage, 'long id')
140         return self._get_info(long_id, video_id)
141
142
143 class YahooSearchIE(SearchInfoExtractor):
144     IE_DESC = 'Yahoo screen search'
145     _MAX_RESULTS = 1000
146     IE_NAME = 'screen.yahoo:search'
147     _SEARCH_KEY = 'yvsearch'
148
149     def _get_n_results(self, query, n):
150         """Get a specified number of results for a query"""
151         entries = []
152         for pagenum in itertools.count(0):
153             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)
154             info = self._download_json(result_url, query,
155                 note='Downloading results page '+str(pagenum+1))
156             m = info['m']
157             results = info['results']
158
159             for (i, r) in enumerate(results):
160                 if (pagenum * 30) + i >= n:
161                     break
162                 mobj = re.search(r'(?P<url>screen\.yahoo\.com/.*?-\d*?\.html)"', r)
163                 e = self.url_result('http://' + mobj.group('url'), 'Yahoo')
164                 entries.append(e)
165             if (pagenum * 30 + i >= n) or (m['last'] >= (m['total'] - 1)):
166                 break
167
168         return {
169             '_type': 'playlist',
170             'id': query,
171             'entries': entries,
172         }