changed to use .get to get field from json object
[youtube-dl] / youtube_dl / extractor / collegerama.py
1 from __future__ import unicode_literals
2
3 import json
4
5 from .common import InfoExtractor
6 from ..utils import (
7     float_or_none,
8     int_or_none,
9     sanitized_Request,
10 )
11
12
13 class CollegeRamaIE(InfoExtractor):
14     _VALID_URL = r'https?://collegerama\.tudelft\.nl/Mediasite/Play/(?P<id>[\da-f]+)'
15     _TESTS = [
16         {
17             'url': 'https://collegerama.tudelft.nl/Mediasite/Play/585a43626e544bdd97aeb71a0ec907a01d',
18             'md5': '481fda1c11f67588c0d9d8fbdced4e39',
19             'info_dict': {
20                 'id': '585a43626e544bdd97aeb71a0ec907a01d',
21                 'ext': 'mp4',
22                 'title': 'Een nieuwe wereld: waarden, bewustzijn en techniek van de mensheid 2.0.',
23                 'description': '',
24                 'thumbnail': r're:^https?://.*\.jpg(?:\?.*?)?$',
25                 'duration': 7713.088,
26                 'timestamp': 1413309600,
27                 'upload_date': '20141014',
28             },
29         },
30         {
31             'url': 'https://collegerama.tudelft.nl/Mediasite/Play/86a9ea9f53e149079fbdb4202b521ed21d?catalog=fd32fd35-6c99-466c-89d4-cd3c431bc8a4',
32             'md5': 'ef1fdded95bdf19b12c5999949419c92',
33             'info_dict': {
34                 'id': '86a9ea9f53e149079fbdb4202b521ed21d',
35                 'ext': 'wmv',
36                 'title': '64ste Vakantiecursus: Afvalwater',
37                 'description': 'md5:7fd774865cc69d972f542b157c328305',
38                 'thumbnail': r're:^https?://.*\.jpg(?:\?.*?)?$',
39                 'duration': 10853,
40                 'timestamp': 1326446400,
41                 'upload_date': '20120113',
42             },
43         },
44     ]
45
46     def _real_extract(self, url):
47         video_id = self._match_id(url)
48
49         player_options_request = {
50             'getPlayerOptionsRequest': {
51                 'ResourceId': video_id,
52                 'QueryString': '',
53             }
54         }
55
56         request = sanitized_Request(
57             'http://collegerama.tudelft.nl/Mediasite/PlayerService/PlayerService.svc/json/GetPlayerOptions',
58             json.dumps(player_options_request))
59         request.add_header('Content-Type', 'application/json')
60
61         player_options = self._download_json(request, video_id)
62
63         presentation = player_options['d']['Presentation']
64         title = presentation['Title']
65         description = presentation.get('Description')
66         thumbnail = None
67         duration = float_or_none(presentation.get('Duration'), 1000)
68         timestamp = int_or_none(presentation.get('UnixTime'), 1000)
69
70         formats = []
71         for stream in presentation['Streams']:
72             for video in stream['VideoUrls']:
73                 thumbnail_url = stream.get('ThumbnailUrl')
74                 if thumbnail_url:
75                     thumbnail = 'http://collegerama.tudelft.nl' + thumbnail_url
76                 format_id = video['MediaType']
77                 if format_id == 'SS':
78                     continue
79                 formats.append({
80                     'url': video['Location'],
81                     'format_id': format_id,
82                 })
83         self._sort_formats(formats)
84
85         return {
86             'id': video_id,
87             'title': title,
88             'description': description,
89             'thumbnail': thumbnail,
90             'duration': duration,
91             'timestamp': timestamp,
92             'formats': formats,
93         }