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