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.

154 lines
5.8 KiB

10 years ago
10 years ago
10 years ago
  1. from __future__ import unicode_literals
  2. import functools
  3. import re
  4. from .turner import TurnerBaseIE
  5. from ..compat import (
  6. compat_urllib_parse_urlencode,
  7. compat_urlparse,
  8. )
  9. from ..utils import (
  10. OnDemandPagedList,
  11. remove_start,
  12. )
  13. class NBAIE(TurnerBaseIE):
  14. _VALID_URL = r'https?://(?:watch\.|www\.)?nba\.com/(?P<path>(?:[^/]+/)+(?P<id>[^?]*?))/?(?:/index\.html)?(?:\?.*)?$'
  15. _TESTS = [{
  16. 'url': 'http://www.nba.com/video/games/nets/2012/12/04/0021200253-okc-bkn-recap.nba/index.html',
  17. 'md5': '9e7729d3010a9c71506fd1248f74e4f4',
  18. 'info_dict': {
  19. 'id': '0021200253-okc-bkn-recap',
  20. 'ext': 'mp4',
  21. 'title': 'Thunder vs. Nets',
  22. 'description': 'Kevin Durant scores 32 points and dishes out six assists as the Thunder beat the Nets in Brooklyn.',
  23. 'duration': 181,
  24. 'timestamp': 1354638466,
  25. 'upload_date': '20121204',
  26. },
  27. 'params': {
  28. # m3u8 download
  29. 'skip_download': True,
  30. },
  31. }, {
  32. 'url': 'http://www.nba.com/video/games/hornets/2014/12/05/0021400276-nyk-cha-play5.nba/',
  33. 'only_matching': True,
  34. }, {
  35. 'url': 'http://watch.nba.com/video/channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
  36. 'md5': 'b2b39b81cf28615ae0c3360a3f9668c4',
  37. 'info_dict': {
  38. 'id': 'channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
  39. 'ext': 'mp4',
  40. 'title': 'Hawks vs. Cavaliers Game 1',
  41. 'description': 'md5:8094c3498d35a9bd6b1a8c396a071b4d',
  42. 'duration': 228,
  43. 'timestamp': 1432134543,
  44. 'upload_date': '20150520',
  45. },
  46. 'expected_warnings': ['Unable to download f4m manifest'],
  47. }, {
  48. 'url': 'http://www.nba.com/clippers/news/doc-rivers-were-not-trading-blake',
  49. 'info_dict': {
  50. 'id': 'teams/clippers/2016/02/17/1455672027478-Doc_Feb16_720.mov-297324',
  51. 'ext': 'mp4',
  52. 'title': 'Practice: Doc Rivers - 2/16/16',
  53. 'description': 'Head Coach Doc Rivers addresses the media following practice.',
  54. 'upload_date': '20160216',
  55. 'timestamp': 1455672000,
  56. },
  57. 'params': {
  58. # m3u8 download
  59. 'skip_download': True,
  60. },
  61. 'expected_warnings': ['Unable to download f4m manifest'],
  62. }, {
  63. 'url': 'http://www.nba.com/timberwolves/wiggins-shootaround#',
  64. 'info_dict': {
  65. 'id': 'timberwolves',
  66. 'title': 'Shootaround Access - Dec. 12 | Andrew Wiggins',
  67. },
  68. 'playlist_count': 30,
  69. 'params': {
  70. # Download the whole playlist takes too long time
  71. 'playlist_items': '1-30',
  72. },
  73. }, {
  74. 'url': 'http://www.nba.com/timberwolves/wiggins-shootaround#',
  75. 'info_dict': {
  76. 'id': 'teams/timberwolves/2014/12/12/Wigginsmp4-3462601',
  77. 'ext': 'mp4',
  78. 'title': 'Shootaround Access - Dec. 12 | Andrew Wiggins',
  79. 'description': 'Wolves rookie Andrew Wiggins addresses the media after Friday\'s shootaround.',
  80. 'upload_date': '20141212',
  81. 'timestamp': 1418418600,
  82. },
  83. 'params': {
  84. 'noplaylist': True,
  85. # m3u8 download
  86. 'skip_download': True,
  87. },
  88. 'expected_warnings': ['Unable to download f4m manifest'],
  89. }]
  90. _PAGE_SIZE = 30
  91. def _fetch_page(self, team, video_id, page):
  92. search_url = 'http://searchapp2.nba.com/nba-search/query.jsp?' + compat_urllib_parse_urlencode({
  93. 'type': 'teamvideo',
  94. 'start': page * self._PAGE_SIZE + 1,
  95. 'npp': (page + 1) * self._PAGE_SIZE + 1,
  96. 'sort': 'recent',
  97. 'output': 'json',
  98. 'site': team,
  99. })
  100. results = self._download_json(
  101. search_url, video_id, note='Download page %d of playlist data' % page)['results'][0]
  102. for item in results:
  103. yield self.url_result(compat_urlparse.urljoin('http://www.nba.com/', item['url']))
  104. def _extract_playlist(self, orig_path, video_id, webpage):
  105. team = orig_path.split('/')[0]
  106. if self._downloader.params.get('noplaylist'):
  107. self.to_screen('Downloading just video because of --no-playlist')
  108. video_path = self._search_regex(
  109. r'nbaVideoCore\.firstVideo\s*=\s*\'([^\']+)\';', webpage, 'video path')
  110. video_url = 'http://www.nba.com/%s/video/%s' % (team, video_path)
  111. return self.url_result(video_url)
  112. self.to_screen('Downloading playlist - add --no-playlist to just download video')
  113. playlist_title = self._og_search_title(webpage, fatal=False)
  114. entries = OnDemandPagedList(
  115. functools.partial(self._fetch_page, team, video_id),
  116. self._PAGE_SIZE, use_cache=True)
  117. return self.playlist_result(entries, team, playlist_title)
  118. def _real_extract(self, url):
  119. path, video_id = re.match(self._VALID_URL, url).groups()
  120. orig_path = path
  121. if path.startswith('nba/'):
  122. path = path[3:]
  123. if 'video/' not in path:
  124. webpage = self._download_webpage(url, video_id)
  125. path = remove_start(self._search_regex(r'data-videoid="([^"]+)"', webpage, 'video id'), '/')
  126. if path == '{{id}}':
  127. return self._extract_playlist(orig_path, video_id, webpage)
  128. # See prepareContentId() of pkgCvp.js
  129. if path.startswith('video/teams'):
  130. path = 'video/channels/proxy/' + path[6:]
  131. return self._extract_cvp_info(
  132. 'http://www.nba.com/%s.xml' % path, video_id, {
  133. 'default': {
  134. 'media_src': 'http://nba.cdn.turner.com/nba/big',
  135. },
  136. 'm3u8': {
  137. 'media_src': 'http://nbavod-f.akamaihd.net',
  138. },
  139. })