]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/pokemon.py
[VideocampusSachsen] Improve extractor (#3604)
[yt-dlp.git] / yt_dlp / extractor / pokemon.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 extract_attributes,
7 int_or_none,
8 js_to_json,
9 merge_dicts,
10 )
11
12
13 class PokemonIE(InfoExtractor):
14 _VALID_URL = r'https?://(?:www\.)?pokemon\.com/[a-z]{2}(?:.*?play=(?P<id>[a-z0-9]{32})|/(?:[^/]+/)+(?P<display_id>[^/?#&]+))'
15 _TESTS = [{
16 'url': 'https://www.pokemon.com/us/pokemon-episodes/20_30-the-ol-raise-and-switch/',
17 'md5': '2fe8eaec69768b25ef898cda9c43062e',
18 'info_dict': {
19 'id': 'afe22e30f01c41f49d4f1d9eab5cd9a4',
20 'ext': 'mp4',
21 'title': 'The Ol’ Raise and Switch!',
22 'description': 'md5:7db77f7107f98ba88401d3adc80ff7af',
23 },
24 'add_id': ['LimelightMedia'],
25 }, {
26 # no data-video-title
27 'url': 'https://www.pokemon.com/fr/episodes-pokemon/films-pokemon/pokemon-lascension-de-darkrai-2008',
28 'info_dict': {
29 'id': 'dfbaf830d7e54e179837c50c0c6cc0e1',
30 'ext': 'mp4',
31 'title': "Pokémon : L'ascension de Darkrai",
32 'description': 'md5:d1dbc9e206070c3e14a06ff557659fb5',
33 },
34 'add_id': ['LimelightMedia'],
35 'params': {
36 'skip_download': True,
37 },
38 }, {
39 'url': 'http://www.pokemon.com/uk/pokemon-episodes/?play=2e8b5c761f1d4a9286165d7748c1ece2',
40 'only_matching': True,
41 }, {
42 'url': 'http://www.pokemon.com/fr/episodes-pokemon/18_09-un-hiver-inattendu/',
43 'only_matching': True,
44 }, {
45 'url': 'http://www.pokemon.com/de/pokemon-folgen/01_20-bye-bye-smettbo/',
46 'only_matching': True,
47 }]
48
49 def _real_extract(self, url):
50 video_id, display_id = self._match_valid_url(url).groups()
51 webpage = self._download_webpage(url, video_id or display_id)
52 video_data = extract_attributes(self._search_regex(
53 r'(<[^>]+data-video-id="%s"[^>]*>)' % (video_id if video_id else '[a-z0-9]{32}'),
54 webpage, 'video data element'))
55 video_id = video_data['data-video-id']
56 title = video_data.get('data-video-title') or self._html_search_meta(
57 'pkm-title', webpage, ' title', default=None) or self._search_regex(
58 r'<h1[^>]+\bclass=["\']us-title[^>]+>([^<]+)', webpage, 'title')
59 return {
60 '_type': 'url_transparent',
61 'id': video_id,
62 'url': 'limelight:media:%s' % video_id,
63 'title': title,
64 'description': video_data.get('data-video-summary'),
65 'thumbnail': video_data.get('data-video-poster'),
66 'series': 'Pokémon',
67 'season_number': int_or_none(video_data.get('data-video-season')),
68 'episode': title,
69 'episode_number': int_or_none(video_data.get('data-video-episode')),
70 'ie_key': 'LimelightMedia',
71 }
72
73
74 class PokemonWatchIE(InfoExtractor):
75 _VALID_URL = r'https?://watch\.pokemon\.com/[a-z]{2}-[a-z]{2}/(?:#/)?player(?:\.html)?\?id=(?P<id>[a-z0-9]{32})'
76 _API_URL = 'https://www.pokemon.com/api/pokemontv/v2/channels/{0:}'
77 _TESTS = [{
78 'url': 'https://watch.pokemon.com/en-us/player.html?id=8309a40969894a8e8d5bc1311e9c5667',
79 'md5': '62833938a31e61ab49ada92f524c42ff',
80 'info_dict': {
81 'id': '8309a40969894a8e8d5bc1311e9c5667',
82 'ext': 'mp4',
83 'title': 'Lillier and the Staff!',
84 'description': 'md5:338841b8c21b283d24bdc9b568849f04',
85 }
86 }, {
87 'url': 'https://watch.pokemon.com/en-us/#/player?id=3fe7752ba09141f0b0f7756d1981c6b2',
88 'only_matching': True
89 }, {
90 'url': 'https://watch.pokemon.com/de-de/player.html?id=b3c402e111a4459eb47e12160ab0ba07',
91 'only_matching': True
92 }]
93
94 def _extract_media(self, channel_array, video_id):
95 for channel in channel_array:
96 for media in channel.get('media'):
97 if media.get('id') == video_id:
98 return media
99 return None
100
101 def _real_extract(self, url):
102 video_id = self._match_id(url)
103
104 info = {
105 '_type': 'url',
106 'id': video_id,
107 'url': 'limelight:media:%s' % video_id,
108 'ie_key': 'LimelightMedia',
109 }
110
111 # API call can be avoided entirely if we are listing formats
112 if self.get_param('listformats', False):
113 return info
114
115 webpage = self._download_webpage(url, video_id)
116 build_vars = self._parse_json(self._search_regex(
117 r'(?s)buildVars\s*=\s*({.*?})', webpage, 'build vars'),
118 video_id, transform_source=js_to_json)
119 region = build_vars.get('region')
120 channel_array = self._download_json(self._API_URL.format(region), video_id)
121 video_data = self._extract_media(channel_array, video_id)
122
123 if video_data is None:
124 raise ExtractorError(
125 'Video %s does not exist' % video_id, expected=True)
126
127 info['_type'] = 'url_transparent'
128 images = video_data.get('images')
129
130 return merge_dicts(info, {
131 'title': video_data.get('title'),
132 'description': video_data.get('description'),
133 'thumbnail': images.get('medium') or images.get('small'),
134 'series': 'Pokémon',
135 'season_number': int_or_none(video_data.get('season')),
136 'episode': video_data.get('title'),
137 'episode_number': int_or_none(video_data.get('episode')),
138 })
139
140
141 class PokemonSoundLibraryIE(InfoExtractor):
142 _VALID_URL = r'https?://soundlibrary\.pokemon\.co\.jp'
143
144 _TESTS = [{
145 'url': 'https://soundlibrary.pokemon.co.jp/',
146 'info_dict': {
147 'title': 'Pokémon Diamond and Pearl Sound Tracks',
148 },
149 'playlist_mincount': 149,
150 }]
151
152 def _real_extract(self, url):
153 musicbox_webpage = self._download_webpage(
154 'https://soundlibrary.pokemon.co.jp/musicbox', None,
155 'Downloading list of songs')
156 song_titles = [x.group(1) for x in re.finditer(r'<span>([^>]+?)</span><br/>をてもち曲に加えます。', musicbox_webpage)]
157 song_titles = song_titles[4::2]
158
159 # each songs don't have permalink; instead we return all songs at once
160 song_entries = [{
161 'id': f'pokemon-soundlibrary-{song_id}',
162 'url': f'https://soundlibrary.pokemon.co.jp/api/assets/signing/sounds/wav/{song_id}.wav',
163 # note: the server always serves MP3 files, despite its extension of the URL above
164 'ext': 'mp3',
165 'acodec': 'mp3',
166 'vcodec': 'none',
167 'title': song_title,
168 'track': song_title,
169 'artist': 'Nintendo / Creatures Inc. / GAME FREAK inc.',
170 'uploader': 'Pokémon',
171 'release_year': 2006,
172 'release_date': '20060928',
173 'track_number': song_id,
174 'album': 'Pokémon Diamond and Pearl',
175 } for song_id, song_title in enumerate(song_titles, 1)]
176
177 return self.playlist_result(song_entries, playlist_title='Pokémon Diamond and Pearl Sound Tracks')