nba.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. parse_duration,
  6. int_or_none,
  7. )
  8. class NBAIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:watch\.|www\.)?nba\.com/(?P<path>(?:[^/]+/)?video/(?P<id>[^?]*?))/?(?:/index\.html)?(?:\?.*)?$'
  10. _TESTS = [{
  11. 'url': 'http://www.nba.com/video/games/nets/2012/12/04/0021200253-okc-bkn-recap.nba/index.html',
  12. 'md5': '9e7729d3010a9c71506fd1248f74e4f4',
  13. 'info_dict': {
  14. 'id': '0021200253-okc-bkn-recap',
  15. 'ext': 'flv',
  16. 'title': 'Thunder vs. Nets',
  17. 'description': 'Kevin Durant scores 32 points and dishes out six assists as the Thunder beat the Nets in Brooklyn.',
  18. 'duration': 181,
  19. 'timestamp': 1354638466,
  20. 'upload_date': '20121204',
  21. },
  22. }, {
  23. 'url': 'http://www.nba.com/video/games/hornets/2014/12/05/0021400276-nyk-cha-play5.nba/',
  24. 'only_matching': True,
  25. },{
  26. 'url': 'http://watch.nba.com/video/channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
  27. 'md5': 'b2b39b81cf28615ae0c3360a3f9668c4',
  28. 'info_dict': {
  29. 'id': '0041400301-cle-atl-recap',
  30. 'ext': 'mp4',
  31. 'title': 'Hawks vs. Cavaliers Game 1',
  32. 'description': 'md5:8094c3498d35a9bd6b1a8c396a071b4d',
  33. 'duration': 228,
  34. 'timestamp': 1432134543,
  35. 'upload_date': '20150520',
  36. }
  37. }]
  38. def _real_extract(self, url):
  39. path, video_id = re.match(self._VALID_URL, url).groups()
  40. video_info = self._download_xml('http://www.nba.com/%s.xml' % path, video_id)
  41. video_id = video_info.find('slug').text
  42. title = video_info.find('headline').text
  43. description = video_info.find('description').text
  44. duration = parse_duration(video_info.find('length').text)
  45. timestamp = int_or_none(video_info.find('dateCreated').attrib.get('uts'))
  46. thumbnails = []
  47. for image in video_info.find('images'):
  48. thumbnails.append({
  49. 'id': image.attrib.get('cut'),
  50. 'url': image.text,
  51. 'width': int_or_none(image.attrib.get('width')),
  52. 'height': int_or_none(image.attrib.get('height')),
  53. })
  54. formats = []
  55. for video_file in video_info.find('files').iter('file'):
  56. video_url = video_file.text
  57. if video_url.startswith('/'):
  58. continue
  59. if video_url.endswith('.m3u8'):
  60. formats.extend(self._extract_m3u8_formats(video_url, video_id, m3u8_id='hls'))
  61. elif video_url.endswith('.f4m'):
  62. formats.extend(self._extract_f4m_formats(video_url + '?hdcore=3.4.1.1', video_id, f4m_id='hds'))
  63. else:
  64. key = video_file.attrib.get('bitrate')
  65. width, height, bitrate = re.search(r'(\d+)x(\d+)(?:_(\d+))?', key).groups()
  66. formats.append({
  67. 'format_id': key,
  68. 'url': video_url,
  69. 'width': int_or_none(width),
  70. 'height': int_or_none(height),
  71. 'tbr': int_or_none(bitrate),
  72. })
  73. self._sort_formats(formats)
  74. return {
  75. 'id': video_id,
  76. 'title': title,
  77. 'description': description,
  78. 'duration': duration,
  79. 'timestamp': timestamp,
  80. 'thumbnails': thumbnails,
  81. 'formats': formats,
  82. }