]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/yahoo.py
Merge remote-tracking branch 'liudongmiao/patch-subtitle'
[yt-dlp.git] / 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'(?P<url>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': '2d25e626-2378-391f-ada0-ddaf1417e588',
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': 'd1dedf8c-d58c-38c3-8963-e899929ae0a9',
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 'url': 'https://screen.yahoo.com/community/community-sizzle-reel-203225340.html?format=embed',
52 'md5': '60e8ac193d8fb71997caa8fce54c6460',
53 'info_dict': {
54 'id': '4fe78544-8d48-39d8-97cd-13f205d9fcdb',
55 'ext': 'mp4',
56 'title': "Yahoo Saves 'Community'",
57 'description': 'md5:4d4145af2fd3de00cbb6c1d664105053',
58 }
59 },
60 ]
61
62 def _real_extract(self, url):
63 mobj = re.match(self._VALID_URL, url)
64 video_id = mobj.group('id')
65 url = mobj.group('url')
66 webpage = self._download_webpage(url, video_id)
67
68 items_json = self._search_regex(
69 r'mediaItems: ({.*?})$', webpage, 'items', flags=re.MULTILINE,
70 default=None)
71 if items_json is None:
72 CONTENT_ID_REGEXES = [
73 r'YUI\.namespace\("Media"\)\.CONTENT_ID\s*=\s*"([^"]+)"',
74 r'root\.App\.Cache\.context\.videoCache\.curVideo = \{"([^"]+)"'
75 ]
76 long_id = self._search_regex(CONTENT_ID_REGEXES, webpage, 'content ID')
77 video_id = long_id
78 else:
79 items = json.loads(items_json)
80 info = items['mediaItems']['query']['results']['mediaObj'][0]
81 # The 'meta' field is not always in the video webpage, we request it
82 # from another page
83 long_id = info['id']
84 return self._get_info(long_id, video_id, webpage)
85
86 def _get_info(self, long_id, video_id, webpage):
87 query = ('SELECT * FROM yahoo.media.video.streams WHERE id="%s"'
88 ' AND plrs="86Gj0vCaSzV_Iuf6hNylf2" AND region="US"'
89 ' AND protocol="http"' % long_id)
90 data = compat_urllib_parse.urlencode({
91 'q': query,
92 'env': 'prod',
93 'format': 'json',
94 })
95 query_result = self._download_json(
96 'http://video.query.yahoo.com/v1/public/yql?' + data,
97 video_id, 'Downloading video info')
98 info = query_result['query']['results']['mediaObj'][0]
99 meta = info['meta']
100
101 formats = []
102 for s in info['streams']:
103 format_info = {
104 'width': int_or_none(s.get('width')),
105 'height': int_or_none(s.get('height')),
106 'tbr': int_or_none(s.get('bitrate')),
107 }
108
109 host = s['host']
110 path = s['path']
111 if host.startswith('rtmp'):
112 format_info.update({
113 'url': host,
114 'play_path': path,
115 'ext': 'flv',
116 })
117 else:
118 format_url = compat_urlparse.urljoin(host, path)
119 format_info['url'] = format_url
120 formats.append(format_info)
121
122 self._sort_formats(formats)
123
124 return {
125 'id': video_id,
126 'title': meta['title'],
127 'formats': formats,
128 'description': clean_html(meta['description']),
129 'thumbnail': meta['thumbnail'] if meta.get('thumbnail') else self._og_search_thumbnail(webpage),
130 }
131
132
133 class YahooNewsIE(YahooIE):
134 IE_NAME = 'yahoo:news'
135 _VALID_URL = r'http://news\.yahoo\.com/video/.*?-(?P<id>\d*?)\.html'
136
137 _TESTS = [{
138 'url': 'http://news.yahoo.com/video/china-moses-crazy-blues-104538833.html',
139 'md5': '67010fdf3a08d290e060a4dd96baa07b',
140 'info_dict': {
141 'id': '104538833',
142 'ext': 'mp4',
143 'title': 'China Moses Is Crazy About the Blues',
144 'description': 'md5:9900ab8cd5808175c7b3fe55b979bed0',
145 },
146 }]
147
148 def _real_extract(self, url):
149 mobj = re.match(self._VALID_URL, url)
150 video_id = mobj.group('id')
151 webpage = self._download_webpage(url, video_id)
152 long_id = self._search_regex(r'contentId: \'(.+?)\',', webpage, 'long id')
153 return self._get_info(long_id, video_id, webpage)
154
155
156 class YahooSearchIE(SearchInfoExtractor):
157 IE_DESC = 'Yahoo screen search'
158 _MAX_RESULTS = 1000
159 IE_NAME = 'screen.yahoo:search'
160 _SEARCH_KEY = 'yvsearch'
161
162 def _get_n_results(self, query, n):
163 """Get a specified number of results for a query"""
164 entries = []
165 for pagenum in itertools.count(0):
166 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)
167 info = self._download_json(result_url, query,
168 note='Downloading results page '+str(pagenum+1))
169 m = info['m']
170 results = info['results']
171
172 for (i, r) in enumerate(results):
173 if (pagenum * 30) + i >= n:
174 break
175 mobj = re.search(r'(?P<url>screen\.yahoo\.com/.*?-\d*?\.html)"', r)
176 e = self.url_result('http://' + mobj.group('url'), 'Yahoo')
177 entries.append(e)
178 if (pagenum * 30 + i >= n) or (m['last'] >= (m['total'] - 1)):
179 break
180
181 return {
182 '_type': 'playlist',
183 'id': query,
184 'entries': entries,
185 }