Merge branch 'fstirlitz-filmon'
[youtube-dl] / youtube_dl / extractor / beampro.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     clean_html,
8     compat_str,
9     int_or_none,
10     parse_iso8601,
11     try_get,
12 )
13
14
15 class BeamProLiveIE(InfoExtractor):
16     IE_NAME = 'Beam:live'
17     _VALID_URL = r'https?://(?:\w+\.)?beam\.pro/(?P<id>[^/?#&]+)'
18     _RATINGS = {'family': 0, 'teen': 13, '18+': 18}
19     _TEST = {
20         'url': 'http://www.beam.pro/niterhayven',
21         'info_dict': {
22             'id': '261562',
23             'ext': 'mp4',
24             'title': 'Introducing The Witcher 3 //  The Grind Starts Now!',
25             'description': 'md5:0b161ac080f15fe05d18a07adb44a74d',
26             'thumbnail': r're:https://.*\.jpg$',
27             'timestamp': 1483477281,
28             'upload_date': '20170103',
29             'uploader': 'niterhayven',
30             'uploader_id': '373396',
31             'age_limit': 18,
32             'is_live': True,
33             'view_count': int,
34         },
35         'skip': 'niterhayven is offline',
36         'params': {
37             'skip_download': True,
38         },
39     }
40
41     def _real_extract(self, url):
42         channel_name = self._match_id(url)
43
44         chan = self._download_json(
45             'https://beam.pro/api/v1/channels/%s' % channel_name, channel_name)
46
47         if chan.get('online') is False:
48             raise ExtractorError(
49                 '{0} is offline'.format(channel_name), expected=True)
50
51         channel_id = chan['id']
52
53         formats = self._extract_m3u8_formats(
54             'https://beam.pro/api/v1/channels/%s/manifest.m3u8' % channel_id,
55             channel_name, ext='mp4', m3u8_id='hls', fatal=False)
56         self._sort_formats(formats)
57
58         user_id = chan.get('userId') or try_get(chan, lambda x: x['user']['id'])
59
60         return {
61             'id': compat_str(chan.get('id') or channel_name),
62             'title': self._live_title(chan.get('name') or channel_name),
63             'description': clean_html(chan.get('description')),
64             'thumbnail': try_get(chan, lambda x: x['thumbnail']['url'], compat_str),
65             'timestamp': parse_iso8601(chan.get('updatedAt')),
66             'uploader': chan.get('token') or try_get(
67                 chan, lambda x: x['user']['username'], compat_str),
68             'uploader_id': compat_str(user_id) if user_id else None,
69             'age_limit': self._RATINGS.get(chan.get('audience')),
70             'is_live': True,
71             'view_count': int_or_none(chan.get('viewersTotal')),
72             'formats': formats,
73         }