hidive.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. ExtractorError,
  8. int_or_none,
  9. urlencode_postdata,
  10. )
  11. class HiDiveIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?hidive\.com/stream/(?P<title>[^/]+)/(?P<key>[^/?#&]+)'
  13. # Using X-Forwarded-For results in 403 HTTP error for HLS fragments,
  14. # so disabling geo bypass completely
  15. _GEO_BYPASS = False
  16. _NETRC_MACHINE = 'hidive'
  17. _LOGGED_IN = False
  18. _LOGIN_URL = 'https://www.hidive.com/account/login'
  19. _TESTS = [{
  20. 'url': 'https://www.hidive.com/stream/the-comic-artist-and-his-assistants/s01e001',
  21. 'info_dict': {
  22. 'id': 'the-comic-artist-and-his-assistants/s01e001',
  23. 'ext': 'mp4',
  24. 'title': 'the-comic-artist-and-his-assistants/s01e001',
  25. 'series': 'the-comic-artist-and-his-assistants',
  26. 'season_number': 1,
  27. 'episode_number': 1,
  28. },
  29. 'params': {
  30. 'skip_download': True,
  31. },
  32. 'skip': 'Requires Authentication',
  33. }]
  34. def _real_initialize(self):
  35. if self._LOGGED_IN:
  36. return
  37. (email, password) = self._get_login_info()
  38. if email is None:
  39. return
  40. webpage = self._download_webpage(self._LOGIN_URL, None)
  41. form = self._search_regex(
  42. r'(?s)<form[^>]+action="/account/login"[^>]*>(.+?)</form>',
  43. webpage, 'login form')
  44. data = self._hidden_inputs(form)
  45. data.update({
  46. 'Email': email,
  47. 'Password': password,
  48. })
  49. self._download_webpage(
  50. self._LOGIN_URL, None, 'Logging in', data=urlencode_postdata(data))
  51. self._LOGGED_IN = True
  52. def _real_extract(self, url):
  53. mobj = re.match(self._VALID_URL, url)
  54. title, key = mobj.group('title', 'key')
  55. video_id = '%s/%s' % (title, key)
  56. settings = self._download_json(
  57. 'https://www.hidive.com/play/settings', video_id,
  58. data=urlencode_postdata({
  59. 'Title': title,
  60. 'Key': key,
  61. 'PlayerId': 'f4f895ce1ca713ba263b91caeb1daa2d08904783',
  62. }))
  63. restriction = settings.get('restrictionReason')
  64. if restriction == 'RegionRestricted':
  65. self.raise_geo_restricted()
  66. if restriction and restriction != 'None':
  67. raise ExtractorError(
  68. '%s said: %s' % (self.IE_NAME, restriction), expected=True)
  69. formats = []
  70. subtitles = {}
  71. for rendition_id, rendition in settings['renditions'].items():
  72. bitrates = rendition.get('bitrates')
  73. if not isinstance(bitrates, dict):
  74. continue
  75. m3u8_url = bitrates.get('hls')
  76. if not isinstance(m3u8_url, compat_str):
  77. continue
  78. formats.extend(self._extract_m3u8_formats(
  79. m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
  80. m3u8_id='%s-hls' % rendition_id, fatal=False))
  81. cc_files = rendition.get('ccFiles')
  82. if not isinstance(cc_files, list):
  83. continue
  84. for cc_file in cc_files:
  85. if not isinstance(cc_file, list) or len(cc_file) < 3:
  86. continue
  87. cc_lang = cc_file[0]
  88. cc_url = cc_file[2]
  89. if not isinstance(cc_lang, compat_str) or not isinstance(
  90. cc_url, compat_str):
  91. continue
  92. subtitles.setdefault(cc_lang, []).append({
  93. 'url': cc_url,
  94. })
  95. self._sort_formats(formats)
  96. season_number = int_or_none(self._search_regex(
  97. r's(\d+)', key, 'season number', default=None))
  98. episode_number = int_or_none(self._search_regex(
  99. r'e(\d+)', key, 'episode number', default=None))
  100. return {
  101. 'id': video_id,
  102. 'title': video_id,
  103. 'subtitles': subtitles,
  104. 'formats': formats,
  105. 'series': title,
  106. 'season_number': season_number,
  107. 'episode_number': episode_number,
  108. }