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.

161 lines
5.9 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. 'url': 'http://www.nfl.com/news/story/0ap3000000467586/article/patriots-seahawks-involved-in-lategame-skirmish',
  48. 'info_dict': {
  49. 'id': '0ap3000000467607',
  50. 'ext': 'mp4',
  51. 'title': 'Frustrations flare on the field',
  52. 'description': 'Emotions ran high at the end of the Super Bowl on both sides of the ball after a dramatic finish.',
  53. 'timestamp': 1422850320,
  54. 'upload_date': '20150202',
  55. },
  56. },
  57. ]
  58. @staticmethod
  59. def prepend_host(host, url):
  60. if not url.startswith('http'):
  61. if not url.startswith('/'):
  62. url = '/%s' % url
  63. url = 'http://{0:}{1:}'.format(host, url)
  64. return url
  65. @staticmethod
  66. def format_from_stream(stream, protocol, host, path_prefix='',
  67. preference=0, note=None):
  68. url = '{protocol:}://{host:}/{prefix:}{path:}'.format(
  69. protocol=protocol,
  70. host=host,
  71. prefix=path_prefix,
  72. path=stream.get('path'),
  73. )
  74. return {
  75. 'url': url,
  76. 'vbr': int_or_none(stream.get('rate', 0), 1000),
  77. 'preference': preference,
  78. 'format_note': note,
  79. }
  80. def _real_extract(self, url):
  81. mobj = re.match(self._VALID_URL, url)
  82. video_id, host = mobj.group('id'), mobj.group('host')
  83. webpage = self._download_webpage(url, video_id)
  84. config_url = NFLIE.prepend_host(host, self._search_regex(
  85. r'(?:config|configURL)\s*:\s*"([^"]+)"', webpage, 'config URL',
  86. default='static/content/static/config/video/config.json'))
  87. # For articles, the id in the url is not the video id
  88. video_id = self._search_regex(
  89. r'contentId\s*:\s*"([^"]+)"', webpage, 'video id', default=video_id)
  90. config = self._download_json(config_url, video_id,
  91. note='Downloading player config')
  92. url_template = NFLIE.prepend_host(
  93. host, '{contentURLTemplate:}'.format(**config))
  94. video_data = self._download_json(
  95. url_template.format(id=video_id), video_id)
  96. formats = []
  97. cdn_data = video_data.get('cdnData', {})
  98. streams = cdn_data.get('bitrateInfo', [])
  99. if cdn_data.get('format') == 'EXTERNAL_HTTP_STREAM':
  100. parts = compat_urllib_parse_urlparse(cdn_data.get('uri'))
  101. protocol, host = parts.scheme, parts.netloc
  102. for stream in streams:
  103. formats.append(
  104. NFLIE.format_from_stream(stream, protocol, host))
  105. else:
  106. cdns = config.get('cdns')
  107. if not cdns:
  108. raise ExtractorError('Failed to get CDN data', expected=True)
  109. for name, cdn in cdns.items():
  110. # LimeLight streams don't seem to work
  111. if cdn.get('name') == 'LIMELIGHT':
  112. continue
  113. protocol = cdn.get('protocol')
  114. host = remove_end(cdn.get('host', ''), '/')
  115. if not (protocol and host):
  116. continue
  117. prefix = cdn.get('pathprefix', '')
  118. if prefix and not prefix.endswith('/'):
  119. prefix = '%s/' % prefix
  120. preference = 0
  121. if protocol == 'rtmp':
  122. preference = -2
  123. elif 'prog' in name.lower():
  124. preference = 1
  125. for stream in streams:
  126. formats.append(
  127. NFLIE.format_from_stream(stream, protocol, host,
  128. prefix, preference, name))
  129. self._sort_formats(formats)
  130. thumbnail = None
  131. for q in ('xl', 'l', 'm', 's', 'xs'):
  132. thumbnail = video_data.get('imagePaths', {}).get(q)
  133. if thumbnail:
  134. break
  135. return {
  136. 'id': video_id,
  137. 'title': video_data.get('headline'),
  138. 'formats': formats,
  139. 'description': video_data.get('caption'),
  140. 'duration': video_data.get('duration'),
  141. 'thumbnail': thumbnail,
  142. 'timestamp': int_or_none(video_data.get('posted'), 1000),
  143. }