itprotv.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. int_or_none,
  5. str_or_none,
  6. traverse_obj,
  7. urljoin,
  8. )
  9. class ITProTVBaseIE(InfoExtractor):
  10. _ENDPOINTS = {
  11. 'course': 'course?url={}&brand=00002560-0000-3fa9-0000-1d61000035f3',
  12. 'episode': 'brand/00002560-0000-3fa9-0000-1d61000035f3/episode?url={}',
  13. }
  14. def _call_api(self, ep, item_id, webpage):
  15. return self._download_json(
  16. f'https://api.itpro.tv/api/urza/v3/consumer-web/{self._ENDPOINTS[ep].format(item_id)}',
  17. item_id, note=f'Fetching {ep} data API',
  18. headers={'Authorization': f'Bearer {self._fetch_jwt(webpage)}'})[ep]
  19. def _fetch_jwt(self, webpage):
  20. return self._search_regex(r'{"passedToken":"([\w-]+\.[\w-]+\.[\w-]+)",', webpage, 'jwt')
  21. def _check_if_logged_in(self, webpage):
  22. if re.match(r'{\s*member\s*:\s*null', webpage):
  23. self.raise_login_required()
  24. class ITProTVIE(ITProTVBaseIE):
  25. _VALID_URL = r'https?://app\.itpro\.tv/course/(?P<course>[\w-]+)/(?P<id>[\w-]+)'
  26. _TESTS = [{
  27. 'url': 'https://app.itpro.tv/course/guided-tour/introductionitprotv',
  28. 'md5': 'bca4a28c2667fd1a63052e71a94bb88c',
  29. 'info_dict': {
  30. 'id': 'introductionitprotv',
  31. 'ext': 'mp4',
  32. 'title': 'An Introduction to ITProTV 101',
  33. 'thumbnail': 'https://itprotv-image-bucket.s3.amazonaws.com/getting-started/itprotv-101-introduction-PGM.11_39_56_02.Still001.png',
  34. 'description': 'md5:b175c2c3061ce35a4dd33865b2c1da4e',
  35. 'duration': 269,
  36. 'series': 'ITProTV 101',
  37. 'series_id': 'guided-tour',
  38. 'availability': 'needs_auth',
  39. 'chapter': 'ITProTV 101',
  40. 'chapter_number': 1,
  41. 'chapter_id': '5dbb3de426b46c0010b5d1b6',
  42. },
  43. },
  44. {
  45. 'url': 'https://app.itpro.tv/course/beyond-tech/job-interview-tips',
  46. 'md5': '101a299b98c47ccf4c67f9f0951defa8',
  47. 'info_dict': {
  48. 'id': 'job-interview-tips',
  49. 'ext': 'mp4',
  50. 'title': 'Job Interview Tips',
  51. 'thumbnail': 'https://s3.amazonaws.com:443/production-itprotv-thumbnails/2f370bf5-294d-4bbe-ab80-c0b5781630ea.png',
  52. 'description': 'md5:30d8ba483febdf89ec85623aad3c3cb6',
  53. 'duration': 267,
  54. 'series': 'Beyond Tech',
  55. 'series_id': 'beyond-tech',
  56. 'availability': 'needs_auth',
  57. 'chapter': 'Job Development',
  58. 'chapter_number': 2,
  59. 'chapter_id': '5f7c78d424330c000edf04d9',
  60. },
  61. }]
  62. def _real_extract(self, url):
  63. episode_id, course_name = self._match_valid_url(url).group('id', 'course')
  64. webpage = self._download_webpage(url, episode_id)
  65. self._check_if_logged_in(webpage)
  66. course = self._call_api('course', course_name, webpage)
  67. episode = self._call_api('episode', episode_id, webpage)
  68. chapter_number, chapter = next((
  69. (i, topic) for i, topic in enumerate(course.get('topics') or [], 1)
  70. if traverse_obj(topic, 'id') == episode.get('topic')), {})
  71. return {
  72. 'id': episode_id,
  73. 'title': episode.get('title'),
  74. 'description': episode.get('description'),
  75. 'thumbnail': episode.get('thumbnail'),
  76. 'formats': [
  77. {'url': episode[f'jwVideo{h}Embed'], 'height': h}
  78. for h in (320, 480, 720, 1080) if episode.get(f'jwVideo{h}Embed')
  79. ],
  80. 'duration': int_or_none(episode.get('length')),
  81. 'series': course.get('name'),
  82. 'series_id': course.get('url'),
  83. 'chapter': str_or_none(chapter.get('title')),
  84. 'chapter_number': chapter_number,
  85. 'chapter_id': str_or_none(chapter.get('id')),
  86. 'subtitles': {
  87. 'en': [{'ext': 'vtt', 'data': episode['enCaptionData']}],
  88. } if episode.get('enCaptionData') else None,
  89. }
  90. class ITProTVCourseIE(ITProTVBaseIE):
  91. _VALID_URL = r'https?://app\.itpro\.tv/course/(?P<id>[\w-]+)/?(?:$|[#?])'
  92. _TESTS = [
  93. {
  94. 'url': 'https://app.itpro.tv/course/guided-tour',
  95. 'info_dict': {
  96. 'id': 'guided-tour',
  97. 'description': 'md5:b175c2c3061ce35a4dd33865b2c1da4e',
  98. 'title': 'ITProTV 101',
  99. },
  100. 'playlist_count': 6,
  101. },
  102. {
  103. 'url': 'https://app.itpro.tv/course/beyond-tech',
  104. 'info_dict': {
  105. 'id': 'beyond-tech',
  106. 'description': 'md5:44cd99855e7f81a15ce1269bd0621fed',
  107. 'title': 'Beyond Tech',
  108. },
  109. 'playlist_count': 15,
  110. },
  111. ]
  112. def _real_extract(self, url):
  113. course_id = self._match_id(url)
  114. webpage = self._download_webpage(url, course_id)
  115. self._check_if_logged_in(webpage)
  116. course = self._call_api('course', course_id, webpage)
  117. entries = [self.url_result(
  118. urljoin(url, f'{course_id}/{episode["url"]}'), ITProTVIE,
  119. episode['url'], episode.get('title'), url_transparent=True)
  120. for episode in course['episodes']]
  121. return self.playlist_result(
  122. entries, course_id, course.get('name'), course.get('description'))