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