egghead.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. determine_ext,
  7. int_or_none,
  8. try_get,
  9. unified_timestamp,
  10. url_or_none,
  11. )
  12. class EggheadBaseIE(InfoExtractor):
  13. def _call_api(self, path, video_id, resource, fatal=True):
  14. return self._download_json(
  15. 'https://app.egghead.io/api/v1/' + path,
  16. video_id, 'Downloading %s JSON' % resource)
  17. class EggheadCourseIE(EggheadBaseIE):
  18. IE_DESC = 'egghead.io course'
  19. IE_NAME = 'egghead:course'
  20. _VALID_URL = r'https://egghead\.io/courses/(?P<id>[^/?#&]+)'
  21. _TEST = {
  22. 'url': 'https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript',
  23. 'playlist_count': 29,
  24. 'info_dict': {
  25. 'id': '72',
  26. 'title': 'Professor Frisby Introduces Composable Functional JavaScript',
  27. 'description': 're:(?s)^This course teaches the ubiquitous.*You\'ll start composing functionality before you know it.$',
  28. },
  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 = compat_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 = compat_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://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': ['free', 'javascript'],
  71. },
  72. 'params': {
  73. 'skip_download': True,
  74. 'format': 'bestvideo',
  75. },
  76. }, {
  77. 'url': 'https://egghead.io/api/v1/lessons/react-add-redux-to-a-react-application',
  78. 'only_matching': True,
  79. }]
  80. def _real_extract(self, url):
  81. display_id = self._match_id(url)
  82. lesson = self._call_api(
  83. 'lessons/' + display_id, display_id, 'lesson')
  84. lesson_id = compat_str(lesson['id'])
  85. title = lesson['title']
  86. formats = []
  87. for _, format_url in lesson['media_urls'].items():
  88. format_url = url_or_none(format_url)
  89. if not format_url:
  90. continue
  91. ext = determine_ext(format_url)
  92. if ext == 'm3u8':
  93. formats.extend(self._extract_m3u8_formats(
  94. format_url, lesson_id, 'mp4', entry_protocol='m3u8',
  95. m3u8_id='hls', fatal=False))
  96. elif ext == 'mpd':
  97. formats.extend(self._extract_mpd_formats(
  98. format_url, lesson_id, mpd_id='dash', fatal=False))
  99. else:
  100. formats.append({
  101. 'url': format_url,
  102. })
  103. self._sort_formats(formats)
  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'], compat_str),
  116. 'formats': formats,
  117. }