logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

funimation.py (5828B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import random
  4. import string
  5. from .common import InfoExtractor
  6. from ..compat import compat_HTTPError
  7. from ..utils import (
  8. determine_ext,
  9. int_or_none,
  10. js_to_json,
  11. ExtractorError,
  12. urlencode_postdata
  13. )
  14. class FunimationIE(InfoExtractor):
  15. _VALID_URL = r'https?://(?:www\.)?funimation(?:\.com|now\.uk)/(?:[^/]+/)?shows/[^/]+/(?P<id>[^/?#&]+)'
  16. _NETRC_MACHINE = 'funimation'
  17. _TOKEN = None
  18. _TESTS = [{
  19. 'url': 'https://www.funimation.com/shows/hacksign/role-play/',
  20. 'info_dict': {
  21. 'id': '91144',
  22. 'display_id': 'role-play',
  23. 'ext': 'mp4',
  24. 'title': '.hack//SIGN - Role Play',
  25. 'description': 'md5:b602bdc15eef4c9bbb201bb6e6a4a2dd',
  26. 'thumbnail': r're:https?://.*\.jpg',
  27. },
  28. 'params': {
  29. # m3u8 download
  30. 'skip_download': True,
  31. },
  32. }, {
  33. 'url': 'https://www.funimation.com/shows/attack-on-titan-junior-high/broadcast-dub-preview/',
  34. 'info_dict': {
  35. 'id': '210051',
  36. 'display_id': 'broadcast-dub-preview',
  37. 'ext': 'mp4',
  38. 'title': 'Attack on Titan: Junior High - Broadcast Dub Preview',
  39. 'thumbnail': r're:https?://.*\.(?:jpg|png)',
  40. },
  41. 'params': {
  42. # m3u8 download
  43. 'skip_download': True,
  44. },
  45. }, {
  46. 'url': 'https://www.funimationnow.uk/shows/puzzle-dragons-x/drop-impact/simulcast/',
  47. 'only_matching': True,
  48. }, {
  49. # with lang code
  50. 'url': 'https://www.funimation.com/en/shows/hacksign/role-play/',
  51. 'only_matching': True,
  52. }]
  53. def _login(self):
  54. username, password = self._get_login_info()
  55. if username is None:
  56. return
  57. try:
  58. data = self._download_json(
  59. 'https://prod-api-funimationnow.dadcdigital.com/api/auth/login/',
  60. None, 'Logging in', data=urlencode_postdata({
  61. 'username': username,
  62. 'password': password,
  63. }))
  64. self._TOKEN = data['token']
  65. except ExtractorError as e:
  66. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
  67. error = self._parse_json(e.cause.read().decode(), None)['error']
  68. raise ExtractorError(error, expected=True)
  69. raise
  70. def _real_initialize(self):
  71. self._login()
  72. def _real_extract(self, url):
  73. display_id = self._match_id(url)
  74. webpage = self._download_webpage(url, display_id)
  75. def _search_kane(name):
  76. return self._search_regex(
  77. r"KANE_customdimensions\.%s\s*=\s*'([^']+)';" % name,
  78. webpage, name, default=None)
  79. title_data = self._parse_json(self._search_regex(
  80. r'TITLE_DATA\s*=\s*({[^}]+})',
  81. webpage, 'title data', default=''),
  82. display_id, js_to_json, fatal=False) or {}
  83. video_id = title_data.get('id') or self._search_regex([
  84. r"KANE_customdimensions.videoID\s*=\s*'(\d+)';",
  85. r'<iframe[^>]+src="/player/(\d+)',
  86. ], webpage, 'video_id', default=None)
  87. if not video_id:
  88. player_url = self._html_search_meta([
  89. 'al:web:url',
  90. 'og:video:url',
  91. 'og:video:secure_url',
  92. ], webpage, fatal=True)
  93. video_id = self._search_regex(r'/player/(\d+)', player_url, 'video id')
  94. title = episode = title_data.get('title') or _search_kane('videoTitle') or self._og_search_title(webpage)
  95. series = _search_kane('showName')
  96. if series:
  97. title = '%s - %s' % (series, title)
  98. description = self._html_search_meta(['description', 'og:description'], webpage, fatal=True)
  99. try:
  100. headers = {}
  101. if self._TOKEN:
  102. headers['Authorization'] = 'Token %s' % self._TOKEN
  103. sources = self._download_json(
  104. 'https://www.funimation.com/api/showexperience/%s/' % video_id,
  105. video_id, headers=headers, query={
  106. 'pinst_id': ''.join([random.choice(string.digits + string.ascii_letters) for _ in range(8)]),
  107. })['items']
  108. except ExtractorError as e:
  109. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  110. error = self._parse_json(e.cause.read(), video_id)['errors'][0]
  111. raise ExtractorError('%s said: %s' % (
  112. self.IE_NAME, error.get('detail') or error.get('title')), expected=True)
  113. raise
  114. formats = []
  115. for source in sources:
  116. source_url = source.get('src')
  117. if not source_url:
  118. continue
  119. source_type = source.get('videoType') or determine_ext(source_url)
  120. if source_type == 'm3u8':
  121. formats.extend(self._extract_m3u8_formats(
  122. source_url, video_id, 'mp4',
  123. m3u8_id='hls', fatal=False))
  124. else:
  125. formats.append({
  126. 'format_id': source_type,
  127. 'url': source_url,
  128. })
  129. self._sort_formats(formats)
  130. return {
  131. 'id': video_id,
  132. 'display_id': display_id,
  133. 'title': title,
  134. 'description': description,
  135. 'thumbnail': self._og_search_thumbnail(webpage),
  136. 'series': series,
  137. 'season_number': int_or_none(title_data.get('seasonNum') or _search_kane('season')),
  138. 'episode_number': int_or_none(title_data.get('episodeNum')),
  139. 'episode': episode,
  140. 'season_id': title_data.get('seriesId'),
  141. 'formats': formats,
  142. }