yahoo.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. from __future__ import unicode_literals
  2. import itertools
  3. import json
  4. import re
  5. from .common import InfoExtractor, SearchInfoExtractor
  6. from ..utils import (
  7. compat_urllib_parse,
  8. compat_urlparse,
  9. clean_html,
  10. int_or_none,
  11. )
  12. class YahooIE(InfoExtractor):
  13. IE_DESC = 'Yahoo screen and movies'
  14. _VALID_URL = r'(?P<url>https?://(?:screen|movies)\.yahoo\.com/.*?-(?P<id>[0-9]+)(?:-[a-z]+)?\.html)'
  15. _TESTS = [
  16. {
  17. 'url': 'http://screen.yahoo.com/julian-smith-travis-legg-watch-214727115.html',
  18. 'md5': '4962b075c08be8690a922ee026d05e69',
  19. 'info_dict': {
  20. 'id': '2d25e626-2378-391f-ada0-ddaf1417e588',
  21. 'ext': 'mp4',
  22. 'title': 'Julian Smith & Travis Legg Watch Julian Smith',
  23. 'description': 'Julian and Travis watch Julian Smith',
  24. },
  25. },
  26. {
  27. 'url': 'http://screen.yahoo.com/wired/codefellas-s1-ep12-cougar-lies-103000935.html',
  28. 'md5': 'd6e6fc6e1313c608f316ddad7b82b306',
  29. 'info_dict': {
  30. 'id': 'd1dedf8c-d58c-38c3-8963-e899929ae0a9',
  31. 'ext': 'mp4',
  32. 'title': 'Codefellas - The Cougar Lies with Spanish Moss',
  33. '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?',
  34. },
  35. },
  36. {
  37. 'url': 'https://movies.yahoo.com/video/world-loves-spider-man-190819223.html',
  38. 'md5': '410b7104aa9893b765bc22787a22f3d9',
  39. 'info_dict': {
  40. 'id': '516ed8e2-2c4f-339f-a211-7a8b49d30845',
  41. 'ext': 'mp4',
  42. 'title': 'The World Loves Spider-Man',
  43. '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.''',
  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. }
  55. },
  56. ]
  57. def _real_extract(self, url):
  58. mobj = re.match(self._VALID_URL, url)
  59. video_id = mobj.group('id')
  60. url = mobj.group('url')
  61. webpage = self._download_webpage(url, video_id)
  62. items_json = self._search_regex(
  63. r'mediaItems: ({.*?})$', webpage, 'items', flags=re.MULTILINE,
  64. default=None)
  65. if items_json is None:
  66. CONTENT_ID_REGEXES = [
  67. r'YUI\.namespace\("Media"\)\.CONTENT_ID\s*=\s*"([^"]+)"',
  68. r'root\.App\.Cache\.context\.videoCache\.curVideo = \{"([^"]+)"',
  69. r'"first_videoid"\s*:\s*"([^"]+)"',
  70. ]
  71. long_id = self._search_regex(CONTENT_ID_REGEXES, webpage, 'content ID')
  72. video_id = long_id
  73. else:
  74. items = json.loads(items_json)
  75. info = items['mediaItems']['query']['results']['mediaObj'][0]
  76. # The 'meta' field is not always in the video webpage, we request it
  77. # from another page
  78. long_id = info['id']
  79. return self._get_info(long_id, video_id, webpage)
  80. def _get_info(self, long_id, video_id, webpage):
  81. query = ('SELECT * FROM yahoo.media.video.streams WHERE id="%s"'
  82. ' AND plrs="86Gj0vCaSzV_Iuf6hNylf2" AND region="US"'
  83. ' AND protocol="http"' % long_id)
  84. data = compat_urllib_parse.urlencode({
  85. 'q': query,
  86. 'env': 'prod',
  87. 'format': 'json',
  88. })
  89. query_result = self._download_json(
  90. 'http://video.query.yahoo.com/v1/public/yql?' + data,
  91. video_id, 'Downloading video info')
  92. info = query_result['query']['results']['mediaObj'][0]
  93. meta = info['meta']
  94. formats = []
  95. for s in info['streams']:
  96. format_info = {
  97. 'width': int_or_none(s.get('width')),
  98. 'height': int_or_none(s.get('height')),
  99. 'tbr': int_or_none(s.get('bitrate')),
  100. }
  101. host = s['host']
  102. path = s['path']
  103. if host.startswith('rtmp'):
  104. format_info.update({
  105. 'url': host,
  106. 'play_path': path,
  107. 'ext': 'flv',
  108. })
  109. else:
  110. format_url = compat_urlparse.urljoin(host, path)
  111. format_info['url'] = format_url
  112. formats.append(format_info)
  113. self._sort_formats(formats)
  114. return {
  115. 'id': video_id,
  116. 'title': meta['title'],
  117. 'formats': formats,
  118. 'description': clean_html(meta['description']),
  119. 'thumbnail': meta['thumbnail'] if meta.get('thumbnail') else self._og_search_thumbnail(webpage),
  120. }
  121. class YahooNewsIE(YahooIE):
  122. IE_NAME = 'yahoo:news'
  123. _VALID_URL = r'http://news\.yahoo\.com/video/.*?-(?P<id>\d*?)\.html'
  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. def _real_extract(self, url):
  135. mobj = re.match(self._VALID_URL, url)
  136. video_id = mobj.group('id')
  137. webpage = self._download_webpage(url, video_id)
  138. long_id = self._search_regex(r'contentId: \'(.+?)\',', webpage, 'long id')
  139. return self._get_info(long_id, video_id, webpage)
  140. class YahooSearchIE(SearchInfoExtractor):
  141. IE_DESC = 'Yahoo screen search'
  142. _MAX_RESULTS = 1000
  143. IE_NAME = 'screen.yahoo:search'
  144. _SEARCH_KEY = 'yvsearch'
  145. def _get_n_results(self, query, n):
  146. """Get a specified number of results for a query"""
  147. entries = []
  148. for pagenum in itertools.count(0):
  149. 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)
  150. info = self._download_json(result_url, query,
  151. note='Downloading results page '+str(pagenum+1))
  152. m = info['m']
  153. results = info['results']
  154. for (i, r) in enumerate(results):
  155. if (pagenum * 30) + i >= n:
  156. break
  157. mobj = re.search(r'(?P<url>screen\.yahoo\.com/.*?-\d*?\.html)"', r)
  158. e = self.url_result('http://' + mobj.group('url'), 'Yahoo')
  159. entries.append(e)
  160. if (pagenum * 30 + i >= n) or (m['last'] >= (m['total'] - 1)):
  161. break
  162. return {
  163. '_type': 'playlist',
  164. 'id': query,
  165. 'entries': entries,
  166. }