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