[ciscolive] Add extractor
[youtube-dl] / youtube_dl / extractor / ciscolive.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_urllib_parse_urlparse,
8     compat_parse_qs
9 )
10 from ..utils import (
11     clean_html,
12     int_or_none,
13     try_get,
14     urlencode_postdata,
15 )
16
17
18 class CiscoLiveIE(InfoExtractor):
19     IE_NAME = 'ciscolive'
20     _VALID_URL = r'(?:https?://)?ciscolive\.cisco\.com/on-demand-library/\??(?P<query>[^#]+)#/(?:session/(?P<id>.+))?$'
21     _TESTS = [
22         {
23             'url': 'https://ciscolive.cisco.com/on-demand-library/?#/session/1423353499155001FoSs',
24             'md5': 'c98acf395ed9c9f766941c70f5352e22',
25             'info_dict': {
26                 'id': '5803694304001',
27                 'ext': 'mp4',
28                 'title': '13 Smart Automations to Monitor Your Cisco IOS Network',
29                 'description': 'md5:ec4a436019e09a918dec17714803f7cc',
30                 'timestamp': 1530305395,
31                 'uploader_id': '5647924234001',
32                 'upload_date': '20180629',
33                 'location': '16B Mezz.',
34             },
35         },
36         {
37             'url': 'https://ciscolive.cisco.com/on-demand-library/?search.event=ciscoliveus2018&search.technicallevel=scpsSkillLevel_aintroductory&search.focus=scpsSessionFocus_designAndDeployment#/',
38             'md5': '993d4cf051f6174059328b1dce8e94bd',
39             'info_dict': {
40                 'upload_date': '20180629',
41                 'title': 'DevNet Panel-Applying Design Thinking to Building Products in Cisco',
42                 'timestamp': 1530316421,
43                 'uploader_id': '5647924234001',
44                 'id': '5803751616001',
45                 'description': 'md5:5f144575cd6848117fe2f756855b038b',
46                 'location': 'WoS, DevNet Theater',
47                 'ext': 'mp4',
48             },
49         },
50         {
51             'url': 'https://ciscolive.cisco.com/on-demand-library/?search.technology=scpsTechnology_applicationDevelopment&search.technology=scpsTechnology_ipv6&search.focus=scpsSessionFocus_troubleshootingTroubleshooting#/',
52             'md5': '80e0c3b87e373fe3a3316b934b8915bf',
53             'info_dict': {
54                 'upload_date': '20180629',
55                 'title': 'Beating the CCIE Routing & Switching',
56                 'timestamp': 1530311842,
57                 'uploader_id': '5647924234001',
58                 'id': '5803735679001',
59                 'description': 'md5:e71970799e92d7f5ff57ae23f64b0929',
60                 'location': 'TulĂșm 02',
61                 'ext': 'mp4',
62             },
63         }
64     ]
65
66     # These appear to be constant across all Cisco Live presentations
67     # and are not tied to any user session or event
68     RAINFOCUS_API_URL = 'https://events.rainfocus.com/api/%s'
69     RAINFOCUS_APIPROFILEID = 'Na3vqYdAlJFSxhYTYQGuMbpafMqftalz'
70     RAINFOCUS_WIDGETID = 'n6l4Lo05R8fiy3RpUBm447dZN8uNWoye'
71     BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/5647924234001/SyK2FdqjM_default/index.html?videoId=%s'
72
73     def _parse_rf_item(self, rf_item):
74         ''' Parses metadata and passes to Brightcove extractor '''
75         event_name = rf_item.get('eventName')
76         title = rf_item['title']
77         description = clean_html(rf_item.get('abstract'))
78         presenter_name = try_get(rf_item, lambda x: x['participants'][0]['fullName'])
79         bc_id = rf_item['videos'][0]['url']
80         bc_url = self.BRIGHTCOVE_URL_TEMPLATE % bc_id
81         duration = int_or_none(try_get(rf_item, lambda x: x['times'][0]['length']))
82         location = try_get(rf_item, lambda x: x['times'][0]['room'])
83
84         if duration:
85             duration = duration * 60
86
87         return {
88             '_type': 'url_transparent',
89             'creator': presenter_name,
90             'description': description,
91             'duration': duration,
92             'ie_key': 'BrightcoveNew',
93             'location': location,
94             'series': event_name,
95             'title': title,
96             'url': bc_url,
97         }
98
99     def _check_bc_id_exists(self, rf_item):
100         ''' Checks for the existence of a Brightcove URL in an API result '''
101         bc_id = try_get(rf_item, lambda x: x['videos'][0]['url'])
102         if bc_id:
103             if bc_id.strip().isdigit():
104                 return rf_item
105
106     def _real_extract(self, url):
107         mobj = re.match(self._VALID_URL, url)
108         HEADERS = {
109             'Origin': 'https://ciscolive.cisco.com',
110             'rfApiProfileId': self.RAINFOCUS_APIPROFILEID,
111             'rfWidgetId': self.RAINFOCUS_WIDGETID,
112             'Referer': url,
113         }
114         # Single session URL (single video)
115         if mobj.group('id'):
116             rf_id = mobj.group('id')
117             request = self.RAINFOCUS_API_URL % 'session'
118             data = urlencode_postdata({'id': rf_id})
119             rf_result = self._download_json(request, rf_id, data=data, headers=HEADERS)
120             rf_item = self._check_bc_id_exists(rf_result['items'][0])
121             return self._parse_rf_item(rf_item)
122         else:
123             # Filter query URL (multiple videos)
124             rf_query = compat_parse_qs((compat_urllib_parse_urlparse(url).query))
125             rf_query['type'] = 'session'
126             rf_query['size'] = 1000
127             data = urlencode_postdata(rf_query)
128             request = self.RAINFOCUS_API_URL % 'search'
129             rf_results = self._download_json(request, 'Filter query', data=data, headers=HEADERS)
130             entries = [
131                 self._parse_rf_item(rf_item)
132                 for rf_item
133                 in rf_results['sectionList'][0]['items']
134                 if self._check_bc_id_exists(rf_item)
135             ]
136             return self.playlist_result(entries, 'Filter query')