teachertube.py 4.2 KB

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