changed to use .get to get field from json object
[youtube-dl] / youtube_dl / extractor / sandia.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import (
8     int_or_none,
9     mimetype2ext,
10 )
11
12
13 class SandiaIE(InfoExtractor):
14     IE_DESC = 'Sandia National Laboratories'
15     _VALID_URL = r'https?://digitalops\.sandia\.gov/Mediasite/Play/(?P<id>[0-9a-f]+)'
16     _TEST = {
17         'url': 'http://digitalops.sandia.gov/Mediasite/Play/24aace4429fc450fb5b38cdbf424a66e1d',
18         'md5': '9422edc9b9a60151727e4b6d8bef393d',
19         'info_dict': {
20             'id': '24aace4429fc450fb5b38cdbf424a66e1d',
21             'ext': 'mp4',
22             'title': 'Xyce Software Training - Section 1',
23             'description': 're:(?s)SAND Number: SAND 2013-7800.{200,}',
24             'upload_date': '20120409',
25             'timestamp': 1333983600,
26             'duration': 7794,
27         }
28     }
29
30     def _real_extract(self, url):
31         video_id = self._match_id(url)
32
33         presentation_data = self._download_json(
34             'http://digitalops.sandia.gov/Mediasite/PlayerService/PlayerService.svc/json/GetPlayerOptions',
35             video_id, data=json.dumps({
36                 'getPlayerOptionsRequest': {
37                     'ResourceId': video_id,
38                     'QueryString': '',
39                 }
40             }), headers={
41                 'Content-Type': 'application/json; charset=utf-8',
42             })['d']['Presentation']
43
44         title = presentation_data['Title']
45
46         formats = []
47         for stream in presentation_data.get('Streams', []):
48             for fd in stream.get('VideoUrls', []):
49                 formats.append({
50                     'format_id': fd['MediaType'],
51                     'format_note': fd['MimeType'].partition('/')[2],
52                     'ext': mimetype2ext(fd['MimeType']),
53                     'url': fd['Location'],
54                     'protocol': 'f4m' if fd['MimeType'] == 'video/x-mp4-fragmented' else None,
55                 })
56         self._sort_formats(formats)
57
58         return {
59             'id': video_id,
60             'title': title,
61             'description': presentation_data.get('Description'),
62             'formats': formats,
63             'timestamp': int_or_none(presentation_data.get('UnixTime'), 1000),
64             'duration': int_or_none(presentation_data.get('Duration'), 1000),
65         }