]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/filmon.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / filmon.py
CommitLineData
a0758dfa 1from .common import InfoExtractor
4ce3407d
RA
2from ..compat import (
3 compat_str,
4 compat_HTTPError,
5)
6from ..utils import (
7 qualities,
8 strip_or_none,
9 int_or_none,
10 ExtractorError,
11)
a0758dfa 12
13
14class FilmOnIE(InfoExtractor):
4ce3407d
RA
15 IE_NAME = 'filmon'
16 _VALID_URL = r'(?:https?://(?:www\.)?filmon\.com/vod/view/|filmon:)(?P<id>\d+)'
a0758dfa 17 _TESTS = [{
4ce3407d
RA
18 'url': 'https://www.filmon.com/vod/view/24869-0-plan-9-from-outer-space',
19 'info_dict': {
20 'id': '24869',
21 'ext': 'mp4',
22 'title': 'Plan 9 From Outer Space',
23 'description': 'Dead human, zombies and vampires',
24 },
a0758dfa 25 }, {
4ce3407d
RA
26 'url': 'https://www.filmon.com/vod/view/2825-1-popeye-series-1',
27 'info_dict': {
28 'id': '2825',
29 'title': 'Popeye Series 1',
30 'description': 'The original series of Popeye.',
31 },
32 'playlist_mincount': 8,
a0758dfa 33 }]
34
35 def _real_extract(self, url):
4ce3407d 36 video_id = self._match_id(url)
a0758dfa 37
4ce3407d
RA
38 try:
39 response = self._download_json(
40 'https://www.filmon.com/api/vod/movie?id=%s' % video_id,
41 video_id)['response']
42 except ExtractorError as e:
43 if isinstance(e.cause, compat_HTTPError):
44 errmsg = self._parse_json(e.cause.read().decode(), video_id)['reason']
45 raise ExtractorError('%s said: %s' % (self.IE_NAME, errmsg), expected=True)
46 raise
a0758dfa 47
4ce3407d
RA
48 title = response['title']
49 description = strip_or_none(response.get('description'))
a0758dfa 50
4ce3407d
RA
51 if response.get('type_id') == 1:
52 entries = [self.url_result('filmon:' + episode_id) for episode_id in response.get('episodes', [])]
53 return self.playlist_result(entries, video_id, title, description)
a0758dfa 54
4ce3407d
RA
55 QUALITY = qualities(('low', 'high'))
56 formats = []
57 for format_id, stream in response.get('streams', {}).items():
58 stream_url = stream.get('url')
59 if not stream_url:
60 continue
a0758dfa 61 formats.append({
4ce3407d
RA
62 'format_id': format_id,
63 'url': stream_url,
a0758dfa 64 'ext': 'mp4',
4ce3407d
RA
65 'quality': QUALITY(stream.get('quality')),
66 'protocol': 'm3u8_native',
a0758dfa 67 })
a0758dfa 68
4ce3407d
RA
69 thumbnails = []
70 poster = response.get('poster', {})
71 thumbs = poster.get('thumbs', {})
72 thumbs['poster'] = poster
73 for thumb_id, thumb in thumbs.items():
74 thumb_url = thumb.get('url')
75 if not thumb_url:
76 continue
77 thumbnails.append({
78 'id': thumb_id,
79 'url': thumb_url,
80 'width': int_or_none(thumb.get('width')),
81 'height': int_or_none(thumb.get('height')),
82 })
83
a0758dfa 84 return {
4ce3407d
RA
85 'id': video_id,
86 'title': title,
a0758dfa 87 'formats': formats,
4ce3407d 88 'description': description,
a0758dfa 89 'thumbnails': thumbnails,
a0758dfa 90 }
91
92
4ce3407d
RA
93class FilmOnChannelIE(InfoExtractor):
94 IE_NAME = 'filmon:channel'
95 _VALID_URL = r'https?://(?:www\.)?filmon\.com/(?:tv|channel)/(?P<id>[a-z0-9-]+)'
a0758dfa 96 _TESTS = [{
4ce3407d
RA
97 # VOD
98 'url': 'http://www.filmon.com/tv/sports-haters',
a0758dfa 99 'info_dict': {
4ce3407d 100 'id': '4190',
a0758dfa 101 'ext': 'mp4',
4ce3407d
RA
102 'title': 'Sports Haters',
103 'description': 'md5:dabcb4c1d9cfc77085612f1a85f8275d',
a0758dfa 104 },
105 }, {
4ce3407d
RA
106 # LIVE
107 'url': 'https://www.filmon.com/channel/filmon-sports',
108 'only_matching': True,
109 }, {
110 'url': 'https://www.filmon.com/tv/2894',
111 'only_matching': True,
a0758dfa 112 }]
113
4ce3407d
RA
114 _THUMBNAIL_RES = [
115 ('logo', 56, 28),
116 ('big_logo', 106, 106),
117 ('extra_big_logo', 300, 300),
118 ]
a0758dfa 119
4ce3407d
RA
120 def _real_extract(self, url):
121 channel_id = self._match_id(url)
a0758dfa 122
4ce3407d
RA
123 try:
124 channel_data = self._download_json(
125 'http://www.filmon.com/api-v2/channel/' + channel_id, channel_id)['data']
126 except ExtractorError as e:
127 if isinstance(e.cause, compat_HTTPError):
128 errmsg = self._parse_json(e.cause.read().decode(), channel_id)['message']
129 raise ExtractorError('%s said: %s' % (self.IE_NAME, errmsg), expected=True)
130 raise
a0758dfa 131
4ce3407d
RA
132 channel_id = compat_str(channel_data['id'])
133 is_live = not channel_data.get('is_vod') and not channel_data.get('is_vox')
134 title = channel_data['title']
a0758dfa 135
4ce3407d 136 QUALITY = qualities(('low', 'high'))
a0758dfa 137 formats = []
4ce3407d
RA
138 for stream in channel_data.get('streams', []):
139 stream_url = stream.get('url')
140 if not stream_url:
141 continue
142 if not is_live:
143 formats.extend(self._extract_wowza_formats(
144 stream_url, channel_id, skip_protocols=['dash', 'rtmp', 'rtsp']))
145 continue
146 quality = stream.get('quality')
a0758dfa 147 formats.append({
4ce3407d
RA
148 'format_id': quality,
149 # this is an m3u8 stream, but we are deliberately not using _extract_m3u8_formats
150 # because it doesn't have bitrate variants anyway
151 'url': stream_url,
a0758dfa 152 'ext': 'mp4',
4ce3407d 153 'quality': QUALITY(quality),
a0758dfa 154 })
a0758dfa 155
4ce3407d
RA
156 thumbnails = []
157 for name, width, height in self._THUMBNAIL_RES:
a0758dfa 158 thumbnails.append({
4ce3407d
RA
159 'id': name,
160 'url': 'http://static.filmon.com/assets/channels/%s/%s.png' % (channel_id, name),
161 'width': width,
162 'height': height,
a0758dfa 163 })
164
165 return {
4ce3407d
RA
166 'id': channel_id,
167 'display_id': channel_data.get('alias'),
39ca3b5c 168 'title': title,
4ce3407d 169 'description': channel_data.get('description'),
a0758dfa 170 'thumbnails': thumbnails,
4ce3407d
RA
171 'formats': formats,
172 'is_live': is_live,
a0758dfa 173 }