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.

217 lines
8.4 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>[^/#?&]+)
  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.patriots.com/video/2015/09/18/10-days-gillette',
  97. 'md5': '4c319e2f625ffd0b481b4382c6fc124c',
  98. 'info_dict': {
  99. 'id': 'n-238346',
  100. 'ext': 'mp4',
  101. 'title': '10 Days at Gillette',
  102. 'description': 'md5:8cd9cd48fac16de596eadc0b24add951',
  103. 'timestamp': 1442618809,
  104. 'upload_date': '20150918',
  105. },
  106. }, {
  107. 'url': 'http://www.nfl.com/videos/nfl-network-top-ten/09000d5d810a6bd4/Top-10-Gutsiest-Performances-Jack-Youngblood',
  108. 'only_matching': True,
  109. }, {
  110. 'url': 'http://www.buffalobills.com/video/videos/Rex_Ryan_Show_World_Wide_Rex/b1dcfab2-3190-4bb1-bfc0-d6e603d6601a',
  111. 'only_matching': True,
  112. }]
  113. @staticmethod
  114. def prepend_host(host, url):
  115. if not url.startswith('http'):
  116. if not url.startswith('/'):
  117. url = '/%s' % url
  118. url = 'http://{0:}{1:}'.format(host, url)
  119. return url
  120. @staticmethod
  121. def format_from_stream(stream, protocol, host, path_prefix='',
  122. preference=0, note=None):
  123. url = '{protocol:}://{host:}/{prefix:}{path:}'.format(
  124. protocol=protocol,
  125. host=host,
  126. prefix=path_prefix,
  127. path=stream.get('path'),
  128. )
  129. return {
  130. 'url': url,
  131. 'vbr': int_or_none(stream.get('rate', 0), 1000),
  132. 'preference': preference,
  133. 'format_note': note,
  134. }
  135. def _real_extract(self, url):
  136. mobj = re.match(self._VALID_URL, url)
  137. video_id, host = mobj.group('id'), mobj.group('host')
  138. webpage = self._download_webpage(url, video_id)
  139. config_url = NFLIE.prepend_host(host, self._search_regex(
  140. r'(?:(?:config|configURL)\s*:\s*|<nflcs:avplayer[^>]+data-config\s*=\s*)(["\'])(?P<config>.+?)\1',
  141. webpage, 'config URL', default='static/content/static/config/video/config.json',
  142. group='config'))
  143. # For articles, the id in the url is not the video id
  144. video_id = self._search_regex(
  145. r'(?:<nflcs:avplayer[^>]+data-contentId\s*=\s*|contentId\s*:\s*)(["\'])(?P<id>.+?)\1',
  146. webpage, 'video id', default=video_id, group='id')
  147. config = self._download_json(config_url, video_id, 'Downloading player config')
  148. url_template = NFLIE.prepend_host(
  149. host, '{contentURLTemplate:}'.format(**config))
  150. video_data = self._download_json(
  151. url_template.format(id=video_id), video_id)
  152. formats = []
  153. cdn_data = video_data.get('cdnData', {})
  154. streams = cdn_data.get('bitrateInfo', [])
  155. if cdn_data.get('format') == 'EXTERNAL_HTTP_STREAM':
  156. parts = compat_urllib_parse_urlparse(cdn_data.get('uri'))
  157. protocol, host = parts.scheme, parts.netloc
  158. for stream in streams:
  159. formats.append(
  160. NFLIE.format_from_stream(stream, protocol, host))
  161. else:
  162. cdns = config.get('cdns')
  163. if not cdns:
  164. raise ExtractorError('Failed to get CDN data', expected=True)
  165. for name, cdn in cdns.items():
  166. # LimeLight streams don't seem to work
  167. if cdn.get('name') == 'LIMELIGHT':
  168. continue
  169. protocol = cdn.get('protocol')
  170. host = remove_end(cdn.get('host', ''), '/')
  171. if not (protocol and host):
  172. continue
  173. prefix = cdn.get('pathprefix', '')
  174. if prefix and not prefix.endswith('/'):
  175. prefix = '%s/' % prefix
  176. preference = 0
  177. if protocol == 'rtmp':
  178. preference = -2
  179. elif 'prog' in name.lower():
  180. preference = 1
  181. for stream in streams:
  182. formats.append(
  183. NFLIE.format_from_stream(stream, protocol, host,
  184. prefix, preference, name))
  185. self._sort_formats(formats)
  186. thumbnail = None
  187. for q in ('xl', 'l', 'm', 's', 'xs'):
  188. thumbnail = video_data.get('imagePaths', {}).get(q)
  189. if thumbnail:
  190. break
  191. return {
  192. 'id': video_id,
  193. 'title': video_data.get('headline'),
  194. 'formats': formats,
  195. 'description': video_data.get('caption'),
  196. 'duration': video_data.get('duration'),
  197. 'thumbnail': thumbnail,
  198. 'timestamp': int_or_none(video_data.get('posted'), 1000),
  199. }