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.

205 lines
7.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)
  16. https?://
  17. (?P<host>
  18. (?:www\.)?
  19. (?:
  20. (?:
  21. nfl|
  22. buffalobills|
  23. miamidolphins|
  24. patriots|
  25. newyorkjets|
  26. baltimoreravens|
  27. bengals|
  28. clevelandbrowns|
  29. steelers|
  30. houstontexans|
  31. colts|
  32. jaguars|
  33. titansonline|
  34. denverbroncos|
  35. kcchiefs|
  36. raiders|
  37. chargers|
  38. dallascowboys|
  39. giants|
  40. philadelphiaeagles|
  41. redskins|
  42. chicagobears|
  43. detroitlions|
  44. packers|
  45. vikings|
  46. atlantafalcons|
  47. panthers|
  48. neworleanssaints|
  49. buccaneers|
  50. azcardinals|
  51. stlouisrams|
  52. 49ers|
  53. seahawks
  54. )\.com|
  55. .+?\.clubs\.nfl\.com
  56. )
  57. )/
  58. (?:.+?/)*
  59. (?P<id>(?:[a-z0-9]{16}|\w{8}\-(?:\w{4}\-){3}\w{12}))
  60. '''
  61. _TESTS = [{
  62. 'url': 'http://www.nfl.com/videos/nfl-game-highlights/0ap3000000398478/Week-3-Redskins-vs-Eagles-highlights',
  63. 'md5': '394ef771ddcd1354f665b471d78ec4c6',
  64. 'info_dict': {
  65. 'id': '0ap3000000398478',
  66. 'ext': 'mp4',
  67. 'title': 'Week 3: Redskins vs. Eagles highlights',
  68. 'description': 'md5:56323bfb0ac4ee5ab24bd05fdf3bf478',
  69. 'upload_date': '20140921',
  70. 'timestamp': 1411337580,
  71. 'thumbnail': 're:^https?://.*\.jpg$',
  72. }
  73. }, {
  74. 'url': 'http://prod.www.steelers.clubs.nfl.com/video-and-audio/videos/LIVE_Post_Game_vs_Browns/9d72f26a-9e2b-4718-84d3-09fb4046c266',
  75. 'md5': 'cf85bdb4bc49f6e9d3816d130c78279c',
  76. 'info_dict': {
  77. 'id': '9d72f26a-9e2b-4718-84d3-09fb4046c266',
  78. 'ext': 'mp4',
  79. 'title': 'LIVE: Post Game vs. Browns',
  80. 'description': 'md5:6a97f7e5ebeb4c0e69a418a89e0636e8',
  81. 'upload_date': '20131229',
  82. 'timestamp': 1388354455,
  83. 'thumbnail': 're:^https?://.*\.jpg$',
  84. }
  85. }, {
  86. 'url': 'http://www.nfl.com/news/story/0ap3000000467586/article/patriots-seahawks-involved-in-lategame-skirmish',
  87. 'info_dict': {
  88. 'id': '0ap3000000467607',
  89. 'ext': 'mp4',
  90. 'title': 'Frustrations flare on the field',
  91. 'description': 'Emotions ran high at the end of the Super Bowl on both sides of the ball after a dramatic finish.',
  92. 'timestamp': 1422850320,
  93. 'upload_date': '20150202',
  94. },
  95. }, {
  96. 'url': 'http://www.nfl.com/videos/nfl-network-top-ten/09000d5d810a6bd4/Top-10-Gutsiest-Performances-Jack-Youngblood',
  97. 'only_matching': True,
  98. }, {
  99. 'url': 'http://www.buffalobills.com/video/videos/Rex_Ryan_Show_World_Wide_Rex/b1dcfab2-3190-4bb1-bfc0-d6e603d6601a',
  100. 'only_matching': True,
  101. }]
  102. @staticmethod
  103. def prepend_host(host, url):
  104. if not url.startswith('http'):
  105. if not url.startswith('/'):
  106. url = '/%s' % url
  107. url = 'http://{0:}{1:}'.format(host, url)
  108. return url
  109. @staticmethod
  110. def format_from_stream(stream, protocol, host, path_prefix='',
  111. preference=0, note=None):
  112. url = '{protocol:}://{host:}/{prefix:}{path:}'.format(
  113. protocol=protocol,
  114. host=host,
  115. prefix=path_prefix,
  116. path=stream.get('path'),
  117. )
  118. return {
  119. 'url': url,
  120. 'vbr': int_or_none(stream.get('rate', 0), 1000),
  121. 'preference': preference,
  122. 'format_note': note,
  123. }
  124. def _real_extract(self, url):
  125. mobj = re.match(self._VALID_URL, url)
  126. video_id, host = mobj.group('id'), mobj.group('host')
  127. webpage = self._download_webpage(url, video_id)
  128. config_url = NFLIE.prepend_host(host, self._search_regex(
  129. r'(?:config|configURL)\s*:\s*"([^"]+)"', webpage, 'config URL',
  130. default='static/content/static/config/video/config.json'))
  131. # For articles, the id in the url is not the video id
  132. video_id = self._search_regex(
  133. r'contentId\s*:\s*"([^"]+)"', webpage, 'video id', default=video_id)
  134. config = self._download_json(config_url, video_id,
  135. note='Downloading player config')
  136. url_template = NFLIE.prepend_host(
  137. host, '{contentURLTemplate:}'.format(**config))
  138. video_data = self._download_json(
  139. url_template.format(id=video_id), video_id)
  140. formats = []
  141. cdn_data = video_data.get('cdnData', {})
  142. streams = cdn_data.get('bitrateInfo', [])
  143. if cdn_data.get('format') == 'EXTERNAL_HTTP_STREAM':
  144. parts = compat_urllib_parse_urlparse(cdn_data.get('uri'))
  145. protocol, host = parts.scheme, parts.netloc
  146. for stream in streams:
  147. formats.append(
  148. NFLIE.format_from_stream(stream, protocol, host))
  149. else:
  150. cdns = config.get('cdns')
  151. if not cdns:
  152. raise ExtractorError('Failed to get CDN data', expected=True)
  153. for name, cdn in cdns.items():
  154. # LimeLight streams don't seem to work
  155. if cdn.get('name') == 'LIMELIGHT':
  156. continue
  157. protocol = cdn.get('protocol')
  158. host = remove_end(cdn.get('host', ''), '/')
  159. if not (protocol and host):
  160. continue
  161. prefix = cdn.get('pathprefix', '')
  162. if prefix and not prefix.endswith('/'):
  163. prefix = '%s/' % prefix
  164. preference = 0
  165. if protocol == 'rtmp':
  166. preference = -2
  167. elif 'prog' in name.lower():
  168. preference = 1
  169. for stream in streams:
  170. formats.append(
  171. NFLIE.format_from_stream(stream, protocol, host,
  172. prefix, preference, name))
  173. self._sort_formats(formats)
  174. thumbnail = None
  175. for q in ('xl', 'l', 'm', 's', 'xs'):
  176. thumbnail = video_data.get('imagePaths', {}).get(q)
  177. if thumbnail:
  178. break
  179. return {
  180. 'id': video_id,
  181. 'title': video_data.get('headline'),
  182. 'formats': formats,
  183. 'description': video_data.get('caption'),
  184. 'duration': video_data.get('duration'),
  185. 'thumbnail': thumbnail,
  186. 'timestamp': int_or_none(video_data.get('posted'), 1000),
  187. }