You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
5.4 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. clean_html,
  7. determine_ext,
  8. ExtractorError,
  9. float_or_none,
  10. get_element_by_class,
  11. get_element_by_id,
  12. parse_duration,
  13. remove_end,
  14. urlencode_postdata,
  15. urljoin,
  16. )
  17. class TeamTreeHouseIE(InfoExtractor):
  18. _VALID_URL = r'https?://(?:www\.)?teamtreehouse\.com/library/(?P<id>[^/]+)'
  19. _TESTS = [{
  20. # Course
  21. 'url': 'https://teamtreehouse.com/library/introduction-to-user-authentication-in-php',
  22. 'info_dict': {
  23. 'id': 'introduction-to-user-authentication-in-php',
  24. 'title': 'Introduction to User Authentication in PHP',
  25. 'description': 'md5:405d7b4287a159b27ddf30ca72b5b053',
  26. },
  27. 'playlist_mincount': 24,
  28. }, {
  29. # WorkShop
  30. 'url': 'https://teamtreehouse.com/library/deploying-a-react-app',
  31. 'info_dict': {
  32. 'id': 'deploying-a-react-app',
  33. 'title': 'Deploying a React App',
  34. 'description': 'md5:10a82e3ddff18c14ac13581c9b8e5921',
  35. },
  36. 'playlist_mincount': 4,
  37. }, {
  38. # Video
  39. 'url': 'https://teamtreehouse.com/library/application-overview-2',
  40. 'info_dict': {
  41. 'id': 'application-overview-2',
  42. 'ext': 'mp4',
  43. 'title': 'Application Overview',
  44. 'description': 'md5:4b0a234385c27140a4378de5f1e15127',
  45. },
  46. 'expected_warnings': ['This is just a preview'],
  47. }]
  48. _NETRC_MACHINE = 'teamtreehouse'
  49. def _real_initialize(self):
  50. email, password = self._get_login_info()
  51. if email is None:
  52. return
  53. signin_page = self._download_webpage(
  54. 'https://teamtreehouse.com/signin',
  55. None, 'Downloading signin page')
  56. data = self._form_hidden_inputs('new_user_session', signin_page)
  57. data.update({
  58. 'user_session[email]': email,
  59. 'user_session[password]': password,
  60. })
  61. error_message = get_element_by_class('error-message', self._download_webpage(
  62. 'https://teamtreehouse.com/person_session',
  63. None, 'Logging in', data=urlencode_postdata(data)))
  64. if error_message:
  65. raise ExtractorError(clean_html(error_message), expected=True)
  66. def _real_extract(self, url):
  67. display_id = self._match_id(url)
  68. webpage = self._download_webpage(url, display_id)
  69. title = self._html_search_meta(['og:title', 'twitter:title'], webpage)
  70. description = self._html_search_meta(
  71. ['description', 'og:description', 'twitter:description'], webpage)
  72. entries = self._parse_html5_media_entries(url, webpage, display_id)
  73. if entries:
  74. info = entries[0]
  75. for subtitles in info.get('subtitles', {}).values():
  76. for subtitle in subtitles:
  77. subtitle['ext'] = determine_ext(subtitle['url'], 'srt')
  78. is_preview = 'data-preview="true"' in webpage
  79. if is_preview:
  80. self.report_warning(
  81. 'This is just a preview. You need to be signed in with a Basic account to download the entire video.', display_id)
  82. duration = 30
  83. else:
  84. duration = float_or_none(self._search_regex(
  85. r'data-duration="(\d+)"', webpage, 'duration'), 1000)
  86. if not duration:
  87. duration = parse_duration(get_element_by_id(
  88. 'video-duration', webpage))
  89. info.update({
  90. 'id': display_id,
  91. 'title': title,
  92. 'description': description,
  93. 'duration': duration,
  94. })
  95. return info
  96. else:
  97. def extract_urls(html, extract_info=None):
  98. for path in re.findall(r'<a[^>]+href="([^"]+)"', html):
  99. page_url = urljoin(url, path)
  100. entry = {
  101. '_type': 'url_transparent',
  102. 'id': self._match_id(page_url),
  103. 'url': page_url,
  104. 'id_key': self.ie_key(),
  105. }
  106. if extract_info:
  107. entry.update(extract_info)
  108. entries.append(entry)
  109. workshop_videos = self._search_regex(
  110. r'(?s)<ul[^>]+id="workshop-videos"[^>]*>(.+?)</ul>',
  111. webpage, 'workshop videos', default=None)
  112. if workshop_videos:
  113. extract_urls(workshop_videos)
  114. else:
  115. stages_path = self._search_regex(
  116. r'(?s)<div[^>]+id="syllabus-stages"[^>]+data-url="([^"]+)"',
  117. webpage, 'stages path')
  118. if stages_path:
  119. stages_page = self._download_webpage(
  120. urljoin(url, stages_path), display_id, 'Downloading stages page')
  121. for chapter_number, (chapter, steps_list) in enumerate(re.findall(r'(?s)<h2[^>]*>\s*(.+?)\s*</h2>.+?<ul[^>]*>(.+?)</ul>', stages_page), 1):
  122. extract_urls(steps_list, {
  123. 'chapter': chapter,
  124. 'chapter_number': chapter_number,
  125. })
  126. title = remove_end(title, ' Course')
  127. return self.playlist_result(
  128. entries, display_id, title, description)