yahoo.py 5.8 KB

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