[freshlive] Add extractor
[youtube-dl] / youtube_dl / extractor / freshlive.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5
6 from ..utils import (
7     int_or_none,
8     parse_iso8601
9 )
10
11 class FreshliveIE(InfoExtractor):
12     _VALID_URL = r'https?://freshlive\.tv/(?P<streamer>[^/]+)/(?P<id>[0-9]+)'
13     _TEST = {
14         'url': 'https://freshlive.tv/satotv/74712',
15         'md5': '224f50d268b6b9f94e4198deccd55d6d',
16         'info_dict': {
17             'description': 'テスト',
18             'duration': 1511,
19             'id': '74712',
20             'ext': 'mp4',
21             'timestamp': 1483621764,
22             'title': 'テスト',
23             'thumbnail': r're:^https?://.*\.jpg$',
24             'upload_date': '20170105',
25             'uploader': 'サトTV',
26             'uploader_id': 'satotv',
27             'view_count': int,
28         }
29     }
30
31     def _real_extract(self, url):
32         video_id = self._match_id(url)
33         webpage = self._download_webpage(url, video_id)
34
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
41         programs = options['context']['dispatcher']['stores']['ProgramStore']['programs']
42         info = programs.get(video_id, {})
43
44         video_url = info.get('liveStreamUrl') or info.get('archiveStreamUrl')
45         if not video_url:
46             raise ExtractorError('%s not a valid broadcast ID' % video_id, expected=True)
47
48         formats = self._extract_m3u8_formats(
49             video_url, video_id, ext='mp4', m3u8_id='hls')
50
51         return {
52             'id': video_id,
53             'formats': formats,
54             'title': info.get('title'),
55             'description': info.get('description'),
56             'duration': int_or_none(info.get('airTime')),
57             'is_live': int_or_none(info.get('airTime')) == None,
58             'thumbnail': info.get('thumbnailUrl'),
59             'uploader': info.get('channel', {}).get('title'),
60             'uploader_id': info.get('channel', {}).get('code'),
61             'uploader_url': info.get('channel', {}).get('permalink'),
62             'timestamp': parse_iso8601(info.get('startAt')),
63             'view_count': int_or_none(info.get('viewCount')),
64         }