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.

167 lines
6.4 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_HTTPError,
  6. compat_urllib_parse_unquote_plus,
  7. )
  8. from ..utils import (
  9. determine_ext,
  10. int_or_none,
  11. js_to_json,
  12. sanitized_Request,
  13. ExtractorError,
  14. urlencode_postdata
  15. )
  16. class FunimationIE(InfoExtractor):
  17. _VALID_URL = r'https?://(?:www\.)?funimation(?:\.com|now\.uk)/shows/[^/]+/(?P<id>[^/?#&]+)'
  18. _NETRC_MACHINE = 'funimation'
  19. _TESTS = [{
  20. 'url': 'https://www.funimation.com/shows/hacksign/role-play/',
  21. 'info_dict': {
  22. 'id': '91144',
  23. 'display_id': 'role-play',
  24. 'ext': 'mp4',
  25. 'title': '.hack//SIGN - Role Play',
  26. 'description': 'md5:b602bdc15eef4c9bbb201bb6e6a4a2dd',
  27. 'thumbnail': r're:https?://.*\.jpg',
  28. },
  29. 'params': {
  30. # m3u8 download
  31. 'skip_download': True,
  32. },
  33. }, {
  34. 'url': 'https://www.funimation.com/shows/attack-on-titan-junior-high/broadcast-dub-preview/',
  35. 'info_dict': {
  36. 'id': '9635',
  37. 'display_id': 'broadcast-dub-preview',
  38. 'ext': 'mp4',
  39. 'title': 'Attack on Titan: Junior High - Broadcast Dub Preview',
  40. 'description': 'md5:f8ec49c0aff702a7832cd81b8a44f803',
  41. 'thumbnail': r're:https?://.*\.(?:jpg|png)',
  42. },
  43. 'skip': 'Access without user interaction is forbidden by CloudFlare',
  44. }, {
  45. 'url': 'https://www.funimationnow.uk/shows/puzzle-dragons-x/drop-impact/simulcast/',
  46. 'only_matching': True,
  47. }]
  48. _LOGIN_URL = 'http://www.funimation.com/login'
  49. def _extract_cloudflare_session_ua(self, url):
  50. ci_session_cookie = self._get_cookies(url).get('ci_session')
  51. if ci_session_cookie:
  52. ci_session = compat_urllib_parse_unquote_plus(ci_session_cookie.value)
  53. # ci_session is a string serialized by PHP function serialize()
  54. # This case is simple enough to use regular expressions only
  55. return self._search_regex(
  56. r'"user_agent";s:\d+:"([^"]+)"', ci_session, 'user agent',
  57. default=None)
  58. def _login(self):
  59. (username, password) = self._get_login_info()
  60. if username is None:
  61. return
  62. data = urlencode_postdata({
  63. 'email_field': username,
  64. 'password_field': password,
  65. })
  66. user_agent = self._extract_cloudflare_session_ua(self._LOGIN_URL)
  67. if not user_agent:
  68. user_agent = 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0'
  69. login_request = sanitized_Request(self._LOGIN_URL, data, headers={
  70. 'User-Agent': user_agent,
  71. 'Content-Type': 'application/x-www-form-urlencoded'
  72. })
  73. login_page = self._download_webpage(
  74. login_request, None, 'Logging in as %s' % username)
  75. if any(p in login_page for p in ('funimation.com/logout', '>Log Out<')):
  76. return
  77. error = self._html_search_regex(
  78. r'(?s)<div[^>]+id=["\']errorMessages["\'][^>]*>(.+?)</div>',
  79. login_page, 'error messages', default=None)
  80. if error:
  81. raise ExtractorError('Unable to login: %s' % error, expected=True)
  82. raise ExtractorError('Unable to log in')
  83. def _real_initialize(self):
  84. self._login()
  85. def _real_extract(self, url):
  86. display_id = self._match_id(url)
  87. webpage = self._download_webpage(url, display_id)
  88. def _search_kane(name):
  89. return self._search_regex(
  90. r"KANE_customdimensions\.%s\s*=\s*'([^']+)';" % name,
  91. webpage, name, default=None)
  92. title_data = self._parse_json(self._search_regex(
  93. r'TITLE_DATA\s*=\s*({[^}]+})',
  94. webpage, 'title data', default=''),
  95. display_id, js_to_json, fatal=False) or {}
  96. video_id = title_data.get('id') or self._search_regex([
  97. r"KANE_customdimensions.videoID\s*=\s*'(\d+)';",
  98. r'<iframe[^>]+src="/player/(\d+)"',
  99. ], webpage, 'video_id', default=None)
  100. if not video_id:
  101. player_url = self._html_search_meta([
  102. 'al:web:url',
  103. 'og:video:url',
  104. 'og:video:secure_url',
  105. ], webpage, fatal=True)
  106. video_id = self._search_regex(r'/player/(\d+)', player_url, 'video id')
  107. title = episode = title_data.get('title') or _search_kane('videoTitle') or self._og_search_title(webpage)
  108. series = _search_kane('showName')
  109. if series:
  110. title = '%s - %s' % (series, title)
  111. description = self._html_search_meta(['description', 'og:description'], webpage, fatal=True)
  112. try:
  113. sources = self._download_json(
  114. 'https://prod-api-funimationnow.dadcdigital.com/api/source/catalog/video/%s/signed/' % video_id,
  115. video_id)['items']
  116. except ExtractorError as e:
  117. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  118. error = self._parse_json(e.cause.read(), video_id)['errors'][0]
  119. raise ExtractorError('%s said: %s' % (
  120. self.IE_NAME, error.get('detail') or error.get('title')), expected=True)
  121. raise
  122. formats = []
  123. for source in sources:
  124. source_url = source.get('src')
  125. if not source_url:
  126. continue
  127. source_type = source.get('videoType') or determine_ext(source_url)
  128. if source_type == 'm3u8':
  129. formats.extend(self._extract_m3u8_formats(
  130. source_url, video_id, 'mp4',
  131. m3u8_id='hls', fatal=False))
  132. else:
  133. formats.append({
  134. 'format_id': source_type,
  135. 'url': source_url,
  136. })
  137. self._sort_formats(formats)
  138. return {
  139. 'id': video_id,
  140. 'display_id': display_id,
  141. 'title': title,
  142. 'description': description,
  143. 'thumbnail': self._og_search_thumbnail(webpage),
  144. 'series': series,
  145. 'season_number': int_or_none(title_data.get('seasonNum') or _search_kane('season')),
  146. 'episode_number': int_or_none(title_data.get('episodeNum')),
  147. 'episode': episode,
  148. 'season_id': title_data.get('seriesId'),
  149. 'formats': formats,
  150. }