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