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.

154 lines
5.5 KiB

  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. def _login(self):
  50. username, password = self._get_login_info()
  51. if username is None:
  52. return
  53. try:
  54. data = self._download_json(
  55. 'https://prod-api-funimationnow.dadcdigital.com/api/auth/login/',
  56. None, 'Logging in', data=urlencode_postdata({
  57. 'username': username,
  58. 'password': password,
  59. }))
  60. self._TOKEN = data['token']
  61. except ExtractorError as e:
  62. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
  63. error = self._parse_json(e.cause.read().decode(), None)['error']
  64. raise ExtractorError(error, expected=True)
  65. raise
  66. def _real_initialize(self):
  67. self._login()
  68. def _real_extract(self, url):
  69. display_id = self._match_id(url)
  70. webpage = self._download_webpage(url, display_id)
  71. def _search_kane(name):
  72. return self._search_regex(
  73. r"KANE_customdimensions\.%s\s*=\s*'([^']+)';" % name,
  74. webpage, name, default=None)
  75. title_data = self._parse_json(self._search_regex(
  76. r'TITLE_DATA\s*=\s*({[^}]+})',
  77. webpage, 'title data', default=''),
  78. display_id, js_to_json, fatal=False) or {}
  79. video_id = title_data.get('id') or self._search_regex([
  80. r"KANE_customdimensions.videoID\s*=\s*'(\d+)';",
  81. r'<iframe[^>]+src="/player/(\d+)',
  82. ], webpage, 'video_id', default=None)
  83. if not video_id:
  84. player_url = self._html_search_meta([
  85. 'al:web:url',
  86. 'og:video:url',
  87. 'og:video:secure_url',
  88. ], webpage, fatal=True)
  89. video_id = self._search_regex(r'/player/(\d+)', player_url, 'video id')
  90. title = episode = title_data.get('title') or _search_kane('videoTitle') or self._og_search_title(webpage)
  91. series = _search_kane('showName')
  92. if series:
  93. title = '%s - %s' % (series, title)
  94. description = self._html_search_meta(['description', 'og:description'], webpage, fatal=True)
  95. try:
  96. headers = {}
  97. if self._TOKEN:
  98. headers['Authorization'] = 'Token %s' % self._TOKEN
  99. sources = self._download_json(
  100. 'https://www.funimation.com/api/showexperience/%s/' % video_id,
  101. video_id, headers=headers, query={
  102. 'pinst_id': ''.join([random.choice(string.digits + string.ascii_letters) for _ in range(8)]),
  103. })['items']
  104. except ExtractorError as e:
  105. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  106. error = self._parse_json(e.cause.read(), video_id)['errors'][0]
  107. raise ExtractorError('%s said: %s' % (
  108. self.IE_NAME, error.get('detail') or error.get('title')), expected=True)
  109. raise
  110. formats = []
  111. for source in sources:
  112. source_url = source.get('src')
  113. if not source_url:
  114. continue
  115. source_type = source.get('videoType') or determine_ext(source_url)
  116. if source_type == 'm3u8':
  117. formats.extend(self._extract_m3u8_formats(
  118. source_url, video_id, 'mp4',
  119. m3u8_id='hls', fatal=False))
  120. else:
  121. formats.append({
  122. 'format_id': source_type,
  123. 'url': source_url,
  124. })
  125. self._sort_formats(formats)
  126. return {
  127. 'id': video_id,
  128. 'display_id': display_id,
  129. 'title': title,
  130. 'description': description,
  131. 'thumbnail': self._og_search_thumbnail(webpage),
  132. 'series': series,
  133. 'season_number': int_or_none(title_data.get('seasonNum') or _search_kane('season')),
  134. 'episode_number': int_or_none(title_data.get('episodeNum')),
  135. 'episode': episode,
  136. 'season_id': title_data.get('seriesId'),
  137. 'formats': formats,
  138. }