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