]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/pokemon.py
[cleanup] Revert unnecessary changes in 51d9739f8031fb37d8e25b0e9f1abea561e3d2e3
[yt-dlp.git] / yt_dlp / extractor / pokemon.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8 ExtractorError,
9 extract_attributes,
10 int_or_none,
11 js_to_json,
12 merge_dicts,
13 )
14
15
16 class PokemonIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:www\.)?pokemon\.com/[a-z]{2}(?:.*?play=(?P<id>[a-z0-9]{32})|/(?:[^/]+/)+(?P<display_id>[^/?#&]+))'
18 _TESTS = [{
19 'url': 'https://www.pokemon.com/us/pokemon-episodes/20_30-the-ol-raise-and-switch/',
20 'md5': '2fe8eaec69768b25ef898cda9c43062e',
21 'info_dict': {
22 'id': 'afe22e30f01c41f49d4f1d9eab5cd9a4',
23 'ext': 'mp4',
24 'title': 'The Ol’ Raise and Switch!',
25 'description': 'md5:7db77f7107f98ba88401d3adc80ff7af',
26 },
27 'add_id': ['LimelightMedia'],
28 }, {
29 # no data-video-title
30 'url': 'https://www.pokemon.com/fr/episodes-pokemon/films-pokemon/pokemon-lascension-de-darkrai-2008',
31 'info_dict': {
32 'id': 'dfbaf830d7e54e179837c50c0c6cc0e1',
33 'ext': 'mp4',
34 'title': "Pokémon : L'ascension de Darkrai",
35 'description': 'md5:d1dbc9e206070c3e14a06ff557659fb5',
36 },
37 'add_id': ['LimelightMedia'],
38 'params': {
39 'skip_download': True,
40 },
41 }, {
42 'url': 'http://www.pokemon.com/uk/pokemon-episodes/?play=2e8b5c761f1d4a9286165d7748c1ece2',
43 'only_matching': True,
44 }, {
45 'url': 'http://www.pokemon.com/fr/episodes-pokemon/18_09-un-hiver-inattendu/',
46 'only_matching': True,
47 }, {
48 'url': 'http://www.pokemon.com/de/pokemon-folgen/01_20-bye-bye-smettbo/',
49 'only_matching': True,
50 }]
51
52 def _real_extract(self, url):
53 video_id, display_id = re.match(self._VALID_URL, url).groups()
54 webpage = self._download_webpage(url, video_id or display_id)
55 video_data = extract_attributes(self._search_regex(
56 r'(<[^>]+data-video-id="%s"[^>]*>)' % (video_id if video_id else '[a-z0-9]{32}'),
57 webpage, 'video data element'))
58 video_id = video_data['data-video-id']
59 title = video_data.get('data-video-title') or self._html_search_meta(
60 'pkm-title', webpage, ' title', default=None) or self._search_regex(
61 r'<h1[^>]+\bclass=["\']us-title[^>]+>([^<]+)', webpage, 'title')
62 return {
63 '_type': 'url_transparent',
64 'id': video_id,
65 'url': 'limelight:media:%s' % video_id,
66 'title': title,
67 'description': video_data.get('data-video-summary'),
68 'thumbnail': video_data.get('data-video-poster'),
69 'series': 'Pokémon',
70 'season_number': int_or_none(video_data.get('data-video-season')),
71 'episode': title,
72 'episode_number': int_or_none(video_data.get('data-video-episode')),
73 'ie_key': 'LimelightMedia',
74 }
75
76
77 class PokemonWatchIE(InfoExtractor):
78 _VALID_URL = r'https?://watch\.pokemon\.com/[a-z]{2}-[a-z]{2}/(?:#/)?player(?:\.html)?\?id=(?P<id>[a-z0-9]{32})'
79 _API_URL = 'https://www.pokemon.com/api/pokemontv/v2/channels/{0:}'
80 _TESTS = [{
81 'url': 'https://watch.pokemon.com/en-us/player.html?id=8309a40969894a8e8d5bc1311e9c5667',
82 'md5': '62833938a31e61ab49ada92f524c42ff',
83 'info_dict': {
84 'id': '8309a40969894a8e8d5bc1311e9c5667',
85 'ext': 'mp4',
86 'title': 'Lillier and the Staff!',
87 'description': 'md5:338841b8c21b283d24bdc9b568849f04',
88 }
89 }, {
90 'url': 'https://watch.pokemon.com/en-us/#/player?id=3fe7752ba09141f0b0f7756d1981c6b2',
91 'only_matching': True
92 }, {
93 'url': 'https://watch.pokemon.com/de-de/player.html?id=b3c402e111a4459eb47e12160ab0ba07',
94 'only_matching': True
95 }]
96
97 def _extract_media(self, channel_array, video_id):
98 for channel in channel_array:
99 for media in channel.get('media'):
100 if media.get('id') == video_id:
101 return media
102 return None
103
104 def _real_extract(self, url):
105 video_id = self._match_id(url)
106
107 info = {
108 '_type': 'url',
109 'id': video_id,
110 'url': 'limelight:media:%s' % video_id,
111 'ie_key': 'LimelightMedia',
112 }
113
114 # API call can be avoided entirely if we are listing formats
115 if self.get_param('listformats', False):
116 return info
117
118 webpage = self._download_webpage(url, video_id)
119 build_vars = self._parse_json(self._search_regex(
120 r'(?s)buildVars\s*=\s*({.*?})', webpage, 'build vars'),
121 video_id, transform_source=js_to_json)
122 region = build_vars.get('region')
123 channel_array = self._download_json(self._API_URL.format(region), video_id)
124 video_data = self._extract_media(channel_array, video_id)
125
126 if video_data is None:
127 raise ExtractorError(
128 'Video %s does not exist' % video_id, expected=True)
129
130 info['_type'] = 'url_transparent'
131 images = video_data.get('images')
132
133 return merge_dicts(info, {
134 'title': video_data.get('title'),
135 'description': video_data.get('description'),
136 'thumbnail': images.get('medium') or images.get('small'),
137 'series': 'Pokémon',
138 'season_number': int_or_none(video_data.get('season')),
139 'episode': video_data.get('title'),
140 'episode_number': int_or_none(video_data.get('episode')),
141 })