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