yahoo.py 5.5 KB

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