[youtube] Fix extraction.
[youtube-dl] / 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, 'mp4',
60             'm3u8_native', m3u8_id='hls')
61
62         if is_live:
63             title = self._live_title(title)
64
65         return {
66             'id': video_id,
67             'formats': formats,
68             'title': title,
69             'description': info.get('description'),
70             'thumbnail': info.get('thumbnailUrl'),
71             'duration': int_or_none(info.get('airTime')),
72             'timestamp': unified_timestamp(info.get('createdAt')),
73             'uploader': try_get(
74                 info, lambda x: x['channel']['title'], compat_str),
75             'uploader_id': try_get(
76                 info, lambda x: x['channel']['code'], compat_str),
77             'uploader_url': try_get(
78                 info, lambda x: x['channel']['permalink'], compat_str),
79             'view_count': int_or_none(info.get('viewCount')),
80             'comment_count': int_or_none(info.get('commentCount')),
81             'tags': info.get('tags', []),
82             'is_live': is_live,
83         }