2
0

nba.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from __future__ import unicode_literals
  2. import os.path
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. int_or_none,
  7. parse_duration,
  8. remove_start,
  9. xpath_text,
  10. xpath_attr,
  11. )
  12. class NBAIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:watch\.|www\.)?nba\.com/(?P<path>(?:[^/]+/)+(?P<id>[^?]*?))/?(?:/index\.html)?(?:\?.*)?$'
  14. _TESTS = [{
  15. 'url': 'http://www.nba.com/video/games/nets/2012/12/04/0021200253-okc-bkn-recap.nba/index.html',
  16. 'md5': '9e7729d3010a9c71506fd1248f74e4f4',
  17. 'info_dict': {
  18. 'id': '0021200253-okc-bkn-recap',
  19. 'ext': 'mp4',
  20. 'title': 'Thunder vs. Nets',
  21. 'description': 'Kevin Durant scores 32 points and dishes out six assists as the Thunder beat the Nets in Brooklyn.',
  22. 'duration': 181,
  23. 'timestamp': 1354638466,
  24. 'upload_date': '20121204',
  25. },
  26. 'params': {
  27. # m3u8 download
  28. 'skip_download': True,
  29. },
  30. }, {
  31. 'url': 'http://www.nba.com/video/games/hornets/2014/12/05/0021400276-nyk-cha-play5.nba/',
  32. 'only_matching': True,
  33. }, {
  34. 'url': 'http://watch.nba.com/video/channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
  35. 'md5': 'b2b39b81cf28615ae0c3360a3f9668c4',
  36. 'info_dict': {
  37. 'id': '0041400301-cle-atl-recap',
  38. 'ext': 'mp4',
  39. 'title': 'Hawks vs. Cavaliers Game 1',
  40. 'description': 'md5:8094c3498d35a9bd6b1a8c396a071b4d',
  41. 'duration': 228,
  42. 'timestamp': 1432134543,
  43. 'upload_date': '20150520',
  44. }
  45. }, {
  46. 'url': 'http://www.nba.com/clippers/news/doc-rivers-were-not-trading-blake',
  47. 'info_dict': {
  48. 'id': '1455672027478-Doc_Feb16_720',
  49. 'ext': 'mp4',
  50. 'title': 'Practice: Doc Rivers - 2/16/16',
  51. 'description': 'Head Coach Doc Rivers addresses the media following practice.',
  52. 'upload_date': '20160217',
  53. 'timestamp': 1455672000,
  54. },
  55. 'params': {
  56. # m3u8 download
  57. 'skip_download': True,
  58. },
  59. }]
  60. def _real_extract(self, url):
  61. path, video_id = re.match(self._VALID_URL, url).groups()
  62. if path.startswith('nba/'):
  63. path = path[3:]
  64. if 'video/' not in path:
  65. webpage = self._download_webpage(url, video_id)
  66. path = remove_start(self._search_regex(r'data-videoid="([^"]+)"', webpage, 'video id'), '/')
  67. # See prepareContentId() of pkgCvp.js
  68. if path.startswith('video/teams'):
  69. path = 'video/channels/proxy/' + path[6:]
  70. video_info = self._download_xml('http://www.nba.com/%s.xml' % path, video_id)
  71. video_id = os.path.splitext(xpath_text(video_info, 'slug'))[0]
  72. title = xpath_text(video_info, 'headline')
  73. description = xpath_text(video_info, 'description')
  74. duration = parse_duration(xpath_text(video_info, 'length'))
  75. timestamp = int_or_none(xpath_attr(video_info, 'dateCreated', 'uts'))
  76. thumbnails = []
  77. for image in video_info.find('images'):
  78. thumbnails.append({
  79. 'id': image.attrib.get('cut'),
  80. 'url': image.text,
  81. 'width': int_or_none(image.attrib.get('width')),
  82. 'height': int_or_none(image.attrib.get('height')),
  83. })
  84. formats = []
  85. for video_file in video_info.findall('.//file'):
  86. video_url = video_file.text
  87. if video_url.startswith('/'):
  88. continue
  89. if video_url.endswith('.m3u8'):
  90. formats.extend(self._extract_m3u8_formats(video_url, video_id, ext='mp4', m3u8_id='hls', fatal=False))
  91. elif video_url.endswith('.f4m'):
  92. formats.extend(self._extract_f4m_formats(video_url + '?hdcore=3.4.1.1', video_id, f4m_id='hds', fatal=False))
  93. else:
  94. key = video_file.attrib.get('bitrate')
  95. format_info = {
  96. 'format_id': key,
  97. 'url': video_url,
  98. }
  99. mobj = re.search(r'(\d+)x(\d+)(?:_(\d+))?', key)
  100. if mobj:
  101. format_info.update({
  102. 'width': int(mobj.group(1)),
  103. 'height': int(mobj.group(2)),
  104. 'tbr': int_or_none(mobj.group(3)),
  105. })
  106. formats.append(format_info)
  107. self._sort_formats(formats)
  108. return {
  109. 'id': video_id,
  110. 'title': title,
  111. 'description': description,
  112. 'duration': duration,
  113. 'timestamp': timestamp,
  114. 'thumbnails': thumbnails,
  115. 'formats': formats,
  116. }