pokemon.py 5.3 KB

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