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.

93 lines
3.1 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. 'md5': '',
  15. 'info_dict': {
  16. 'id': '2609989',
  17. 'ext': 'flv',
  18. 'title': 'Saknar både dusch och avlopp',
  19. 'description': 'md5:',
  20. 'duration': 2636.36,
  21. },
  22. }, {
  23. 'url': 'http://www.kanal9play.se/#!/play/program/335032/video/246042',
  24. 'only_matching': True,
  25. }, {
  26. 'url': 'http://www.kanal11play.se/#!/play/program/232835958/video/367135199',
  27. 'only_matching': True,
  28. }]
  29. def _fix_subtitles(self, subs):
  30. return '\r\n\r\n'.join(
  31. '%s\r\n%s --> %s\r\n%s'
  32. % (
  33. num,
  34. self._subtitles_timecode(item['startMillis'] / 1000.0),
  35. self._subtitles_timecode(item['endMillis'] / 1000.0),
  36. item['text'],
  37. ) for num, item in enumerate(subs, 1))
  38. def _get_subtitles(self, channel_id, video_id):
  39. subs = self._download_json(
  40. 'http://www.kanal%splay.se/api/subtitles/%s' % (channel_id, video_id),
  41. video_id, 'Downloading subtitles JSON', fatal=False)
  42. return {'se': [{'ext': 'srt', 'data': self._fix_subtitles(subs)}]} if subs else {}
  43. def _real_extract(self, url):
  44. mobj = re.match(self._VALID_URL, url)
  45. video_id = mobj.group('id')
  46. channel_id = mobj.group('channel_id')
  47. video = self._download_json(
  48. 'http://www.kanal%splay.se/api/getVideo?format=FLASH&videoId=%s' % (channel_id, video_id),
  49. video_id)
  50. reasons_for_no_streams = video.get('reasonsForNoStreams')
  51. if reasons_for_no_streams:
  52. raise ExtractorError(
  53. '%s returned error: %s' % (self.IE_NAME, '\n'.join(reasons_for_no_streams)),
  54. expected=True)
  55. title = video['title']
  56. description = video.get('description')
  57. duration = float_or_none(video.get('length'), 1000)
  58. thumbnail = video.get('posterUrl')
  59. stream_base_url = video['streamBaseUrl']
  60. formats = [{
  61. 'url': stream_base_url,
  62. 'play_path': stream['source'],
  63. 'ext': 'flv',
  64. 'tbr': float_or_none(stream.get('bitrate'), 1000),
  65. 'rtmp_real_time': True,
  66. } for stream in video['streams']]
  67. self._sort_formats(formats)
  68. subtitles = {}
  69. if video.get('hasSubtitle'):
  70. subtitles = self.extract_subtitles(channel_id, video_id)
  71. return {
  72. 'id': video_id,
  73. 'title': title,
  74. 'description': description,
  75. 'thumbnail': thumbnail,
  76. 'duration': duration,
  77. 'formats': formats,
  78. 'subtitles': subtitles,
  79. }