teamtreehouse.py 5.2 KB

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