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.

144 lines
5.1 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. compat_urllib_parse_urlparse,
  8. int_or_none,
  9. remove_end,
  10. )
  11. class NFLIE(InfoExtractor):
  12. IE_NAME = 'nfl.com'
  13. _VALID_URL = r'''(?x)https?://
  14. (?P<host>(?:www\.)?(?:nfl\.com|.*?\.clubs\.nfl\.com))/
  15. (?:.+?/)*
  16. (?P<id>(?:\d[a-z]{2}\d{13}|\w{8}\-(?:\w{4}\-){3}\w{12}))'''
  17. _TESTS = [
  18. {
  19. 'url': 'http://www.nfl.com/videos/nfl-game-highlights/0ap3000000398478/Week-3-Redskins-vs-Eagles-highlights',
  20. 'md5': '394ef771ddcd1354f665b471d78ec4c6',
  21. 'info_dict': {
  22. 'id': '0ap3000000398478',
  23. 'ext': 'mp4',
  24. 'title': 'Week 3: Redskins vs. Eagles highlights',
  25. 'description': 'md5:56323bfb0ac4ee5ab24bd05fdf3bf478',
  26. 'upload_date': '20140921',
  27. 'timestamp': 1411337580,
  28. 'thumbnail': 're:^https?://.*\.jpg$',
  29. }
  30. },
  31. {
  32. 'url': 'http://prod.www.steelers.clubs.nfl.com/video-and-audio/videos/LIVE_Post_Game_vs_Browns/9d72f26a-9e2b-4718-84d3-09fb4046c266',
  33. 'md5': 'cf85bdb4bc49f6e9d3816d130c78279c',
  34. 'info_dict': {
  35. 'id': '9d72f26a-9e2b-4718-84d3-09fb4046c266',
  36. 'ext': 'mp4',
  37. 'title': 'LIVE: Post Game vs. Browns',
  38. 'description': 'md5:6a97f7e5ebeb4c0e69a418a89e0636e8',
  39. 'upload_date': '20131229',
  40. 'timestamp': 1388354455,
  41. 'thumbnail': 're:^https?://.*\.jpg$',
  42. }
  43. }
  44. ]
  45. @staticmethod
  46. def prepend_host(host, url):
  47. if not url.startswith('http'):
  48. if not url.startswith('/'):
  49. url = '/%s' % url
  50. url = 'http://{0:}{1:}'.format(host, url)
  51. return url
  52. @staticmethod
  53. def format_from_stream(stream, protocol, host, path_prefix='',
  54. preference=0, note=None):
  55. url = '{protocol:}://{host:}/{prefix:}{path:}'.format(
  56. protocol=protocol,
  57. host=host,
  58. prefix=path_prefix,
  59. path=stream.get('path'),
  60. )
  61. return {
  62. 'url': url,
  63. 'vbr': int_or_none(stream.get('rate', 0), 1000),
  64. 'preference': preference,
  65. 'format_note': note,
  66. }
  67. def _real_extract(self, url):
  68. mobj = re.match(self._VALID_URL, url)
  69. video_id, host = mobj.group('id'), mobj.group('host')
  70. webpage = self._download_webpage(url, video_id)
  71. config_url = NFLIE.prepend_host(host, self._search_regex(
  72. r'(?:config|configURL)\s*:\s*"([^"]+)"', webpage, 'config URL'))
  73. config = self._download_json(config_url, video_id,
  74. note='Downloading player config')
  75. url_template = NFLIE.prepend_host(
  76. host, '{contentURLTemplate:}'.format(**config))
  77. video_data = self._download_json(
  78. url_template.format(id=video_id), video_id)
  79. formats = []
  80. cdn_data = video_data.get('cdnData', {})
  81. streams = cdn_data.get('bitrateInfo', [])
  82. if cdn_data.get('format') == 'EXTERNAL_HTTP_STREAM':
  83. parts = compat_urllib_parse_urlparse(cdn_data.get('uri'))
  84. protocol, host = parts.scheme, parts.netloc
  85. for stream in streams:
  86. formats.append(
  87. NFLIE.format_from_stream(stream, protocol, host))
  88. else:
  89. cdns = config.get('cdns')
  90. if not cdns:
  91. raise ExtractorError('Failed to get CDN data', expected=True)
  92. for name, cdn in cdns.items():
  93. # LimeLight streams don't seem to work
  94. if cdn.get('name') == 'LIMELIGHT':
  95. continue
  96. protocol = cdn.get('protocol')
  97. host = remove_end(cdn.get('host', ''), '/')
  98. if not (protocol and host):
  99. continue
  100. prefix = cdn.get('pathprefix', '')
  101. if prefix and not prefix.endswith('/'):
  102. prefix = '%s/' % prefix
  103. preference = 0
  104. if protocol == 'rtmp':
  105. preference = -2
  106. elif 'prog' in name.lower():
  107. preference = 1
  108. for stream in streams:
  109. formats.append(
  110. NFLIE.format_from_stream(stream, protocol, host,
  111. prefix, preference, name))
  112. self._sort_formats(formats)
  113. thumbnail = None
  114. for q in ('xl', 'l', 'm', 's', 'xs'):
  115. thumbnail = video_data.get('imagePaths', {}).get(q)
  116. if thumbnail:
  117. break
  118. return {
  119. 'id': video_id,
  120. 'title': video_data.get('headline'),
  121. 'formats': formats,
  122. 'description': video_data.get('caption'),
  123. 'duration': video_data.get('duration'),
  124. 'thumbnail': thumbnail,
  125. 'timestamp': int_or_none(video_data.get('posted'), 1000),
  126. }