[filmon] new extractor
[youtube-dl] / youtube_dl / extractor / filmon.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import qualities
6 from ..compat import compat_urllib_request
7
8
9 _QUALITY = qualities(('low', 'high'))
10
11
12 class FilmOnIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?filmon\.com/(?:tv|channel)/(?P<id>[a-z0-9-]+)'
14     _TESTS = [{
15         'url': 'https://www.filmon.com/channel/filmon-sports',
16         'only_matching': True,
17     }, {
18         'url': 'https://www.filmon.com/tv/2894',
19         'only_matching': True,
20     }]
21
22     def _real_extract(self, url):
23         channel_id = self._match_id(url)
24
25         request = compat_urllib_request.Request('https://www.filmon.com/channel/%s' % (channel_id))
26         request.add_header('X-Requested-With', 'XMLHttpRequest')
27         channel_info = self._download_json(request, channel_id)
28         now_playing = channel_info['now_playing']
29
30         thumbnails = []
31         for thumb in now_playing.get('images', ()):
32             if thumb['type'] != '2':
33                 continue
34             thumbnails.append({
35                 'url': thumb['url'],
36                 'width': int(thumb['width']),
37                 'height': int(thumb['height']),
38             })
39
40         formats = []
41
42         for stream in channel_info['streams']:
43             formats.append({
44                 'format_id': str(stream['id']),
45                 # this is an m3u8 stream, but we are deliberately not using _extract_m3u8_formats
46                 # because 0) it doesn't have bitrate variants anyway, and 1) the ids generated
47                 # by that method are highly unstable (because the bitrate is variable)
48                 'url': stream['url'],
49                 'resolution': stream['name'],
50                 'format_note': 'expires after %u seconds' % int(stream['watch-timeout']),
51                 'ext': 'mp4',
52                 'quality': _QUALITY(stream['quality']),
53                 'preference': int(stream['watch-timeout']),
54             })
55         self._sort_formats(formats)
56
57         return {
58             'id': str(channel_info['id']),
59             'display_id': channel_info['alias'],
60             'formats': formats,
61             # XXX: use the channel description (channel_info['description'])?
62             'uploader_id': channel_info['alias'],
63             'uploader': channel_info['title'], # XXX: kinda stretching it...
64             'title': now_playing.get('programme_name') or channel_info['title'],
65             'description': now_playing.get('programme_description'),
66             'thumbnails': thumbnails,
67             'is_live': True,
68         }
69
70
71 class FilmOnVODIE(InfoExtractor):
72     _VALID_URL = r'https?://(?:www\.)?filmon\.com/vod/view/(?P<id>\d+)'
73     _TESTS = [{
74         'url': 'https://www.filmon.com/vod/view/24869-0-plan-9-from-outer-space',
75         'info_dict': {
76             'id': '24869',
77             'ext': 'mp4',
78             'title': 'Plan 9 From Outer Space',
79             'description': 'Dead human, zombies and vampires',
80         },
81     }, {
82         'url': 'https://www.filmon.com/vod/view/2825-1-popeye-series-1',
83         'info_dict': {
84             'id': '2825',
85             'title': 'Popeye Series 1',
86         },
87         'playlist_count': 8,
88     }]
89
90     def _real_extract(self, url):
91         video_id = self._match_id(url)
92
93         result = self._download_json('https://www.filmon.com/api/vod/movie?id=%s' % (video_id), video_id)
94         if result['code'] != 200:
95             raise ExtractorError('FilmOn said: %s' % (result['reason']), expected=True)
96
97         response = result['response']
98
99         if response.get('episodes'):
100             return {
101                 '_type': 'playlist',
102                 'id': video_id,
103                 'title': response['title'],
104                 'entries': [{
105                     '_type': 'url',
106                     'url': 'https://www.filmon.com/vod/view/%s' % (ep),
107                 } for ep in response['episodes']]
108             }
109
110         formats = []
111         for (id, stream) in response['streams'].items():
112             formats.append({
113                 'format_id': id,
114                 'url': stream['url'],
115                 'resolution': stream['name'],
116                 'format_note': 'expires after %u seconds' % int(stream['watch-timeout']),
117                 'ext': 'mp4',
118                 'quality': _QUALITY(stream['quality']),
119                 'preference': int(stream['watch-timeout']),
120             })
121         self._sort_formats(formats)
122
123         poster = response['poster']
124         thumbnails = [{
125             'id': 'poster',
126             'url': poster['url'],
127             'width': poster['width'],
128             'height': poster['height'],
129         }]
130         for (id, thumb) in poster['thumbs'].items():
131             thumbnails.append({
132                 'id': id,
133                 'url': thumb['url'],
134                 'width': thumb['width'],
135                 'height': thumb['height'],
136             })
137
138         return {
139             'id': video_id,
140             'title': response['title'],
141             'formats': formats,
142             'description': response['description'],
143             'thumbnails': thumbnails,
144         }