rice.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import re
  2. from .common import InfoExtractor
  3. from ..compat import compat_parse_qs
  4. from ..utils import (
  5. xpath_text,
  6. xpath_element,
  7. int_or_none,
  8. parse_iso8601,
  9. ExtractorError,
  10. )
  11. class RICEIE(InfoExtractor):
  12. _VALID_URL = r'https?://mediahub\.rice\.edu/app/[Pp]ortal/video\.aspx\?(?P<query>.+)'
  13. _TEST = {
  14. 'url': 'https://mediahub.rice.edu/app/Portal/video.aspx?PortalID=25ffd62c-3d01-4b29-8c70-7c94270efb3e&DestinationID=66bc9434-03bd-4725-b47e-c659d8d809db&ContentID=YEWIvbhb40aqdjMD1ALSqw',
  15. 'md5': '9b83b4a2eead4912dc3b7fac7c449b6a',
  16. 'info_dict': {
  17. 'id': 'YEWIvbhb40aqdjMD1ALSqw',
  18. 'ext': 'mp4',
  19. 'title': 'Active Learning in Archeology',
  20. 'upload_date': '20140616',
  21. 'timestamp': 1402926346,
  22. }
  23. }
  24. _NS = 'http://schemas.datacontract.org/2004/07/ensembleVideo.Data.Service.Contracts.Models.Player.Config'
  25. def _real_extract(self, url):
  26. qs = compat_parse_qs(self._match_valid_url(url).group('query'))
  27. if not qs.get('PortalID') or not qs.get('DestinationID') or not qs.get('ContentID'):
  28. raise ExtractorError('Invalid URL', expected=True)
  29. portal_id = qs['PortalID'][0]
  30. playlist_id = qs['DestinationID'][0]
  31. content_id = qs['ContentID'][0]
  32. content_data = self._download_xml('https://mediahub.rice.edu/api/portal/GetContentTitle', content_id, query={
  33. 'portalId': portal_id,
  34. 'playlistId': playlist_id,
  35. 'contentId': content_id
  36. })
  37. metadata = xpath_element(content_data, './/metaData', fatal=True)
  38. title = xpath_text(metadata, 'primaryTitle', fatal=True)
  39. encodings = xpath_element(content_data, './/encodings', fatal=True)
  40. player_data = self._download_xml('https://mediahub.rice.edu/api/player/GetPlayerConfig', content_id, query={
  41. 'temporaryLinkId': xpath_text(encodings, 'temporaryLinkId', fatal=True),
  42. 'contentId': content_id,
  43. })
  44. common_fmt = {}
  45. dimensions = xpath_text(encodings, 'dimensions')
  46. if dimensions:
  47. wh = dimensions.split('x')
  48. if len(wh) == 2:
  49. common_fmt.update({
  50. 'width': int_or_none(wh[0]),
  51. 'height': int_or_none(wh[1]),
  52. })
  53. formats = []
  54. rtsp_path = xpath_text(player_data, self._xpath_ns('RtspPath', self._NS))
  55. if rtsp_path:
  56. fmt = {
  57. 'url': rtsp_path,
  58. 'format_id': 'rtsp',
  59. }
  60. fmt.update(common_fmt)
  61. formats.append(fmt)
  62. for source in player_data.findall(self._xpath_ns('.//Source', self._NS)):
  63. video_url = xpath_text(source, self._xpath_ns('File', self._NS))
  64. if not video_url:
  65. continue
  66. if '.m3u8' in video_url:
  67. formats.extend(self._extract_m3u8_formats(video_url, content_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
  68. else:
  69. fmt = {
  70. 'url': video_url,
  71. 'format_id': video_url.split(':')[0],
  72. }
  73. fmt.update(common_fmt)
  74. rtmp = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>.+))/(?P<playpath>mp4:.+)$', video_url)
  75. if rtmp:
  76. fmt.update({
  77. 'url': rtmp.group('url'),
  78. 'play_path': rtmp.group('playpath'),
  79. 'app': rtmp.group('app'),
  80. 'ext': 'flv',
  81. })
  82. formats.append(fmt)
  83. thumbnails = []
  84. for content_asset in content_data.findall('.//contentAssets'):
  85. asset_type = xpath_text(content_asset, 'type')
  86. if asset_type == 'image':
  87. image_url = xpath_text(content_asset, 'httpPath')
  88. if not image_url:
  89. continue
  90. thumbnails.append({
  91. 'id': xpath_text(content_asset, 'ID'),
  92. 'url': image_url,
  93. })
  94. return {
  95. 'id': content_id,
  96. 'title': title,
  97. 'description': xpath_text(metadata, 'abstract'),
  98. 'duration': int_or_none(xpath_text(metadata, 'duration')),
  99. 'timestamp': parse_iso8601(xpath_text(metadata, 'dateUpdated')),
  100. 'thumbnails': thumbnails,
  101. 'formats': formats,
  102. }