konserthusetplay.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. determine_ext,
  4. float_or_none,
  5. int_or_none,
  6. url_or_none,
  7. )
  8. class KonserthusetPlayIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:www\.)?(?:konserthusetplay|rspoplay)\.se/\?.*\bm=(?P<id>[^&]+)'
  10. _TESTS = [{
  11. 'url': 'http://www.konserthusetplay.se/?m=CKDDnlCY-dhWAAqiMERd-A',
  12. 'md5': 'e3fd47bf44e864bd23c08e487abe1967',
  13. 'info_dict': {
  14. 'id': 'CKDDnlCY-dhWAAqiMERd-A',
  15. 'ext': 'mp4',
  16. 'title': 'Orkesterns instrument: Valthornen',
  17. 'description': 'md5:f10e1f0030202020396a4d712d2fa827',
  18. 'thumbnail': 're:^https?://.*$',
  19. 'duration': 398.76,
  20. },
  21. }, {
  22. 'url': 'http://rspoplay.se/?m=elWuEH34SMKvaO4wO_cHBw',
  23. 'only_matching': True,
  24. }]
  25. def _real_extract(self, url):
  26. video_id = self._match_id(url)
  27. webpage = self._download_webpage(url, video_id)
  28. e = self._search_regex(
  29. r'https?://csp\.picsearch\.com/rest\?.*\be=(.+?)[&"\']', webpage, 'e')
  30. rest = self._download_json(
  31. 'http://csp.picsearch.com/rest?e=%s&containerId=mediaplayer&i=object' % e,
  32. video_id, transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1])
  33. media = rest['media']
  34. player_config = media['playerconfig']
  35. playlist = player_config['playlist']
  36. source = next(f for f in playlist if f.get('bitrates') or f.get('provider'))
  37. FORMAT_ID_REGEX = r'_([^_]+)_h264m\.mp4'
  38. formats = []
  39. m3u8_url = source.get('url')
  40. if m3u8_url and determine_ext(m3u8_url) == 'm3u8':
  41. formats.extend(self._extract_m3u8_formats(
  42. m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
  43. m3u8_id='hls', fatal=False))
  44. fallback_url = source.get('fallbackUrl')
  45. fallback_format_id = None
  46. if fallback_url:
  47. fallback_format_id = self._search_regex(
  48. FORMAT_ID_REGEX, fallback_url, 'format id', default=None)
  49. connection_url = (player_config.get('rtmp', {}).get(
  50. 'netConnectionUrl') or player_config.get(
  51. 'plugins', {}).get('bwcheck', {}).get('netConnectionUrl'))
  52. if connection_url:
  53. for f in source['bitrates']:
  54. video_url = f.get('url')
  55. if not video_url:
  56. continue
  57. format_id = self._search_regex(
  58. FORMAT_ID_REGEX, video_url, 'format id', default=None)
  59. f_common = {
  60. 'vbr': int_or_none(f.get('bitrate')),
  61. 'width': int_or_none(f.get('width')),
  62. 'height': int_or_none(f.get('height')),
  63. }
  64. f = f_common.copy()
  65. f.update({
  66. 'url': connection_url,
  67. 'play_path': video_url,
  68. 'format_id': 'rtmp-%s' % format_id if format_id else 'rtmp',
  69. 'ext': 'flv',
  70. })
  71. formats.append(f)
  72. if format_id and format_id == fallback_format_id:
  73. f = f_common.copy()
  74. f.update({
  75. 'url': fallback_url,
  76. 'format_id': 'http-%s' % format_id if format_id else 'http',
  77. })
  78. formats.append(f)
  79. if not formats and fallback_url:
  80. formats.append({
  81. 'url': fallback_url,
  82. })
  83. title = player_config.get('title') or media['title']
  84. description = player_config.get('mediaInfo', {}).get('description')
  85. thumbnail = media.get('image')
  86. duration = float_or_none(media.get('duration'), 1000)
  87. subtitles = {}
  88. captions = source.get('captionsAvailableLanguages')
  89. if isinstance(captions, dict):
  90. for lang, subtitle_url in captions.items():
  91. subtitle_url = url_or_none(subtitle_url)
  92. if lang != 'none' and subtitle_url:
  93. subtitles.setdefault(lang, []).append({'url': subtitle_url})
  94. return {
  95. 'id': video_id,
  96. 'title': title,
  97. 'description': description,
  98. 'thumbnail': thumbnail,
  99. 'duration': duration,
  100. 'formats': formats,
  101. 'subtitles': subtitles,
  102. }