hidive.py 4.0 KB

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