[cleanup] Misc (#8968)
[yt-dlp.git] / yt_dlp / extractor / wwe.py
blob9bbd477c33e3583e9093d1f173eb724b8fb4762f
1 import re
3 from .common import InfoExtractor
4 from ..compat import compat_str
5 from ..utils import (
6 try_get,
7 unescapeHTML,
8 url_or_none,
9 urljoin,
13 class WWEBaseIE(InfoExtractor):
14 _SUBTITLE_LANGS = {
15 'English': 'en',
16 'Deutsch': 'de',
19 def _extract_entry(self, data, url, video_id=None):
20 video_id = compat_str(video_id or data['nid'])
21 title = data['title']
23 formats = self._extract_m3u8_formats(
24 data['file'], video_id, 'mp4', entry_protocol='m3u8_native',
25 m3u8_id='hls')
27 description = data.get('description')
28 thumbnail = urljoin(url, data.get('image'))
29 series = data.get('show_name')
30 episode = data.get('episode_name')
32 subtitles = {}
33 tracks = data.get('tracks')
34 if isinstance(tracks, list):
35 for track in tracks:
36 if not isinstance(track, dict):
37 continue
38 if track.get('kind') != 'captions':
39 continue
40 track_file = url_or_none(track.get('file'))
41 if not track_file:
42 continue
43 label = track.get('label')
44 lang = self._SUBTITLE_LANGS.get(label, label) or 'en'
45 subtitles.setdefault(lang, []).append({
46 'url': track_file,
49 return {
50 'id': video_id,
51 'title': title,
52 'description': description,
53 'thumbnail': thumbnail,
54 'series': series,
55 'episode': episode,
56 'formats': formats,
57 'subtitles': subtitles,
61 class WWEIE(WWEBaseIE):
62 _VALID_URL = r'https?://(?:[^/]+\.)?wwe\.com/(?:[^/]+/)*videos/(?P<id>[^/?#&]+)'
63 _TESTS = [{
64 'url': 'https://www.wwe.com/videos/daniel-bryan-vs-andrade-cien-almas-smackdown-live-sept-4-2018',
65 'md5': '92811c6a14bfc206f7a6a9c5d9140184',
66 'info_dict': {
67 'id': '40048199',
68 'ext': 'mp4',
69 'title': 'Daniel Bryan vs. Andrade "Cien" Almas: SmackDown LIVE, Sept. 4, 2018',
70 'description': 'md5:2d7424dbc6755c61a0e649d2a8677f67',
71 'thumbnail': r're:^https?://.*\.jpg$',
73 }, {
74 'url': 'https://de.wwe.com/videos/gran-metalik-vs-tony-nese-wwe-205-live-sept-4-2018',
75 'only_matching': True,
78 def _real_extract(self, url):
79 display_id = self._match_id(url)
80 webpage = self._download_webpage(url, display_id)
82 landing = self._parse_json(
83 self._html_search_regex(
84 r'(?s)Drupal\.settings\s*,\s*({.+?})\s*\)\s*;',
85 webpage, 'drupal settings'),
86 display_id)['WWEVideoLanding']
88 data = landing['initialVideo']['playlist'][0]
89 video_id = landing.get('initialVideoId')
91 info = self._extract_entry(data, url, video_id)
92 info['display_id'] = display_id
93 return info
96 class WWEPlaylistIE(WWEBaseIE):
97 _VALID_URL = r'https?://(?:[^/]+\.)?wwe\.com/(?:[^/]+/)*(?P<id>[^/?#&]+)'
98 _TESTS = [{
99 'url': 'https://www.wwe.com/shows/raw/2018-11-12',
100 'info_dict': {
101 'id': '2018-11-12',
103 'playlist_mincount': 11,
104 }, {
105 'url': 'http://www.wwe.com/article/walk-the-prank-wwe-edition',
106 'only_matching': True,
107 }, {
108 'url': 'https://www.wwe.com/shows/wwenxt/article/matt-riddle-interview',
109 'only_matching': True,
112 @classmethod
113 def suitable(cls, url):
114 return False if WWEIE.suitable(url) else super(WWEPlaylistIE, cls).suitable(url)
116 def _real_extract(self, url):
117 display_id = self._match_id(url)
118 webpage = self._download_webpage(url, display_id)
120 entries = []
121 for mobj in re.finditer(
122 r'data-video\s*=\s*(["\'])(?P<data>{.+?})\1', webpage):
123 video = self._parse_json(
124 mobj.group('data'), display_id, transform_source=unescapeHTML,
125 fatal=False)
126 if not video:
127 continue
128 data = try_get(video, lambda x: x['playlist'][0], dict)
129 if not data:
130 continue
131 try:
132 entry = self._extract_entry(data, url)
133 except Exception:
134 continue
135 entry['extractor_key'] = WWEIE.ie_key()
136 entries.append(entry)
138 return self.playlist_result(entries, display_id)