logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

teachertube.py (4417B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. ExtractorError,
  8. qualities,
  9. )
  10. class TeacherTubeIE(InfoExtractor):
  11. IE_NAME = 'teachertube'
  12. IE_DESC = 'teachertube.com videos'
  13. _VALID_URL = r'https?://(?:www\.)?teachertube\.com/(viewVideo\.php\?video_id=|music\.php\?music_id=|video/(?:[\da-z-]+-)?|audio/)(?P<id>\d+)'
  14. _TESTS = [{
  15. # flowplayer
  16. 'url': 'http://www.teachertube.com/viewVideo.php?video_id=339997',
  17. 'md5': 'f9434ef992fd65936d72999951ee254c',
  18. 'info_dict': {
  19. 'id': '339997',
  20. 'ext': 'mp4',
  21. 'title': 'Measures of dispersion from a frequency table',
  22. 'description': 'Measures of dispersion from a frequency table',
  23. 'thumbnail': r're:https?://.*\.(?:jpg|png)',
  24. },
  25. }, {
  26. # jwplayer
  27. 'url': 'http://www.teachertube.com/music.php?music_id=8805',
  28. 'md5': '01e8352006c65757caf7b961f6050e21',
  29. 'info_dict': {
  30. 'id': '8805',
  31. 'ext': 'mp3',
  32. 'title': 'PER ASPERA AD ASTRA',
  33. 'description': 'RADIJSKA EMISIJA ZRAKOPLOVNE TEHNI?KE ?KOLE P',
  34. },
  35. }, {
  36. # unavailable video
  37. 'url': 'http://www.teachertube.com/video/intro-video-schleicher-297790',
  38. 'only_matching': True,
  39. }]
  40. def _real_extract(self, url):
  41. video_id = self._match_id(url)
  42. webpage = self._download_webpage(url, video_id)
  43. error = self._search_regex(
  44. r'<div\b[^>]+\bclass=["\']msgBox error[^>]+>([^<]+)', webpage,
  45. 'error', default=None)
  46. if error:
  47. raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True)
  48. title = self._html_search_meta('title', webpage, 'title', fatal=True)
  49. TITLE_SUFFIX = ' - TeacherTube'
  50. if title.endswith(TITLE_SUFFIX):
  51. title = title[:-len(TITLE_SUFFIX)].strip()
  52. description = self._html_search_meta('description', webpage, 'description')
  53. if description:
  54. description = description.strip()
  55. quality = qualities(['mp3', 'flv', 'mp4'])
  56. media_urls = re.findall(r'data-contenturl="([^"]+)"', webpage)
  57. media_urls.extend(re.findall(r'var\s+filePath\s*=\s*"([^"]+)"', webpage))
  58. media_urls.extend(re.findall(r'\'file\'\s*:\s*["\']([^"\']+)["\'],', webpage))
  59. formats = [
  60. {
  61. 'url': media_url,
  62. 'quality': quality(determine_ext(media_url))
  63. } for media_url in set(media_urls)
  64. ]
  65. self._sort_formats(formats)
  66. thumbnail = self._og_search_thumbnail(
  67. webpage, default=None) or self._html_search_meta(
  68. 'thumbnail', webpage)
  69. return {
  70. 'id': video_id,
  71. 'title': title,
  72. 'description': description,
  73. 'thumbnail': thumbnail,
  74. 'formats': formats,
  75. }
  76. class TeacherTubeUserIE(InfoExtractor):
  77. IE_NAME = 'teachertube:user:collection'
  78. IE_DESC = 'teachertube.com user and collection videos'
  79. _VALID_URL = r'https?://(?:www\.)?teachertube\.com/(user/profile|collection)/(?P<user>[0-9a-zA-Z]+)/?'
  80. _MEDIA_RE = r'''(?sx)
  81. class="?sidebar_thumb_time"?>[0-9:]+</div>
  82. \s*
  83. <a\s+href="(https?://(?:www\.)?teachertube\.com/(?:video|audio)/[^"]+)"
  84. '''
  85. _TEST = {
  86. 'url': 'http://www.teachertube.com/user/profile/rbhagwati2',
  87. 'info_dict': {
  88. 'id': 'rbhagwati2'
  89. },
  90. 'playlist_mincount': 179,
  91. }
  92. def _real_extract(self, url):
  93. mobj = re.match(self._VALID_URL, url)
  94. user_id = mobj.group('user')
  95. urls = []
  96. webpage = self._download_webpage(url, user_id)
  97. urls.extend(re.findall(self._MEDIA_RE, webpage))
  98. pages = re.findall(r'/ajax-user/user-videos/%s\?page=([0-9]+)' % user_id, webpage)[:-1]
  99. for p in pages:
  100. more = 'http://www.teachertube.com/ajax-user/user-videos/%s?page=%s' % (user_id, p)
  101. webpage = self._download_webpage(more, user_id, 'Downloading page %s/%s' % (p, len(pages)))
  102. video_urls = re.findall(self._MEDIA_RE, webpage)
  103. urls.extend(video_urls)
  104. entries = [self.url_result(vurl, 'TeacherTube') for vurl in urls]
  105. return self.playlist_result(entries, user_id)