logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

konserthusetplay.py (4505B)


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