logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

hidive.py (4113B)


  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. url_or_none,
  10. urlencode_postdata,
  11. )
  12. class HiDiveIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:www\.)?hidive\.com/stream/(?P<title>[^/]+)/(?P<key>[^/?#&]+)'
  14. # Using X-Forwarded-For results in 403 HTTP error for HLS fragments,
  15. # so disabling geo bypass completely
  16. _GEO_BYPASS = False
  17. _NETRC_MACHINE = 'hidive'
  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. email, password = self._get_login_info()
  36. if email is None:
  37. return
  38. webpage = self._download_webpage(self._LOGIN_URL, None)
  39. form = self._search_regex(
  40. r'(?s)<form[^>]+action="/account/login"[^>]*>(.+?)</form>',
  41. webpage, 'login form')
  42. data = self._hidden_inputs(form)
  43. data.update({
  44. 'Email': email,
  45. 'Password': password,
  46. })
  47. self._download_webpage(
  48. self._LOGIN_URL, None, 'Logging in', data=urlencode_postdata(data))
  49. def _real_extract(self, url):
  50. mobj = re.match(self._VALID_URL, url)
  51. title, key = mobj.group('title', 'key')
  52. video_id = '%s/%s' % (title, key)
  53. settings = self._download_json(
  54. 'https://www.hidive.com/play/settings', video_id,
  55. data=urlencode_postdata({
  56. 'Title': title,
  57. 'Key': key,
  58. 'PlayerId': 'f4f895ce1ca713ba263b91caeb1daa2d08904783',
  59. }))
  60. restriction = settings.get('restrictionReason')
  61. if restriction == 'RegionRestricted':
  62. self.raise_geo_restricted()
  63. if restriction and restriction != 'None':
  64. raise ExtractorError(
  65. '%s said: %s' % (self.IE_NAME, restriction), expected=True)
  66. formats = []
  67. subtitles = {}
  68. for rendition_id, rendition in settings['renditions'].items():
  69. bitrates = rendition.get('bitrates')
  70. if not isinstance(bitrates, dict):
  71. continue
  72. m3u8_url = url_or_none(bitrates.get('hls'))
  73. if not m3u8_url:
  74. continue
  75. formats.extend(self._extract_m3u8_formats(
  76. m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
  77. m3u8_id='%s-hls' % rendition_id, fatal=False))
  78. cc_files = rendition.get('ccFiles')
  79. if not isinstance(cc_files, list):
  80. continue
  81. for cc_file in cc_files:
  82. if not isinstance(cc_file, list) or len(cc_file) < 3:
  83. continue
  84. cc_lang = cc_file[0]
  85. cc_url = url_or_none(cc_file[2])
  86. if not isinstance(cc_lang, compat_str) or not cc_url:
  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. }