ciscolive.py 5.7 KB

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