]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/yahoo.py
debian/changelog: Initiate new release.
[youtubedl] / 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'
18 _VALID_URL = r'http://screen\.yahoo\.com/.*?-(?P<id>\d*?)\.html'
19 _TESTS = [
20 {
21 'url': 'http://screen.yahoo.com/julian-smith-travis-legg-watch-214727115.html',
22 'file': '214727115.mp4',
23 'md5': '4962b075c08be8690a922ee026d05e69',
24 'info_dict': {
25 'title': 'Julian Smith & Travis Legg Watch Julian Smith',
26 'description': 'Julian and Travis watch Julian Smith',
27 },
28 },
29 {
30 'url': 'http://screen.yahoo.com/wired/codefellas-s1-ep12-cougar-lies-103000935.html',
31 'file': '103000935.mp4',
32 'md5': 'd6e6fc6e1313c608f316ddad7b82b306',
33 'info_dict': {
34 'title': 'Codefellas - The Cougar Lies with Spanish Moss',
35 '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?',
36 },
37 },
38 ]
39
40 def _real_extract(self, url):
41 mobj = re.match(self._VALID_URL, url)
42 video_id = mobj.group('id')
43 webpage = self._download_webpage(url, video_id)
44
45 items_json = self._search_regex(r'mediaItems: ({.*?})$',
46 webpage, 'items', flags=re.MULTILINE)
47 items = json.loads(items_json)
48 info = items['mediaItems']['query']['results']['mediaObj'][0]
49 # The 'meta' field is not always in the video webpage, we request it
50 # from another page
51 long_id = info['id']
52 return self._get_info(long_id, video_id)
53
54 def _get_info(self, long_id, video_id):
55 query = ('SELECT * FROM yahoo.media.video.streams WHERE id="%s"'
56 ' AND plrs="86Gj0vCaSzV_Iuf6hNylf2" AND region="US"'
57 ' AND protocol="http"' % long_id)
58 data = compat_urllib_parse.urlencode({
59 'q': query,
60 'env': 'prod',
61 'format': 'json',
62 })
63 query_result_json = self._download_webpage(
64 'http://video.query.yahoo.com/v1/public/yql?' + data,
65 video_id, 'Downloading video info')
66 query_result = json.loads(query_result_json)
67 info = query_result['query']['results']['mediaObj'][0]
68 meta = info['meta']
69
70 formats = []
71 for s in info['streams']:
72 format_info = {
73 'width': int_or_none(s.get('width')),
74 'height': int_or_none(s.get('height')),
75 'tbr': int_or_none(s.get('bitrate')),
76 }
77
78 host = s['host']
79 path = s['path']
80 if host.startswith('rtmp'):
81 format_info.update({
82 'url': host,
83 'play_path': path,
84 'ext': 'flv',
85 })
86 else:
87 format_url = compat_urlparse.urljoin(host, path)
88 format_info['url'] = format_url
89
90 formats.append(format_info)
91
92 self._sort_formats(formats)
93
94 return {
95 'id': video_id,
96 'title': meta['title'],
97 'formats': formats,
98 'description': clean_html(meta['description']),
99 'thumbnail': meta['thumbnail'],
100 }
101
102
103 class YahooNewsIE(YahooIE):
104 IE_NAME = 'yahoo:news'
105 _VALID_URL = r'http://news\.yahoo\.com/video/.*?-(?P<id>\d*?)\.html'
106
107 _TEST = {
108 'url': 'http://news.yahoo.com/video/china-moses-crazy-blues-104538833.html',
109 'md5': '67010fdf3a08d290e060a4dd96baa07b',
110 'info_dict': {
111 'id': '104538833',
112 'ext': 'mp4',
113 'title': 'China Moses Is Crazy About the Blues',
114 'description': 'md5:9900ab8cd5808175c7b3fe55b979bed0',
115 },
116 }
117
118 # Overwrite YahooIE properties we don't want
119 _TESTS = []
120
121 def _real_extract(self, url):
122 mobj = re.match(self._VALID_URL, url)
123 video_id = mobj.group('id')
124 webpage = self._download_webpage(url, video_id)
125 long_id = self._search_regex(r'contentId: \'(.+?)\',', webpage, 'long id')
126 return self._get_info(long_id, video_id)
127
128
129 class YahooSearchIE(SearchInfoExtractor):
130 IE_DESC = 'Yahoo screen search'
131 _MAX_RESULTS = 1000
132 IE_NAME = 'screen.yahoo:search'
133 _SEARCH_KEY = 'yvsearch'
134
135 def _get_n_results(self, query, n):
136 """Get a specified number of results for a query"""
137
138 res = {
139 '_type': 'playlist',
140 'id': query,
141 'entries': []
142 }
143 for pagenum in itertools.count(0):
144 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)
145 webpage = self._download_webpage(result_url, query,
146 note='Downloading results page '+str(pagenum+1))
147 info = json.loads(webpage)
148 m = info['m']
149 results = info['results']
150
151 for (i, r) in enumerate(results):
152 if (pagenum * 30) +i >= n:
153 break
154 mobj = re.search(r'(?P<url>screen\.yahoo\.com/.*?-\d*?\.html)"', r)
155 e = self.url_result('http://' + mobj.group('url'), 'Yahoo')
156 res['entries'].append(e)
157 if (pagenum * 30 +i >= n) or (m['last'] >= (m['total'] -1)):
158 break
159
160 return res