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.

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