[cleanup] Make more playlist entries lazy (#11763)
[yt-dlp.git] / yt_dlp / extractor / puhutv.py
blobb62050ecd5d67ad0f277b281c8c35bf2cedd6597
1 from .common import InfoExtractor
2 from ..networking.exceptions import HTTPError
3 from ..utils import (
4 ExtractorError,
5 float_or_none,
6 int_or_none,
7 parse_resolution,
8 str_or_none,
9 try_get,
10 unified_timestamp,
11 url_or_none,
12 urljoin,
16 class PuhuTVIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:www\.)?puhutv\.com/(?P<id>[^/?#&]+)-izle'
18 IE_NAME = 'puhutv'
19 _TESTS = [{
20 # film
21 'url': 'https://puhutv.com/sut-kardesler-izle',
22 'md5': 'a347470371d56e1585d1b2c8dab01c96',
23 'info_dict': {
24 'id': '5085',
25 'display_id': 'sut-kardesler',
26 'ext': 'mp4',
27 'title': 'Süt Kardeşler',
28 'description': 'md5:ca09da25b7e57cbb5a9280d6e48d17aa',
29 'thumbnail': r're:^https?://.*\.jpg$',
30 'duration': 4832.44,
31 'creator': 'Arzu Film',
32 'timestamp': 1561062602,
33 'upload_date': '20190620',
34 'release_year': 1976,
35 'view_count': int,
36 'tags': list,
38 }, {
39 # episode, geo restricted, bypassable with --geo-verification-proxy
40 'url': 'https://puhutv.com/jet-sosyete-1-bolum-izle',
41 'only_matching': True,
42 }, {
43 # 4k, with subtitles
44 'url': 'https://puhutv.com/dip-1-bolum-izle',
45 'only_matching': True,
47 _SUBTITLE_LANGS = {
48 'English': 'en',
49 'Deutsch': 'de',
50 'عربى': 'ar',
53 def _real_extract(self, url):
54 display_id = self._match_id(url)
56 info = self._download_json(
57 urljoin(url, f'/api/slug/{display_id}-izle'),
58 display_id)['data']
60 video_id = str(info['id'])
61 show = info.get('title') or {}
62 title = info.get('name') or show['name']
63 if info.get('display_name'):
64 title = '{} {}'.format(title, info['display_name'])
66 try:
67 videos = self._download_json(
68 f'https://puhutv.com/api/assets/{video_id}/videos',
69 display_id, 'Downloading video JSON',
70 headers=self.geo_verification_headers())
71 except ExtractorError as e:
72 if isinstance(e.cause, HTTPError) and e.cause.status == 403:
73 self.raise_geo_restricted()
74 raise
76 urls = []
77 formats = []
79 for video in videos['data']['videos']:
80 media_url = url_or_none(video.get('url'))
81 if not media_url or media_url in urls:
82 continue
83 urls.append(media_url)
85 playlist = video.get('is_playlist')
86 if (video.get('stream_type') == 'hls' and playlist is True) or 'playlist.m3u8' in media_url:
87 formats.extend(self._extract_m3u8_formats(
88 media_url, video_id, 'mp4', entry_protocol='m3u8_native',
89 m3u8_id='hls', fatal=False))
90 continue
92 quality = int_or_none(video.get('quality'))
93 f = {
94 'url': media_url,
95 'ext': 'mp4',
96 'height': quality,
98 video_format = video.get('video_format')
99 is_hls = (video_format == 'hls' or '/hls/' in media_url or '/chunklist.m3u8' in media_url) and playlist is False
100 if is_hls:
101 format_id = 'hls'
102 f['protocol'] = 'm3u8_native'
103 elif video_format == 'mp4':
104 format_id = 'http'
105 else:
106 continue
107 if quality:
108 format_id += f'-{quality}p'
109 f['format_id'] = format_id
110 formats.append(f)
112 creator = try_get(
113 show, lambda x: x['producer']['name'], str)
115 content = info.get('content') or {}
117 images = try_get(
118 content, lambda x: x['images']['wide'], dict) or {}
119 thumbnails = []
120 for image_id, image_url in images.items():
121 if not isinstance(image_url, str):
122 continue
123 if not image_url.startswith(('http', '//')):
124 image_url = f'https://{image_url}'
125 t = parse_resolution(image_id)
126 t.update({
127 'id': image_id,
128 'url': image_url,
130 thumbnails.append(t)
132 tags = []
133 for genre in show.get('genres') or []:
134 if not isinstance(genre, dict):
135 continue
136 genre_name = genre.get('name')
137 if genre_name and isinstance(genre_name, str):
138 tags.append(genre_name)
140 subtitles = {}
141 for subtitle in content.get('subtitles') or []:
142 if not isinstance(subtitle, dict):
143 continue
144 lang = subtitle.get('language')
145 sub_url = url_or_none(subtitle.get('url') or subtitle.get('file'))
146 if not lang or not isinstance(lang, str) or not sub_url:
147 continue
148 subtitles[self._SUBTITLE_LANGS.get(lang, lang)] = [{
149 'url': sub_url,
152 return {
153 'id': video_id,
154 'display_id': display_id,
155 'title': title,
156 'description': info.get('description') or show.get('description'),
157 'season_id': str_or_none(info.get('season_id')),
158 'season_number': int_or_none(info.get('season_number')),
159 'episode_number': int_or_none(info.get('episode_number')),
160 'release_year': int_or_none(show.get('released_at')),
161 'timestamp': unified_timestamp(info.get('created_at')),
162 'creator': creator,
163 'view_count': int_or_none(content.get('watch_count')),
164 'duration': float_or_none(content.get('duration_in_ms'), 1000),
165 'tags': tags,
166 'subtitles': subtitles,
167 'thumbnails': thumbnails,
168 'formats': formats,
172 class PuhuTVSerieIE(InfoExtractor):
173 _VALID_URL = r'https?://(?:www\.)?puhutv\.com/(?P<id>[^/?#&]+)-detay'
174 IE_NAME = 'puhutv:serie'
175 _TESTS = [{
176 'url': 'https://puhutv.com/deniz-yildizi-detay',
177 'info_dict': {
178 'title': 'Deniz Yıldızı',
179 'id': 'deniz-yildizi',
181 'playlist_mincount': 205,
182 }, {
183 # a film detail page which is using same url with serie page
184 'url': 'https://puhutv.com/kaybedenler-kulubu-detay',
185 'only_matching': True,
188 def _extract_entries(self, seasons):
189 for season in seasons:
190 season_id = season.get('id')
191 if not season_id:
192 continue
193 page = 1
194 has_more = True
195 while has_more is True:
196 season = self._download_json(
197 f'https://galadriel.puhutv.com/seasons/{season_id}',
198 season_id, f'Downloading page {page}', query={
199 'page': page,
200 'per': 40,
202 episodes = season.get('episodes')
203 if isinstance(episodes, list):
204 for ep in episodes:
205 slug_path = str_or_none(ep.get('slugPath'))
206 if not slug_path:
207 continue
208 video_id = str_or_none(int_or_none(ep.get('id')))
209 yield self.url_result(
210 f'https://puhutv.com/{slug_path}',
211 ie=PuhuTVIE.ie_key(), video_id=video_id,
212 video_title=ep.get('name') or ep.get('eventLabel'))
213 page += 1
214 has_more = season.get('hasMore')
216 def _real_extract(self, url):
217 playlist_id = self._match_id(url)
219 info = self._download_json(
220 urljoin(url, f'/api/slug/{playlist_id}-detay'),
221 playlist_id)['data']
223 seasons = info.get('seasons')
224 if seasons:
225 return self.playlist_result(
226 self._extract_entries(seasons), playlist_id, info.get('name'))
228 # For films, these are using same url with series
229 video_id = info.get('slug') or info['assets'][0]['slug']
230 return self.url_result(
231 f'https://puhutv.com/{video_id}-izle',
232 PuhuTVIE.ie_key(), video_id)