egghead.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. determine_ext,
  4. int_or_none,
  5. try_get,
  6. unified_timestamp,
  7. url_or_none,
  8. )
  9. class EggheadBaseIE(InfoExtractor):
  10. def _call_api(self, path, video_id, resource, fatal=True):
  11. return self._download_json(
  12. 'https://app.egghead.io/api/v1/' + path,
  13. video_id, f'Downloading {resource} JSON', fatal=fatal)
  14. class EggheadCourseIE(EggheadBaseIE):
  15. IE_DESC = 'egghead.io course'
  16. IE_NAME = 'egghead:course'
  17. _VALID_URL = r'https?://(?:app\.)?egghead\.io/(?:course|playlist)s/(?P<id>[^/?#&]+)'
  18. _TESTS = [{
  19. 'url': 'https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript',
  20. 'playlist_count': 29,
  21. 'info_dict': {
  22. 'id': '432655',
  23. 'title': 'Professor Frisby Introduces Composable Functional JavaScript',
  24. 'description': 're:(?s)^This course teaches the ubiquitous.*You\'ll start composing functionality before you know it.$',
  25. },
  26. }, {
  27. 'url': 'https://app.egghead.io/playlists/professor-frisby-introduces-composable-functional-javascript',
  28. 'only_matching': True,
  29. }]
  30. def _real_extract(self, url):
  31. playlist_id = self._match_id(url)
  32. series_path = 'series/' + playlist_id
  33. lessons = self._call_api(
  34. series_path + '/lessons', playlist_id, 'course lessons')
  35. entries = []
  36. for lesson in lessons:
  37. lesson_url = url_or_none(lesson.get('http_url'))
  38. if not lesson_url:
  39. continue
  40. lesson_id = lesson.get('id')
  41. if lesson_id:
  42. lesson_id = str(lesson_id)
  43. entries.append(self.url_result(
  44. lesson_url, ie=EggheadLessonIE.ie_key(), video_id=lesson_id))
  45. course = self._call_api(
  46. series_path, playlist_id, 'course', False) or {}
  47. playlist_id = course.get('id')
  48. if playlist_id:
  49. playlist_id = str(playlist_id)
  50. return self.playlist_result(
  51. entries, playlist_id, course.get('title'),
  52. course.get('description'))
  53. class EggheadLessonIE(EggheadBaseIE):
  54. IE_DESC = 'egghead.io lesson'
  55. IE_NAME = 'egghead:lesson'
  56. _VALID_URL = r'https?://(?:app\.)?egghead\.io/(?:api/v1/)?lessons/(?P<id>[^/?#&]+)'
  57. _TESTS = [{
  58. 'url': 'https://egghead.io/lessons/javascript-linear-data-flow-with-container-style-types-box',
  59. 'info_dict': {
  60. 'id': '1196',
  61. 'display_id': 'javascript-linear-data-flow-with-container-style-types-box',
  62. 'ext': 'mp4',
  63. 'title': 'Create linear data flow with container style types (Box)',
  64. 'description': 'md5:9aa2cdb6f9878ed4c39ec09e85a8150e',
  65. 'thumbnail': r're:^https?:.*\.jpg$',
  66. 'timestamp': 1481296768,
  67. 'upload_date': '20161209',
  68. 'duration': 304,
  69. 'view_count': 0,
  70. 'tags': 'count:2',
  71. },
  72. 'params': {
  73. 'skip_download': True,
  74. },
  75. }, {
  76. 'url': 'https://egghead.io/api/v1/lessons/react-add-redux-to-a-react-application',
  77. 'only_matching': True,
  78. }, {
  79. 'url': 'https://app.egghead.io/lessons/javascript-linear-data-flow-with-container-style-types-box',
  80. 'only_matching': True,
  81. }]
  82. def _real_extract(self, url):
  83. display_id = self._match_id(url)
  84. lesson = self._call_api(
  85. 'lessons/' + display_id, display_id, 'lesson')
  86. lesson_id = str(lesson['id'])
  87. title = lesson['title']
  88. formats = []
  89. for _, format_url in lesson['media_urls'].items():
  90. format_url = url_or_none(format_url)
  91. if not format_url:
  92. continue
  93. ext = determine_ext(format_url)
  94. if ext == 'm3u8':
  95. formats.extend(self._extract_m3u8_formats(
  96. format_url, lesson_id, 'mp4', m3u8_id='hls', fatal=False))
  97. elif ext == 'mpd':
  98. formats.extend(self._extract_mpd_formats(
  99. format_url, lesson_id, mpd_id='dash', fatal=False))
  100. else:
  101. formats.append({
  102. 'url': format_url,
  103. })
  104. return {
  105. 'id': lesson_id,
  106. 'display_id': display_id,
  107. 'title': title,
  108. 'description': lesson.get('summary'),
  109. 'thumbnail': lesson.get('thumb_nail'),
  110. 'timestamp': unified_timestamp(lesson.get('published_at')),
  111. 'duration': int_or_none(lesson.get('duration')),
  112. 'view_count': int_or_none(lesson.get('plays_count')),
  113. 'tags': try_get(lesson, lambda x: x['tag_list'], list),
  114. 'series': try_get(
  115. lesson, lambda x: x['series']['title'], str),
  116. 'formats': formats,
  117. }