aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/freshlive.py
blob: 72a8459453bb2702df66664eec2ff16c0c78f1a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# coding: utf-8
from __future__ import unicode_literals

from .common import InfoExtractor
from ..compat import compat_str
from ..utils import (
    ExtractorError,
    int_or_none,
    try_get,
    unified_timestamp,
)


class FreshLiveIE(InfoExtractor):
    _VALID_URL = r'https?://freshlive\.tv/[^/]+/(?P<id>\d+)'
    _TEST = {
        'url': 'https://freshlive.tv/satotv/74712',
        'md5': '9f0cf5516979c4454ce982df3d97f352',
        'info_dict': {
            'id': '74712',
            'ext': 'mp4',
            'title': 'テスト',
            'description': 'テスト',
            'thumbnail': r're:^https?://.*\.jpg$',
            'duration': 1511,
            'timestamp': 1483619655,
            'upload_date': '20170105',
            'uploader': 'サトTV',
            'uploader_id': 'satotv',
            'view_count': int,
            'comment_count': int,
            'is_live': False,
        }
    }

    def _real_extract(self, url):
        video_id = self._match_id(url)

        webpage = self._download_webpage(url, video_id)

        options = self._parse_json(
            self._search_regex(
                r'window\.__CONTEXT__\s*=\s*({.+?});\s*</script>',
                webpage, 'initial context'),
            video_id)

        info = options['context']['dispatcher']['stores']['ProgramStore']['programs'][video_id]

        title = info['title']

        if info.get('status') == 'upcoming':
            raise ExtractorError('Stream %s is upcoming' % video_id, expected=True)

        stream_url = info.get('liveStreamUrl') or info['archiveStreamUrl']

        is_live = info.get('liveStreamUrl') is not None

        formats = self._extract_m3u8_formats(
            stream_url, video_id, 'mp4',
            'm3u8_native', m3u8_id='hls')

        if is_live:
            title = self._live_title(title)

        return {
            'id': video_id,
            'formats': formats,
            'title': title,
            'description': info.get('description'),
            'thumbnail': info.get('thumbnailUrl'),
            'duration': int_or_none(info.get('airTime')),
            'timestamp': unified_timestamp(info.get('createdAt')),
            'uploader': try_get(
                info, lambda x: x['channel']['title'], compat_str),
            'uploader_id': try_get(
                info, lambda x: x['channel']['code'], compat_str),
            'uploader_url': try_get(
                info, lambda x: x['channel']['permalink'], compat_str),
            'view_count': int_or_none(info.get('viewCount')),
            'comment_count': int_or_none(info.get('commentCount')),
            'tags': info.get('tags', []),
            'is_live': is_live,
        }