You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
5.4 KiB

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