flextv.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from .common import InfoExtractor
  2. from ..networking.exceptions import HTTPError
  3. from ..utils import (
  4. ExtractorError,
  5. UserNotLive,
  6. parse_iso8601,
  7. str_or_none,
  8. traverse_obj,
  9. url_or_none,
  10. )
  11. class FlexTVIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?flextv\.co\.kr/channels/(?P<id>\d+)/live'
  13. _TESTS = [{
  14. 'url': 'https://www.flextv.co.kr/channels/231638/live',
  15. 'info_dict': {
  16. 'id': '231638',
  17. 'ext': 'mp4',
  18. 'title': r're:^214하나만\.\.\. ',
  19. 'thumbnail': r're:^https?://.+\.jpg',
  20. 'upload_date': r're:\d{8}',
  21. 'timestamp': int,
  22. 'live_status': 'is_live',
  23. 'channel': 'Hi별',
  24. 'channel_id': '244396',
  25. },
  26. 'skip': 'The channel is offline',
  27. }, {
  28. 'url': 'https://www.flextv.co.kr/channels/746/live',
  29. 'only_matching': True,
  30. }]
  31. def _real_extract(self, url):
  32. channel_id = self._match_id(url)
  33. try:
  34. stream_data = self._download_json(
  35. f'https://api.flextv.co.kr/api/channels/{channel_id}/stream',
  36. channel_id, query={'option': 'all'})
  37. except ExtractorError as e:
  38. if isinstance(e.cause, HTTPError) and e.cause.status == 400:
  39. raise UserNotLive(video_id=channel_id)
  40. raise
  41. playlist_url = stream_data['sources'][0]['url']
  42. formats, subtitles = self._extract_m3u8_formats_and_subtitles(
  43. playlist_url, channel_id, 'mp4')
  44. return {
  45. 'id': channel_id,
  46. 'formats': formats,
  47. 'subtitles': subtitles,
  48. 'is_live': True,
  49. **traverse_obj(stream_data, {
  50. 'title': ('stream', 'title', {str}),
  51. 'timestamp': ('stream', 'createdAt', {parse_iso8601}),
  52. 'thumbnail': ('thumbUrl', {url_or_none}),
  53. 'channel': ('owner', 'name', {str}),
  54. 'channel_id': ('owner', 'id', {str_or_none}),
  55. }),
  56. }