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