]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/nfl.py
[DRTV] Improve `_VALID_URL` (#3708)
[yt-dlp.git] / yt_dlp / extractor / nfl.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 clean_html,
6 determine_ext,
7 get_element_by_class,
8 )
9
10
11 class NFLBaseIE(InfoExtractor):
12 _VALID_URL_BASE = r'''(?x)
13 https?://
14 (?P<host>
15 (?:www\.)?
16 (?:
17 (?:
18 nfl|
19 buffalobills|
20 miamidolphins|
21 patriots|
22 newyorkjets|
23 baltimoreravens|
24 bengals|
25 clevelandbrowns|
26 steelers|
27 houstontexans|
28 colts|
29 jaguars|
30 (?:titansonline|tennesseetitans)|
31 denverbroncos|
32 (?:kc)?chiefs|
33 raiders|
34 chargers|
35 dallascowboys|
36 giants|
37 philadelphiaeagles|
38 (?:redskins|washingtonfootball)|
39 chicagobears|
40 detroitlions|
41 packers|
42 vikings|
43 atlantafalcons|
44 panthers|
45 neworleanssaints|
46 buccaneers|
47 azcardinals|
48 (?:stlouis|the)rams|
49 49ers|
50 seahawks
51 )\.com|
52 .+?\.clubs\.nfl\.com
53 )
54 )/
55 '''
56 _VIDEO_CONFIG_REGEX = r'<script[^>]+id="[^"]*video-config-[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}[^"]*"[^>]*>\s*({.+})'
57 _WORKING = False
58
59 def _parse_video_config(self, video_config, display_id):
60 video_config = self._parse_json(video_config, display_id)
61 item = video_config['playlist'][0]
62 mcp_id = item.get('mcpID')
63 if mcp_id:
64 info = self.url_result(
65 'anvato:GXvEgwyJeWem8KCYXfeoHWknwP48Mboj:' + mcp_id,
66 'Anvato', mcp_id)
67 else:
68 media_id = item.get('id') or item['entityId']
69 title = item['title']
70 item_url = item['url']
71 info = {'id': media_id}
72 ext = determine_ext(item_url)
73 if ext == 'm3u8':
74 info['formats'] = self._extract_m3u8_formats(item_url, media_id, 'mp4')
75 self._sort_formats(info['formats'])
76 else:
77 info['url'] = item_url
78 if item.get('audio') is True:
79 info['vcodec'] = 'none'
80 is_live = video_config.get('live') is True
81 thumbnails = None
82 image_url = item.get(item.get('imageSrc')) or item.get(item.get('posterImage'))
83 if image_url:
84 thumbnails = [{
85 'url': image_url,
86 'ext': determine_ext(image_url, 'jpg'),
87 }]
88 info.update({
89 'title': title,
90 'is_live': is_live,
91 'description': clean_html(item.get('description')),
92 'thumbnails': thumbnails,
93 })
94 return info
95
96
97 class NFLIE(NFLBaseIE):
98 IE_NAME = 'nfl.com'
99 _VALID_URL = NFLBaseIE._VALID_URL_BASE + r'(?:videos?|listen|audio)/(?P<id>[^/#?&]+)'
100 _TESTS = [{
101 'url': 'https://www.nfl.com/videos/baker-mayfield-s-game-changing-plays-from-3-td-game-week-14',
102 'info_dict': {
103 'id': '899441',
104 'ext': 'mp4',
105 'title': "Baker Mayfield's game-changing plays from 3-TD game Week 14",
106 'description': 'md5:85e05a3cc163f8c344340f220521136d',
107 'upload_date': '20201215',
108 'timestamp': 1608009755,
109 'thumbnail': r're:^https?://.*\.jpg$',
110 'uploader': 'NFL',
111 }
112 }, {
113 'url': 'https://www.chiefs.com/listen/patrick-mahomes-travis-kelce-react-to-win-over-dolphins-the-breakdown',
114 'md5': '6886b32c24b463038c760ceb55a34566',
115 'info_dict': {
116 'id': 'd87e8790-3e14-11eb-8ceb-ff05c2867f99',
117 'ext': 'mp3',
118 'title': 'Patrick Mahomes, Travis Kelce React to Win Over Dolphins | The Breakdown',
119 'description': 'md5:12ada8ee70e6762658c30e223e095075',
120 }
121 }, {
122 'url': 'https://www.buffalobills.com/video/buffalo-bills-military-recognition-week-14',
123 'only_matching': True,
124 }, {
125 'url': 'https://www.raiders.com/audio/instant-reactions-raiders-week-14-loss-to-indianapolis-colts-espn-jason-fitz',
126 'only_matching': True,
127 }]
128
129 def _real_extract(self, url):
130 display_id = self._match_id(url)
131 webpage = self._download_webpage(url, display_id)
132 return self._parse_video_config(self._search_regex(
133 self._VIDEO_CONFIG_REGEX, webpage, 'video config'), display_id)
134
135
136 class NFLArticleIE(NFLBaseIE):
137 IE_NAME = 'nfl.com:article'
138 _VALID_URL = NFLBaseIE._VALID_URL_BASE + r'news/(?P<id>[^/#?&]+)'
139 _TEST = {
140 'url': 'https://www.buffalobills.com/news/the-only-thing-we-ve-earned-is-the-noise-bills-coaches-discuss-handling-rising-e',
141 'info_dict': {
142 'id': 'the-only-thing-we-ve-earned-is-the-noise-bills-coaches-discuss-handling-rising-e',
143 'title': "'The only thing we've earned is the noise' | Bills coaches discuss handling rising expectations",
144 },
145 'playlist_count': 4,
146 }
147
148 def _real_extract(self, url):
149 display_id = self._match_id(url)
150 webpage = self._download_webpage(url, display_id)
151 entries = []
152 for video_config in re.findall(self._VIDEO_CONFIG_REGEX, webpage):
153 entries.append(self._parse_video_config(video_config, display_id))
154 title = clean_html(get_element_by_class(
155 'nfl-c-article__title', webpage)) or self._html_search_meta(
156 ['og:title', 'twitter:title'], webpage)
157 return self.playlist_result(entries, display_id, title)