]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/yahoo.py
[walla] Fix typo
[yt-dlp.git] / youtube_dl / extractor / yahoo.py
CommitLineData
2317e6b2
AS
1from __future__ import unicode_literals
2
934858ad 3import itertools
15251481 4import json
d6039175
PH
5import re
6
934858ad 7from .common import InfoExtractor, SearchInfoExtractor
d6039175 8from ..utils import (
934858ad 9 compat_urllib_parse,
9c15e9de 10 compat_urlparse,
9c15e9de 11 clean_html,
7217e148 12 int_or_none,
d6039175
PH
13)
14
9c15e9de 15
d6039175 16class YahooIE(InfoExtractor):
a4eb9578 17 IE_DESC = 'Yahoo screen and movies'
49807b4a 18 _VALID_URL = r'(?P<url>https?://(?:screen|movies)\.yahoo\.com/.*?-(?P<id>[0-9]+)(?:-[a-z]+)?\.html)'
9c15e9de
JMF
19 _TESTS = [
20 {
2317e6b2 21 'url': 'http://screen.yahoo.com/julian-smith-travis-legg-watch-214727115.html',
2317e6b2
AS
22 'md5': '4962b075c08be8690a922ee026d05e69',
23 'info_dict': {
fb6a5b96 24 'id': '2d25e626-2378-391f-ada0-ddaf1417e588',
88f1c6de 25 'ext': 'mp4',
2317e6b2
AS
26 'title': 'Julian Smith & Travis Legg Watch Julian Smith',
27 'description': 'Julian and Travis watch Julian Smith',
9c15e9de
JMF
28 },
29 },
30 {
2317e6b2 31 'url': 'http://screen.yahoo.com/wired/codefellas-s1-ep12-cougar-lies-103000935.html',
2317e6b2
AS
32 'md5': 'd6e6fc6e1313c608f316ddad7b82b306',
33 'info_dict': {
fb6a5b96 34 'id': 'd1dedf8c-d58c-38c3-8963-e899929ae0a9',
88f1c6de 35 'ext': 'mp4',
2317e6b2
AS
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?',
9c15e9de 38 },
6f5ac90c 39 },
49807b4a
S
40 {
41 'url': 'https://screen.yahoo.com/community/community-sizzle-reel-203225340.html?format=embed',
42 'md5': '60e8ac193d8fb71997caa8fce54c6460',
43 'info_dict': {
44 'id': '4fe78544-8d48-39d8-97cd-13f205d9fcdb',
45 'ext': 'mp4',
46 'title': "Yahoo Saves 'Community'",
47 'description': 'md5:4d4145af2fd3de00cbb6c1d664105053',
48 }
49 },
9c15e9de 50 ]
d6039175
PH
51
52 def _real_extract(self, url):
53 mobj = re.match(self._VALID_URL, url)
d6039175 54 video_id = mobj.group('id')
49807b4a 55 url = mobj.group('url')
d6039175 56 webpage = self._download_webpage(url, video_id)
d6039175 57
a4eb9578
PH
58 items_json = self._search_regex(
59 r'mediaItems: ({.*?})$', webpage, 'items', flags=re.MULTILINE,
60 default=None)
61 if items_json is None:
fb6a5b96 62 CONTENT_ID_REGEXES = [
a4eb9578 63 r'YUI\.namespace\("Media"\)\.CONTENT_ID\s*=\s*"([^"]+)"',
071a236c
S
64 r'root\.App\.Cache\.context\.videoCache\.curVideo = \{"([^"]+)"',
65 r'"first_videoid"\s*:\s*"([^"]+)"',
fb6a5b96
S
66 ]
67 long_id = self._search_regex(CONTENT_ID_REGEXES, webpage, 'content ID')
a4eb9578
PH
68 video_id = long_id
69 else:
70 items = json.loads(items_json)
71 info = items['mediaItems']['query']['results']['mediaObj'][0]
72 # The 'meta' field is not always in the video webpage, we request it
73 # from another page
74 long_id = info['id']
dede691a 75 return self._get_info(long_id, video_id, webpage)
befd88b7 76
dede691a 77 def _get_info(self, long_id, video_id, webpage):
cb354c8f 78 query = ('SELECT * FROM yahoo.media.video.streams WHERE id="%s"'
c5171c45
JMF
79 ' AND plrs="86Gj0vCaSzV_Iuf6hNylf2" AND region="US"'
80 ' AND protocol="http"' % long_id)
cb354c8f
JMF
81 data = compat_urllib_parse.urlencode({
82 'q': query,
83 'env': 'prod',
84 'format': 'json',
85 })
88f1c6de 86 query_result = self._download_json(
cb354c8f 87 'http://video.query.yahoo.com/v1/public/yql?' + data,
2317e6b2 88 video_id, 'Downloading video info')
cb354c8f 89 info = query_result['query']['results']['mediaObj'][0]
9c15e9de
JMF
90 meta = info['meta']
91
92 formats = []
93 for s in info['streams']:
94 format_info = {
7217e148
PH
95 'width': int_or_none(s.get('width')),
96 'height': int_or_none(s.get('height')),
97 'tbr': int_or_none(s.get('bitrate')),
9c15e9de
JMF
98 }
99
100 host = s['host']
101 path = s['path']
102 if host.startswith('rtmp'):
103 format_info.update({
104 'url': host,
105 'play_path': path,
106 'ext': 'flv',
107 })
108 else:
109 format_url = compat_urlparse.urljoin(host, path)
110 format_info['url'] = format_url
9c15e9de 111 formats.append(format_info)
7217e148
PH
112
113 self._sort_formats(formats)
9c15e9de 114
35907e23 115 return {
9c15e9de
JMF
116 'id': video_id,
117 'title': meta['title'],
118 'formats': formats,
119 'description': clean_html(meta['description']),
dede691a 120 'thumbnail': meta['thumbnail'] if meta.get('thumbnail') else self._og_search_thumbnail(webpage),
9c15e9de 121 }
d6039175 122
934858ad 123
befd88b7
JMF
124class YahooNewsIE(YahooIE):
125 IE_NAME = 'yahoo:news'
126 _VALID_URL = r'http://news\.yahoo\.com/video/.*?-(?P<id>\d*?)\.html'
127
52fadd5f 128 _TESTS = [{
2317e6b2
AS
129 'url': 'http://news.yahoo.com/video/china-moses-crazy-blues-104538833.html',
130 'md5': '67010fdf3a08d290e060a4dd96baa07b',
131 'info_dict': {
132 'id': '104538833',
133 'ext': 'mp4',
134 'title': 'China Moses Is Crazy About the Blues',
135 'description': 'md5:9900ab8cd5808175c7b3fe55b979bed0',
befd88b7 136 },
52fadd5f 137 }]
befd88b7
JMF
138
139 def _real_extract(self, url):
140 mobj = re.match(self._VALID_URL, url)
141 video_id = mobj.group('id')
142 webpage = self._download_webpage(url, video_id)
2317e6b2 143 long_id = self._search_regex(r'contentId: \'(.+?)\',', webpage, 'long id')
dede691a 144 return self._get_info(long_id, video_id, webpage)
befd88b7
JMF
145
146
934858ad 147class YahooSearchIE(SearchInfoExtractor):
2317e6b2 148 IE_DESC = 'Yahoo screen search'
934858ad 149 _MAX_RESULTS = 1000
2317e6b2 150 IE_NAME = 'screen.yahoo:search'
934858ad
PH
151 _SEARCH_KEY = 'yvsearch'
152
153 def _get_n_results(self, query, n):
154 """Get a specified number of results for a query"""
88f1c6de
JMF
155 entries = []
156 for pagenum in itertools.count(0):
2317e6b2 157 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)
88f1c6de
JMF
158 info = self._download_json(result_url, query,
159 note='Downloading results page '+str(pagenum+1))
2317e6b2
AS
160 m = info['m']
161 results = info['results']
934858ad
PH
162
163 for (i, r) in enumerate(results):
88f1c6de 164 if (pagenum * 30) + i >= n:
934858ad
PH
165 break
166 mobj = re.search(r'(?P<url>screen\.yahoo\.com/.*?-\d*?\.html)"', r)
167 e = self.url_result('http://' + mobj.group('url'), 'Yahoo')
88f1c6de
JMF
168 entries.append(e)
169 if (pagenum * 30 + i >= n) or (m['last'] >= (m['total'] - 1)):
934858ad
PH
170 break
171
88f1c6de
JMF
172 return {
173 '_type': 'playlist',
174 'id': query,
175 'entries': entries,
176 }