freshlive.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. try_get,
  9. unified_timestamp,
  10. )
  11. class FreshLiveIE(InfoExtractor):
  12. _VALID_URL = r'https?://freshlive\.tv/[^/]+/(?P<id>\d+)'
  13. _TEST = {
  14. 'url': 'https://freshlive.tv/satotv/74712',
  15. 'md5': '9f0cf5516979c4454ce982df3d97f352',
  16. 'info_dict': {
  17. 'id': '74712',
  18. 'ext': 'mp4',
  19. 'title': 'テスト',
  20. 'description': 'テスト',
  21. 'thumbnail': r're:^https?://.*\.jpg$',
  22. 'duration': 1511,
  23. 'timestamp': 1483619655,
  24. 'upload_date': '20170105',
  25. 'uploader': 'サトTV',
  26. 'uploader_id': 'satotv',
  27. 'view_count': int,
  28. 'comment_count': int,
  29. 'is_live': False,
  30. }
  31. }
  32. def _real_extract(self, url):
  33. video_id = self._match_id(url)
  34. webpage = self._download_webpage(url, video_id)
  35. options = self._parse_json(
  36. self._search_regex(
  37. r'window\.__CONTEXT__\s*=\s*({.+?});\s*</script>',
  38. webpage, 'initial context'),
  39. video_id)
  40. info = options['context']['dispatcher']['stores']['ProgramStore']['programs'][video_id]
  41. title = info['title']
  42. if info.get('status') == 'upcoming':
  43. raise ExtractorError('Stream %s is upcoming' % video_id, expected=True)
  44. stream_url = info.get('liveStreamUrl') or info['archiveStreamUrl']
  45. is_live = info.get('liveStreamUrl') is not None
  46. formats = self._extract_m3u8_formats(
  47. stream_url, video_id, ext='mp4',
  48. entry_protocol='m3u8' if is_live else 'm3u8_native',
  49. m3u8_id='hls')
  50. if is_live:
  51. title = self._live_title(title)
  52. return {
  53. 'id': video_id,
  54. 'formats': formats,
  55. 'title': title,
  56. 'description': info.get('description'),
  57. 'thumbnail': info.get('thumbnailUrl'),
  58. 'duration': int_or_none(info.get('airTime')),
  59. 'timestamp': unified_timestamp(info.get('createdAt')),
  60. 'uploader': try_get(
  61. info, lambda x: x['channel']['title'], compat_str),
  62. 'uploader_id': try_get(
  63. info, lambda x: x['channel']['code'], compat_str),
  64. 'uploader_url': try_get(
  65. info, lambda x: x['channel']['permalink'], compat_str),
  66. 'view_count': int_or_none(info.get('viewCount')),
  67. 'comment_count': int_or_none(info.get('commentCount')),
  68. 'tags': info.get('tags', []),
  69. 'is_live': is_live,
  70. }