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