viidea.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import re
  2. import urllib.parse
  3. from .common import InfoExtractor
  4. from ..networking.exceptions import HTTPError
  5. from ..utils import (
  6. ExtractorError,
  7. js_to_json,
  8. parse_duration,
  9. parse_iso8601,
  10. )
  11. class ViideaIE(InfoExtractor):
  12. _VALID_URL = r'''(?x)https?://(?:www\.)?(?:
  13. videolectures\.net|
  14. flexilearn\.viidea\.net|
  15. presentations\.ocwconsortium\.org|
  16. video\.travel-zoom\.si|
  17. video\.pomp-forum\.si|
  18. tv\.nil\.si|
  19. video\.hekovnik.com|
  20. video\.szko\.si|
  21. kpk\.viidea\.com|
  22. inside\.viidea\.net|
  23. video\.kiberpipa\.org|
  24. bvvideo\.si|
  25. kongres\.viidea\.net|
  26. edemokracija\.viidea\.com
  27. )(?:/lecture)?/(?P<id>[^/]+)(?:/video/(?P<part>\d+))?/*(?:[#?].*)?$'''
  28. _TESTS = [{
  29. 'url': 'http://videolectures.net/promogram_igor_mekjavic_eng/',
  30. 'info_dict': {
  31. 'id': '20171',
  32. 'display_id': 'promogram_igor_mekjavic_eng',
  33. 'ext': 'mp4',
  34. 'title': 'Automatics, robotics and biocybernetics',
  35. 'description': 'md5:815fc1deb6b3a2bff99de2d5325be482',
  36. 'thumbnail': r're:http://.*\.jpg',
  37. 'timestamp': 1372349289,
  38. 'upload_date': '20130627',
  39. 'duration': 565,
  40. },
  41. 'params': {
  42. # m3u8 download
  43. 'skip_download': True,
  44. },
  45. }, {
  46. # video with invalid direct format links (HTTP 403)
  47. 'url': 'http://videolectures.net/russir2010_filippova_nlp/',
  48. 'info_dict': {
  49. 'id': '14891',
  50. 'display_id': 'russir2010_filippova_nlp',
  51. 'ext': 'flv',
  52. 'title': 'NLP at Google',
  53. 'description': 'md5:fc7a6d9bf0302d7cc0e53f7ca23747b3',
  54. 'thumbnail': r're:http://.*\.jpg',
  55. 'timestamp': 1284375600,
  56. 'upload_date': '20100913',
  57. 'duration': 5352,
  58. },
  59. 'params': {
  60. # rtmp download
  61. 'skip_download': True,
  62. },
  63. }, {
  64. # event playlist
  65. 'url': 'http://videolectures.net/deeplearning2015_montreal/',
  66. 'info_dict': {
  67. 'id': '23181',
  68. 'title': 'Deep Learning Summer School, Montreal 2015',
  69. 'description': 'md5:0533a85e4bd918df52a01f0e1ebe87b7',
  70. 'thumbnail': r're:http://.*\.jpg',
  71. 'timestamp': 1438560000,
  72. },
  73. 'playlist_count': 30,
  74. }, {
  75. # multi part lecture
  76. 'url': 'http://videolectures.net/mlss09uk_bishop_ibi/',
  77. 'info_dict': {
  78. 'id': '9737',
  79. 'display_id': 'mlss09uk_bishop_ibi',
  80. 'title': 'Introduction To Bayesian Inference',
  81. 'thumbnail': r're:http://.*\.jpg',
  82. 'timestamp': 1251622800,
  83. },
  84. 'playlist': [{
  85. 'info_dict': {
  86. 'id': '9737_part1',
  87. 'display_id': 'mlss09uk_bishop_ibi_part1',
  88. 'ext': 'wmv',
  89. 'title': 'Introduction To Bayesian Inference (Part 1)',
  90. 'thumbnail': r're:http://.*\.jpg',
  91. 'duration': 4622,
  92. 'timestamp': 1251622800,
  93. 'upload_date': '20090830',
  94. },
  95. }, {
  96. 'info_dict': {
  97. 'id': '9737_part2',
  98. 'display_id': 'mlss09uk_bishop_ibi_part2',
  99. 'ext': 'wmv',
  100. 'title': 'Introduction To Bayesian Inference (Part 2)',
  101. 'thumbnail': r're:http://.*\.jpg',
  102. 'duration': 5641,
  103. 'timestamp': 1251622800,
  104. 'upload_date': '20090830',
  105. },
  106. }],
  107. 'playlist_count': 2,
  108. }]
  109. def _real_extract(self, url):
  110. lecture_slug, explicit_part_id = self._match_valid_url(url).groups()
  111. webpage = self._download_webpage(url, lecture_slug)
  112. cfg = self._parse_json(self._search_regex(
  113. [r'cfg\s*:\s*({.+?})\s*,\s*[\da-zA-Z_]+\s*:\s*\(?\s*function',
  114. r'cfg\s*:\s*({[^}]+})'],
  115. webpage, 'cfg'), lecture_slug, js_to_json)
  116. lecture_id = str(cfg['obj_id'])
  117. base_url = self._proto_relative_url(cfg['livepipe'], 'http:')
  118. try:
  119. lecture_data = self._download_json(
  120. f'{base_url}/site/api/lecture/{lecture_id}?format=json',
  121. lecture_id)['lecture'][0]
  122. except ExtractorError as e:
  123. if isinstance(e.cause, HTTPError) and e.cause.status == 403:
  124. msg = self._parse_json(
  125. e.cause.response.read().decode('utf-8'), lecture_id)
  126. raise ExtractorError(msg['detail'], expected=True)
  127. raise
  128. lecture_info = {
  129. 'id': lecture_id,
  130. 'display_id': lecture_slug,
  131. 'title': lecture_data['title'],
  132. 'timestamp': parse_iso8601(lecture_data.get('time')),
  133. 'description': lecture_data.get('description_wiki'),
  134. 'thumbnail': lecture_data.get('thumb'),
  135. }
  136. playlist_entries = []
  137. lecture_type = lecture_data.get('type')
  138. parts = [str(video) for video in cfg.get('videos', [])]
  139. if parts:
  140. multipart = len(parts) > 1
  141. def extract_part(part_id):
  142. smil_url = f'{base_url}/{lecture_slug}/video/{part_id}/smil.xml'
  143. smil = self._download_smil(smil_url, lecture_id)
  144. info = self._parse_smil(smil, smil_url, lecture_id)
  145. info['id'] = lecture_id if not multipart else f'{lecture_id}_part{part_id}'
  146. info['display_id'] = lecture_slug if not multipart else f'{lecture_slug}_part{part_id}'
  147. if multipart:
  148. info['title'] += f' (Part {part_id})'
  149. switch = smil.find('.//switch')
  150. if switch is not None:
  151. info['duration'] = parse_duration(switch.attrib.get('dur'))
  152. item_info = lecture_info.copy()
  153. item_info.update(info)
  154. return item_info
  155. if explicit_part_id or not multipart:
  156. result = extract_part(explicit_part_id or parts[0])
  157. else:
  158. result = {
  159. '_type': 'multi_video',
  160. 'entries': [extract_part(part) for part in parts],
  161. }
  162. result.update(lecture_info)
  163. # Immediately return explicitly requested part or non event item
  164. if explicit_part_id or lecture_type != 'evt':
  165. return result
  166. playlist_entries.append(result)
  167. # It's probably a playlist
  168. if not parts or lecture_type == 'evt':
  169. playlist_webpage = self._download_webpage(
  170. f'{base_url}/site/ajax/drilldown/?id={lecture_id}', lecture_id)
  171. entries = [
  172. self.url_result(urllib.parse.urljoin(url, video_url), 'Viidea')
  173. for _, video_url in re.findall(
  174. r'<a[^>]+href=(["\'])(.+?)\1[^>]+id=["\']lec=\d+', playlist_webpage)]
  175. playlist_entries.extend(entries)
  176. playlist = self.playlist_result(playlist_entries, lecture_id)
  177. playlist.update(lecture_info)
  178. return playlist