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.

119 lines
4.1 KiB

  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.mp4',
  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. },
  23. {
  24. u'url': u'http://screen.yahoo.com/wired/codefellas-s1-ep12-cougar-lies-103000935.html',
  25. u'file': u'103000935.flv',
  26. u'info_dict': {
  27. u'title': u'The Cougar Lies with Spanish Moss',
  28. 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?',
  29. },
  30. u'params': {
  31. # Requires rtmpdump
  32. u'skip_download': True,
  33. },
  34. },
  35. ]
  36. def _real_extract(self, url):
  37. mobj = re.match(self._VALID_URL, url)
  38. video_id = mobj.group('id')
  39. webpage = self._download_webpage(url, video_id)
  40. items_json = self._search_regex(r'YVIDEO_INIT_ITEMS = ({.*?});$',
  41. webpage, u'items', flags=re.MULTILINE)
  42. items = json.loads(items_json)
  43. info = items['mediaItems']['query']['results']['mediaObj'][0]
  44. meta = info['meta']
  45. formats = []
  46. for s in info['streams']:
  47. format_info = {
  48. 'width': s.get('width'),
  49. 'height': s.get('height'),
  50. 'bitrate': s.get('bitrate'),
  51. }
  52. host = s['host']
  53. path = s['path']
  54. if host.startswith('rtmp'):
  55. format_info.update({
  56. 'url': host,
  57. 'play_path': path,
  58. 'ext': 'flv',
  59. })
  60. else:
  61. format_url = compat_urlparse.urljoin(host, path)
  62. format_info['url'] = format_url
  63. format_info['ext'] = determine_ext(format_url)
  64. formats.append(format_info)
  65. formats = sorted(formats, key=lambda f:(f['height'], f['width']))
  66. info = {
  67. 'id': video_id,
  68. 'title': meta['title'],
  69. 'formats': formats,
  70. 'description': clean_html(meta['description']),
  71. 'thumbnail': meta['thumbnail'],
  72. }
  73. # TODO: Remove when #980 has been merged
  74. info.update(formats[-1])
  75. return info
  76. class YahooSearchIE(SearchInfoExtractor):
  77. IE_DESC = u'Yahoo screen search'
  78. _MAX_RESULTS = 1000
  79. IE_NAME = u'screen.yahoo:search'
  80. _SEARCH_KEY = 'yvsearch'
  81. def _get_n_results(self, query, n):
  82. """Get a specified number of results for a query"""
  83. res = {
  84. '_type': 'playlist',
  85. 'id': query,
  86. 'entries': []
  87. }
  88. for pagenum in itertools.count(0):
  89. 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)
  90. webpage = self._download_webpage(result_url, query,
  91. note='Downloading results page '+str(pagenum+1))
  92. info = json.loads(webpage)
  93. m = info[u'm']
  94. results = info[u'results']
  95. for (i, r) in enumerate(results):
  96. if (pagenum * 30) +i >= n:
  97. break
  98. mobj = re.search(r'(?P<url>screen\.yahoo\.com/.*?-\d*?\.html)"', r)
  99. e = self.url_result('http://' + mobj.group('url'), 'Yahoo')
  100. res['entries'].append(e)
  101. if (pagenum * 30 +i >= n) or (m[u'last'] >= (m[u'total'] -1 )):
  102. break
  103. return res