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.

146 lines
5.1 KiB

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