You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
5.6 KiB

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