aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/filmon.py
blob: 987792fec699e3919322ae0bb9557bd7a7e62d18 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# coding: utf-8
from __future__ import unicode_literals

from .common import InfoExtractor
from ..utils import qualities
from ..compat import compat_urllib_request


_QUALITY = qualities(('low', 'high'))


class FilmOnIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?filmon\.com/(?:tv|channel)/(?P<id>[a-z0-9-]+)'
    _TESTS = [{
        'url': 'https://www.filmon.com/channel/filmon-sports',
        'only_matching': True,
    }, {
        'url': 'https://www.filmon.com/tv/2894',
        'only_matching': True,
    }]

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

        request = compat_urllib_request.Request('https://www.filmon.com/channel/%s' % (channel_id))
        request.add_header('X-Requested-With', 'XMLHttpRequest')
        channel_info = self._download_json(request, channel_id)
        now_playing = channel_info['now_playing']

        thumbnails = []
        for thumb in now_playing.get('images', ()):
            if thumb['type'] != '2':
                continue
            thumbnails.append({
                'url': thumb['url'],
                'width': int(thumb['width']),
                'height': int(thumb['height']),
            })

        formats = []

        for stream in channel_info['streams']:
            formats.append({
                'format_id': str(stream['id']),
                # this is an m3u8 stream, but we are deliberately not using _extract_m3u8_formats
                # because 0) it doesn't have bitrate variants anyway, and 1) the ids generated
                # by that method are highly unstable (because the bitrate is variable)
                'url': stream['url'],
                'resolution': stream['name'],
                'format_note': 'expires after %u seconds' % int(stream['watch-timeout']),
                'ext': 'mp4',
                'quality': _QUALITY(stream['quality']),
                'preference': int(stream['watch-timeout']),
            })
        self._sort_formats(formats)

        return {
            'id': str(channel_info['id']),
            'display_id': channel_info['alias'],
            'formats': formats,
            # XXX: use the channel description (channel_info['description'])?
            'uploader_id': channel_info['alias'],
            'uploader': channel_info['title'], # XXX: kinda stretching it...
            'title': now_playing.get('programme_name') or channel_info['title'],
            'description': now_playing.get('programme_description'),
            'thumbnails': thumbnails,
            'is_live': True,
        }


class FilmOnVODIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?filmon\.com/vod/view/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.filmon.com/vod/view/24869-0-plan-9-from-outer-space',
        'info_dict': {
            'id': '24869',
            'ext': 'mp4',
            'title': 'Plan 9 From Outer Space',
            'description': 'Dead human, zombies and vampires',
        },
    }, {
        'url': 'https://www.filmon.com/vod/view/2825-1-popeye-series-1',
        'info_dict': {
            'id': '2825',
            'title': 'Popeye Series 1',
        },
        'playlist_count': 8,
    }]

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

        result = self._download_json('https://www.filmon.com/api/vod/movie?id=%s' % (video_id), video_id)
        if result['code'] != 200:
            raise ExtractorError('FilmOn said: %s' % (result['reason']), expected=True)

        response = result['response']

        if response.get('episodes'):
            return {
                '_type': 'playlist',
                'id': video_id,
                'title': response['title'],
                'entries': [{
                    '_type': 'url',
                    'url': 'https://www.filmon.com/vod/view/%s' % (ep),
                } for ep in response['episodes']]
            }

        formats = []
        for (id, stream) in response['streams'].items():
            formats.append({
                'format_id': id,
                'url': stream['url'],
                'resolution': stream['name'],
                'format_note': 'expires after %u seconds' % int(stream['watch-timeout']),
                'ext': 'mp4',
                'quality': _QUALITY(stream['quality']),
                'preference': int(stream['watch-timeout']),
            })
        self._sort_formats(formats)

        poster = response['poster']
        thumbnails = [{
            'id': 'poster',
            'url': poster['url'],
            'width': poster['width'],
            'height': poster['height'],
        }]
        for (id, thumb) in poster['thumbs'].items():
            thumbnails.append({
                'id': id,
                'url': thumb['url'],
                'width': thumb['width'],
                'height': thumb['height'],
            })

        return {
            'id': video_id,
            'title': response['title'],
            'formats': formats,
            'description': response['description'],
            'thumbnails': thumbnails,
        }