nhl.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. determine_ext,
  4. int_or_none,
  5. join_nonempty,
  6. parse_duration,
  7. parse_iso8601,
  8. )
  9. class NHLBaseIE(InfoExtractor):
  10. def _real_extract(self, url):
  11. site, tmp_id = self._match_valid_url(url).groups()
  12. video_data = self._download_json(
  13. 'https://{}/{}/{}id/v1/{}/details/web-v1.json'.format(
  14. self._CONTENT_DOMAIN, site[:3], 'item/' if site == 'mlb' else '', tmp_id), tmp_id)
  15. if video_data.get('type') != 'video':
  16. video_data = video_data['media']
  17. video = video_data.get('video')
  18. if video:
  19. video_data = video
  20. else:
  21. videos = video_data.get('videos')
  22. if videos:
  23. video_data = videos[0]
  24. video_id = str(video_data['id'])
  25. title = video_data['title']
  26. formats = []
  27. for playback in video_data.get('playbacks', []):
  28. playback_url = playback.get('url')
  29. if not playback_url:
  30. continue
  31. ext = determine_ext(playback_url)
  32. if ext == 'm3u8':
  33. m3u8_formats = self._extract_m3u8_formats(
  34. playback_url, video_id, 'mp4', 'm3u8_native',
  35. m3u8_id=playback.get('name', 'hls'), fatal=False)
  36. self._check_formats(m3u8_formats, video_id)
  37. formats.extend(m3u8_formats)
  38. else:
  39. height = int_or_none(playback.get('height'))
  40. formats.append({
  41. 'format_id': playback.get('name') or join_nonempty('http', height and f'{height}p'),
  42. 'url': playback_url,
  43. 'width': int_or_none(playback.get('width')),
  44. 'height': height,
  45. 'tbr': int_or_none(self._search_regex(r'_(\d+)[kK]', playback_url, 'bitrate', default=None)),
  46. })
  47. thumbnails = []
  48. cuts = video_data.get('image', {}).get('cuts') or []
  49. if isinstance(cuts, dict):
  50. cuts = cuts.values()
  51. for thumbnail_data in cuts:
  52. thumbnail_url = thumbnail_data.get('src')
  53. if not thumbnail_url:
  54. continue
  55. thumbnails.append({
  56. 'url': thumbnail_url,
  57. 'width': int_or_none(thumbnail_data.get('width')),
  58. 'height': int_or_none(thumbnail_data.get('height')),
  59. })
  60. return {
  61. 'id': video_id,
  62. 'title': title,
  63. 'description': video_data.get('description'),
  64. 'timestamp': parse_iso8601(video_data.get('date')),
  65. 'duration': parse_duration(video_data.get('duration')),
  66. 'thumbnails': thumbnails,
  67. 'formats': formats,
  68. }
  69. class NHLIE(NHLBaseIE):
  70. IE_NAME = 'nhl.com'
  71. _VALID_URL = r'https?://(?:www\.)?(?P<site>nhl|wch2016)\.com/(?:[^/]+/)*c-(?P<id>\d+)'
  72. _CONTENT_DOMAIN = 'nhl.bamcontent.com'
  73. _TESTS = [{
  74. # type=video
  75. 'url': 'https://www.nhl.com/video/anisimov-cleans-up-mess/t-277752844/c-43663503',
  76. 'md5': '0f7b9a8f986fb4b4eeeece9a56416eaf',
  77. 'info_dict': {
  78. 'id': '43663503',
  79. 'ext': 'mp4',
  80. 'title': 'Anisimov cleans up mess',
  81. 'description': 'md5:a02354acdfe900e940ce40706939ca63',
  82. 'timestamp': 1461288600,
  83. 'upload_date': '20160422',
  84. },
  85. }, {
  86. # type=article
  87. 'url': 'https://www.nhl.com/news/dennis-wideman-suspended/c-278258934',
  88. 'md5': '1f39f4ea74c1394dea110699a25b366c',
  89. 'info_dict': {
  90. 'id': '40784403',
  91. 'ext': 'mp4',
  92. 'title': 'Wideman suspended by NHL',
  93. 'description': 'Flames defenseman Dennis Wideman was banned 20 games for violation of Rule 40 (Physical Abuse of Officials)',
  94. 'upload_date': '20160204',
  95. 'timestamp': 1454544904,
  96. },
  97. }, {
  98. # Some m3u8 URLs are invalid (https://github.com/ytdl-org/youtube-dl/issues/10713)
  99. 'url': 'https://www.nhl.com/predators/video/poile-laviolette-on-subban-trade/t-277437416/c-44315003',
  100. 'md5': '50b2bb47f405121484dda3ccbea25459',
  101. 'info_dict': {
  102. 'id': '44315003',
  103. 'ext': 'mp4',
  104. 'title': 'Poile, Laviolette on Subban trade',
  105. 'description': 'General manager David Poile and head coach Peter Laviolette share their thoughts on acquiring P.K. Subban from Montreal (06/29/16)',
  106. 'timestamp': 1467242866,
  107. 'upload_date': '20160629',
  108. },
  109. }, {
  110. 'url': 'https://www.wch2016.com/video/caneur-best-of-game-2-micd-up/t-281230378/c-44983703',
  111. 'only_matching': True,
  112. }, {
  113. 'url': 'https://www.wch2016.com/news/3-stars-team-europe-vs-team-canada/c-282195068',
  114. 'only_matching': True,
  115. }]