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.

197 lines
7.4 KiB

10 years ago
10 years ago
10 years ago
  1. from __future__ import unicode_literals
  2. import functools
  3. import os.path
  4. import re
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_urllib_parse,
  8. compat_urlparse,
  9. )
  10. from ..utils import (
  11. int_or_none,
  12. OnDemandPagedList,
  13. parse_duration,
  14. remove_start,
  15. xpath_text,
  16. xpath_attr,
  17. )
  18. class NBAIE(InfoExtractor):
  19. _VALID_URL = r'https?://(?:watch\.|www\.)?nba\.com/(?P<path>(?:[^/]+/)+(?P<id>[^?]*?))/?(?:/index\.html)?(?:\?.*)?$'
  20. _TESTS = [{
  21. 'url': 'http://www.nba.com/video/games/nets/2012/12/04/0021200253-okc-bkn-recap.nba/index.html',
  22. 'md5': '9e7729d3010a9c71506fd1248f74e4f4',
  23. 'info_dict': {
  24. 'id': '0021200253-okc-bkn-recap',
  25. 'ext': 'mp4',
  26. 'title': 'Thunder vs. Nets',
  27. 'description': 'Kevin Durant scores 32 points and dishes out six assists as the Thunder beat the Nets in Brooklyn.',
  28. 'duration': 181,
  29. 'timestamp': 1354638466,
  30. 'upload_date': '20121204',
  31. },
  32. 'params': {
  33. # m3u8 download
  34. 'skip_download': True,
  35. },
  36. }, {
  37. 'url': 'http://www.nba.com/video/games/hornets/2014/12/05/0021400276-nyk-cha-play5.nba/',
  38. 'only_matching': True,
  39. }, {
  40. 'url': 'http://watch.nba.com/video/channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
  41. 'md5': 'b2b39b81cf28615ae0c3360a3f9668c4',
  42. 'info_dict': {
  43. 'id': '0041400301-cle-atl-recap',
  44. 'ext': 'mp4',
  45. 'title': 'Hawks vs. Cavaliers Game 1',
  46. 'description': 'md5:8094c3498d35a9bd6b1a8c396a071b4d',
  47. 'duration': 228,
  48. 'timestamp': 1432134543,
  49. 'upload_date': '20150520',
  50. }
  51. }, {
  52. 'url': 'http://www.nba.com/clippers/news/doc-rivers-were-not-trading-blake',
  53. 'info_dict': {
  54. 'id': '1455672027478-Doc_Feb16_720',
  55. 'ext': 'mp4',
  56. 'title': 'Practice: Doc Rivers - 2/16/16',
  57. 'description': 'Head Coach Doc Rivers addresses the media following practice.',
  58. 'upload_date': '20160217',
  59. 'timestamp': 1455672000,
  60. },
  61. 'params': {
  62. # m3u8 download
  63. 'skip_download': True,
  64. },
  65. }, {
  66. 'url': 'http://www.nba.com/timberwolves/wiggins-shootaround#',
  67. 'info_dict': {
  68. 'id': 'timberwolves',
  69. 'title': 'Shootaround Access - Dec. 12 | Andrew Wiggins',
  70. },
  71. 'playlist_count': 30,
  72. 'params': {
  73. # Download the whole playlist takes too long time
  74. 'playlist_items': '1-30',
  75. },
  76. }, {
  77. 'url': 'http://www.nba.com/timberwolves/wiggins-shootaround#',
  78. 'info_dict': {
  79. 'id': 'Wigginsmp4',
  80. 'ext': 'mp4',
  81. 'title': 'Shootaround Access - Dec. 12 | Andrew Wiggins',
  82. 'description': 'Wolves rookie Andrew Wiggins addresses the media after Friday\'s shootaround.',
  83. 'upload_date': '20141212',
  84. 'timestamp': 1418418600,
  85. },
  86. 'params': {
  87. 'noplaylist': True,
  88. # m3u8 download
  89. 'skip_download': True,
  90. },
  91. }]
  92. _PAGE_SIZE = 30
  93. def _fetch_page(self, team, video_id, page):
  94. search_url = 'http://searchapp2.nba.com/nba-search/query.jsp?' + compat_urllib_parse.urlencode({
  95. 'type': 'teamvideo',
  96. 'start': page * self._PAGE_SIZE + 1,
  97. 'npp': (page + 1) * self._PAGE_SIZE + 1,
  98. 'sort': 'recent',
  99. 'output': 'json',
  100. 'site': team,
  101. })
  102. results = self._download_json(
  103. search_url, video_id, note='Download page %d of playlist data' % page)['results'][0]
  104. for item in results:
  105. yield self.url_result(compat_urlparse.urljoin('http://www.nba.com/', item['url']))
  106. def _extract_playlist(self, orig_path, video_id, webpage):
  107. team = orig_path.split('/')[0]
  108. if self._downloader.params.get('noplaylist'):
  109. self.to_screen('Downloading just video because of --no-playlist')
  110. video_path = self._search_regex(
  111. r'nbaVideoCore\.firstVideo\s*=\s*\'([^\']+)\';', webpage, 'video path')
  112. video_url = 'http://www.nba.com/%s/video/%s' % (team, video_path)
  113. return self.url_result(video_url)
  114. self.to_screen('Downloading playlist - add --no-playlist to just download video')
  115. playlist_title = self._og_search_title(webpage, fatal=False)
  116. entries = OnDemandPagedList(
  117. functools.partial(self._fetch_page, team, video_id),
  118. self._PAGE_SIZE, use_cache=True)
  119. return self.playlist_result(entries, team, playlist_title)
  120. def _real_extract(self, url):
  121. path, video_id = re.match(self._VALID_URL, url).groups()
  122. orig_path = path
  123. if path.startswith('nba/'):
  124. path = path[3:]
  125. if 'video/' not in path:
  126. webpage = self._download_webpage(url, video_id)
  127. path = remove_start(self._search_regex(r'data-videoid="([^"]+)"', webpage, 'video id'), '/')
  128. if path == '{{id}}':
  129. return self._extract_playlist(orig_path, video_id, webpage)
  130. # See prepareContentId() of pkgCvp.js
  131. if path.startswith('video/teams'):
  132. path = 'video/channels/proxy/' + path[6:]
  133. video_info = self._download_xml('http://www.nba.com/%s.xml' % path, video_id)
  134. video_id = os.path.splitext(xpath_text(video_info, 'slug'))[0]
  135. title = xpath_text(video_info, 'headline')
  136. description = xpath_text(video_info, 'description')
  137. duration = parse_duration(xpath_text(video_info, 'length'))
  138. timestamp = int_or_none(xpath_attr(video_info, 'dateCreated', 'uts'))
  139. thumbnails = []
  140. for image in video_info.find('images'):
  141. thumbnails.append({
  142. 'id': image.attrib.get('cut'),
  143. 'url': image.text,
  144. 'width': int_or_none(image.attrib.get('width')),
  145. 'height': int_or_none(image.attrib.get('height')),
  146. })
  147. formats = []
  148. for video_file in video_info.findall('.//file'):
  149. video_url = video_file.text
  150. if video_url.startswith('/'):
  151. continue
  152. if video_url.endswith('.m3u8'):
  153. formats.extend(self._extract_m3u8_formats(video_url, video_id, ext='mp4', m3u8_id='hls', fatal=False))
  154. elif video_url.endswith('.f4m'):
  155. formats.extend(self._extract_f4m_formats(video_url + '?hdcore=3.4.1.1', video_id, f4m_id='hds', fatal=False))
  156. else:
  157. key = video_file.attrib.get('bitrate')
  158. format_info = {
  159. 'format_id': key,
  160. 'url': video_url,
  161. }
  162. mobj = re.search(r'(\d+)x(\d+)(?:_(\d+))?', key)
  163. if mobj:
  164. format_info.update({
  165. 'width': int(mobj.group(1)),
  166. 'height': int(mobj.group(2)),
  167. 'tbr': int_or_none(mobj.group(3)),
  168. })
  169. formats.append(format_info)
  170. self._sort_formats(formats)
  171. return {
  172. 'id': video_id,
  173. 'title': title,
  174. 'description': description,
  175. 'duration': duration,
  176. 'timestamp': timestamp,
  177. 'thumbnails': thumbnails,
  178. 'formats': formats,
  179. }