[ciscolive] Fix issues and improve extraction (closes #17984)
[youtube-dl] / youtube_dl / extractor / ciscolive.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import (
6     compat_parse_qs,
7     compat_urllib_parse_urlparse,
8 )
9 from ..utils import (
10     clean_html,
11     float_or_none,
12     int_or_none,
13     try_get,
14     urlencode_postdata,
15 )
16
17
18 class CiscoLiveBaseIE(InfoExtractor):
19     # These appear to be constant across all Cisco Live presentations
20     # and are not tied to any user session or event
21     RAINFOCUS_API_URL = 'https://events.rainfocus.com/api/%s'
22     RAINFOCUS_API_PROFILE_ID = 'Na3vqYdAlJFSxhYTYQGuMbpafMqftalz'
23     RAINFOCUS_WIDGET_ID = 'n6l4Lo05R8fiy3RpUBm447dZN8uNWoye'
24     BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/5647924234001/SyK2FdqjM_default/index.html?videoId=%s'
25
26     HEADERS = {
27         'Origin': 'https://ciscolive.cisco.com',
28         'rfApiProfileId': RAINFOCUS_API_PROFILE_ID,
29         'rfWidgetId': RAINFOCUS_WIDGET_ID,
30     }
31
32     def _call_api(self, ep, rf_id, query, referrer):
33         headers = self.HEADERS.copy()
34         headers['Referer'] = referrer
35         return self._download_json(
36             self.RAINFOCUS_API_URL % ep, rf_id, data=urlencode_postdata(query),
37             headers=headers)
38
39     def _parse_rf_item(self, rf_item):
40         event_name = rf_item.get('eventName')
41         title = rf_item['title']
42         description = clean_html(rf_item.get('abstract'))
43         presenter_name = try_get(rf_item, lambda x: x['participants'][0]['fullName'])
44         bc_id = rf_item['videos'][0]['url']
45         bc_url = self.BRIGHTCOVE_URL_TEMPLATE % bc_id
46         duration = float_or_none(try_get(rf_item, lambda x: x['times'][0]['length']))
47         location = try_get(rf_item, lambda x: x['times'][0]['room'])
48
49         if duration:
50             duration = duration * 60
51
52         return {
53             '_type': 'url_transparent',
54             'url': bc_url,
55             'ie_key': 'BrightcoveNew',
56             'title': title,
57             'description': description,
58             'duration': duration,
59             'creator': presenter_name,
60             'location': location,
61             'series': event_name,
62         }
63
64
65 class CiscoLiveSessionIE(CiscoLiveBaseIE):
66     _VALID_URL = r'https?://ciscolive\.cisco\.com/on-demand-library/\??[^#]*#/session/(?P<id>[^/?&]+)'
67     _TEST = {
68         'url': 'https://ciscolive.cisco.com/on-demand-library/?#/session/1423353499155001FoSs',
69         'md5': 'c98acf395ed9c9f766941c70f5352e22',
70         'info_dict': {
71             'id': '5803694304001',
72             'ext': 'mp4',
73             'title': '13 Smart Automations to Monitor Your Cisco IOS Network',
74             'description': 'md5:ec4a436019e09a918dec17714803f7cc',
75             'timestamp': 1530305395,
76             'upload_date': '20180629',
77             'uploader_id': '5647924234001',
78             'location': '16B Mezz.',
79         },
80         'params': {
81             'proxy': '127.0.0.1:8118',
82         }
83     }
84
85     def _real_extract(self, url):
86         rf_id = self._match_id(url)
87         rf_result = self._call_api('session', rf_id, {'id': rf_id}, url)
88         return self._parse_rf_item(rf_result['items'][0])
89
90
91 class CiscoLiveSearchIE(CiscoLiveBaseIE):
92     _VALID_URL = r'https?://ciscolive\.cisco\.com/on-demand-library/'
93     _TESTS = [{
94         'url': 'https://ciscolive.cisco.com/on-demand-library/?search.event=ciscoliveus2018&search.technicallevel=scpsSkillLevel_aintroductory&search.focus=scpsSessionFocus_designAndDeployment#/',
95         'info_dict': {
96             'title': 'Filter query',
97         },
98         'playlist_count': 5,
99         'params': {
100             'proxy': '127.0.0.1:8118',
101         }
102     }, {
103         'url': 'https://ciscolive.cisco.com/on-demand-library/?search.technology=scpsTechnology_applicationDevelopment&search.technology=scpsTechnology_ipv6&search.focus=scpsSessionFocus_troubleshootingTroubleshooting#/',
104         'only_matching': True,
105     }]
106
107     @classmethod
108     def suitable(cls, url):
109         return False if CiscoLiveSessionIE.suitable(url) else super(CiscoLiveSearchIE, cls).suitable(url)
110
111     @staticmethod
112     def _check_bc_id_exists(rf_item):
113         return int_or_none(try_get(rf_item, lambda x: x['videos'][0]['url'])) is not None
114
115     def _real_extract(self, url):
116         rf_query = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
117         rf_query['type'] = 'session'
118         rf_query['size'] = 1000
119         rf_results = self._call_api('search', None, rf_query, url)
120         entries = [
121             self._parse_rf_item(rf_item)
122             for rf_item
123             in rf_results['sectionList'][0]['items']
124             if self._check_bc_id_exists(rf_item)
125         ]
126         return self.playlist_result(entries, playlist_title='Filter query')