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.

231 lines
8.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>[^/#?&]+)
  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': r'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': r'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. # lowercase data-contentid
  108. 'url': 'http://www.steelers.com/news/article-1/Tomlin-on-Ben-getting-Vick-ready/56399c96-4160-48cf-a7ad-1d17d4a3aef7',
  109. 'info_dict': {
  110. 'id': '12693586-6ea9-4743-9c1c-02c59e4a5ef2',
  111. 'ext': 'mp4',
  112. 'title': 'Tomlin looks ahead to Ravens on a short week',
  113. 'description': 'md5:32f3f7b139f43913181d5cbb24ecad75',
  114. 'timestamp': 1443459651,
  115. 'upload_date': '20150928',
  116. },
  117. 'params': {
  118. 'skip_download': True,
  119. },
  120. }, {
  121. 'url': 'http://www.nfl.com/videos/nfl-network-top-ten/09000d5d810a6bd4/Top-10-Gutsiest-Performances-Jack-Youngblood',
  122. 'only_matching': True,
  123. }, {
  124. 'url': 'http://www.buffalobills.com/video/videos/Rex_Ryan_Show_World_Wide_Rex/b1dcfab2-3190-4bb1-bfc0-d6e603d6601a',
  125. 'only_matching': True,
  126. }]
  127. @staticmethod
  128. def prepend_host(host, url):
  129. if not url.startswith('http'):
  130. if not url.startswith('/'):
  131. url = '/%s' % url
  132. url = 'http://{0:}{1:}'.format(host, url)
  133. return url
  134. @staticmethod
  135. def format_from_stream(stream, protocol, host, path_prefix='',
  136. preference=0, note=None):
  137. url = '{protocol:}://{host:}/{prefix:}{path:}'.format(
  138. protocol=protocol,
  139. host=host,
  140. prefix=path_prefix,
  141. path=stream.get('path'),
  142. )
  143. return {
  144. 'url': url,
  145. 'vbr': int_or_none(stream.get('rate', 0), 1000),
  146. 'preference': preference,
  147. 'format_note': note,
  148. }
  149. def _real_extract(self, url):
  150. mobj = re.match(self._VALID_URL, url)
  151. video_id, host = mobj.group('id'), mobj.group('host')
  152. webpage = self._download_webpage(url, video_id)
  153. config_url = NFLIE.prepend_host(host, self._search_regex(
  154. r'(?:(?:config|configURL)\s*:\s*|<nflcs:avplayer[^>]+data-config\s*=\s*)(["\'])(?P<config>.+?)\1',
  155. webpage, 'config URL', default='static/content/static/config/video/config.json',
  156. group='config'))
  157. # For articles, the id in the url is not the video id
  158. video_id = self._search_regex(
  159. r'(?:<nflcs:avplayer[^>]+data-content[Ii]d\s*=\s*|content[Ii]d\s*:\s*)(["\'])(?P<id>(?:(?!\1).)+)\1',
  160. webpage, 'video id', default=video_id, group='id')
  161. config = self._download_json(config_url, video_id, 'Downloading player config')
  162. url_template = NFLIE.prepend_host(
  163. host, '{contentURLTemplate:}'.format(**config))
  164. video_data = self._download_json(
  165. url_template.format(id=video_id), video_id)
  166. formats = []
  167. cdn_data = video_data.get('cdnData', {})
  168. streams = cdn_data.get('bitrateInfo', [])
  169. if cdn_data.get('format') == 'EXTERNAL_HTTP_STREAM':
  170. parts = compat_urllib_parse_urlparse(cdn_data.get('uri'))
  171. protocol, host = parts.scheme, parts.netloc
  172. for stream in streams:
  173. formats.append(
  174. NFLIE.format_from_stream(stream, protocol, host))
  175. else:
  176. cdns = config.get('cdns')
  177. if not cdns:
  178. raise ExtractorError('Failed to get CDN data', expected=True)
  179. for name, cdn in cdns.items():
  180. # LimeLight streams don't seem to work
  181. if cdn.get('name') == 'LIMELIGHT':
  182. continue
  183. protocol = cdn.get('protocol')
  184. host = remove_end(cdn.get('host', ''), '/')
  185. if not (protocol and host):
  186. continue
  187. prefix = cdn.get('pathprefix', '')
  188. if prefix and not prefix.endswith('/'):
  189. prefix = '%s/' % prefix
  190. preference = 0
  191. if protocol == 'rtmp':
  192. preference = -2
  193. elif 'prog' in name.lower():
  194. preference = 1
  195. for stream in streams:
  196. formats.append(
  197. NFLIE.format_from_stream(stream, protocol, host,
  198. prefix, preference, name))
  199. self._sort_formats(formats)
  200. thumbnail = None
  201. for q in ('xl', 'l', 'm', 's', 'xs'):
  202. thumbnail = video_data.get('imagePaths', {}).get(q)
  203. if thumbnail:
  204. break
  205. return {
  206. 'id': video_id,
  207. 'title': video_data.get('headline'),
  208. 'formats': formats,
  209. 'description': video_data.get('caption'),
  210. 'duration': video_data.get('duration'),
  211. 'thumbnail': thumbnail,
  212. 'timestamp': int_or_none(video_data.get('posted'), 1000),
  213. }