teachertube.py 4.2 KB

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