ciscolive.py 5.3 KB

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