You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.2 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. float_or_none,
  8. )
  9. class KanalPlayIE(InfoExtractor):
  10. IE_DESC = 'Kanal 5/9/11 Play'
  11. _VALID_URL = r'https?://(?:www\.)?kanal(?P<channel_id>5|9|11)play\.se/(?:#!/)?(?:play/)?program/\d+/video/(?P<id>\d+)'
  12. _TESTS = [{
  13. 'url': 'http://www.kanal5play.se/#!/play/program/3060212363/video/3270012277',
  14. 'info_dict': {
  15. 'id': '3270012277',
  16. 'ext': 'flv',
  17. 'title': 'Saknar både dusch och avlopp',
  18. 'description': 'md5:6023a95832a06059832ae93bc3c7efb7',
  19. 'duration': 2636.36,
  20. },
  21. 'params': {
  22. # rtmp download
  23. 'skip_download': True,
  24. }
  25. }, {
  26. 'url': 'http://www.kanal9play.se/#!/play/program/335032/video/246042',
  27. 'only_matching': True,
  28. }, {
  29. 'url': 'http://www.kanal11play.se/#!/play/program/232835958/video/367135199',
  30. 'only_matching': True,
  31. }]
  32. def _fix_subtitles(self, subs):
  33. return '\r\n\r\n'.join(
  34. '%s\r\n%s --> %s\r\n%s'
  35. % (
  36. num,
  37. self._subtitles_timecode(item['startMillis'] / 1000.0),
  38. self._subtitles_timecode(item['endMillis'] / 1000.0),
  39. item['text'],
  40. ) for num, item in enumerate(subs, 1))
  41. def _get_subtitles(self, channel_id, video_id):
  42. subs = self._download_json(
  43. 'http://www.kanal%splay.se/api/subtitles/%s' % (channel_id, video_id),
  44. video_id, 'Downloading subtitles JSON', fatal=False)
  45. return {'se': [{'ext': 'srt', 'data': self._fix_subtitles(subs)}]} if subs else {}
  46. def _real_extract(self, url):
  47. mobj = re.match(self._VALID_URL, url)
  48. video_id = mobj.group('id')
  49. channel_id = mobj.group('channel_id')
  50. video = self._download_json(
  51. 'http://www.kanal%splay.se/api/getVideo?format=FLASH&videoId=%s' % (channel_id, video_id),
  52. video_id)
  53. reasons_for_no_streams = video.get('reasonsForNoStreams')
  54. if reasons_for_no_streams:
  55. raise ExtractorError(
  56. '%s returned error: %s' % (self.IE_NAME, '\n'.join(reasons_for_no_streams)),
  57. expected=True)
  58. title = video['title']
  59. description = video.get('description')
  60. duration = float_or_none(video.get('length'), 1000)
  61. thumbnail = video.get('posterUrl')
  62. stream_base_url = video['streamBaseUrl']
  63. formats = [{
  64. 'url': stream_base_url,
  65. 'play_path': stream['source'],
  66. 'ext': 'flv',
  67. 'tbr': float_or_none(stream.get('bitrate'), 1000),
  68. 'rtmp_real_time': True,
  69. } for stream in video['streams']]
  70. self._sort_formats(formats)
  71. subtitles = {}
  72. if video.get('hasSubtitle'):
  73. subtitles = self.extract_subtitles(channel_id, video_id)
  74. return {
  75. 'id': video_id,
  76. 'title': title,
  77. 'description': description,
  78. 'thumbnail': thumbnail,
  79. 'duration': duration,
  80. 'formats': formats,
  81. 'subtitles': subtitles,
  82. }