]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/puhutv.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / puhutv.py
CommitLineData
6de82b44 1from .common import InfoExtractor
3d2623a8 2from ..networking.exceptions import HTTPError
6de82b44 3from ..utils import (
8fd2a7be 4 ExtractorError,
6de82b44 5 float_or_none,
e897bd82 6 int_or_none,
8fd2a7be 7 parse_resolution,
6de82b44 8 str_or_none,
6de82b44 9 try_get,
8fd2a7be
S
10 unified_timestamp,
11 url_or_none,
12 urljoin,
6de82b44
E
13)
14
15
16class PuhuTVIE(InfoExtractor):
8fd2a7be 17 _VALID_URL = r'https?://(?:www\.)?puhutv\.com/(?P<id>[^/?#&]+)-izle'
6de82b44 18 IE_NAME = 'puhutv'
8fd2a7be
S
19 _TESTS = [{
20 # film
21 'url': 'https://puhutv.com/sut-kardesler-izle',
162bcc68 22 'md5': 'a347470371d56e1585d1b2c8dab01c96',
8fd2a7be
S
23 'info_dict': {
24 'id': '5085',
25 'display_id': 'sut-kardesler',
26 'ext': 'mp4',
27 'title': 'Süt Kardeşler',
162bcc68 28 'description': 'md5:ca09da25b7e57cbb5a9280d6e48d17aa',
8fd2a7be
S
29 'thumbnail': r're:^https?://.*\.jpg$',
30 'duration': 4832.44,
31 'creator': 'Arzu Film',
162bcc68
RA
32 'timestamp': 1561062602,
33 'upload_date': '20190620',
8fd2a7be
S
34 'release_year': 1976,
35 'view_count': int,
162bcc68 36 'tags': list,
6de82b44 37 },
8fd2a7be
S
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,
46 }]
6de82b44
E
47 _SUBTITLE_LANGS = {
48 'English': 'en',
49 'Deutsch': 'de',
add96eb9 50 'عربى': 'ar',
6de82b44
E
51 }
52
53 def _real_extract(self, url):
8fd2a7be
S
54 display_id = self._match_id(url)
55
6de82b44 56 info = self._download_json(
add96eb9 57 urljoin(url, f'/api/slug/{display_id}-izle'),
8fd2a7be 58 display_id)['data']
6de82b44 59
add96eb9 60 video_id = str(info['id'])
162bcc68
RA
61 show = info.get('title') or {}
62 title = info.get('name') or show['name']
6de82b44 63 if info.get('display_name'):
add96eb9 64 title = '{} {}'.format(title, info['display_name'])
6de82b44 65
8fd2a7be
S
66 try:
67 videos = self._download_json(
add96eb9 68 f'https://puhutv.com/api/assets/{video_id}/videos',
8fd2a7be
S
69 display_id, 'Downloading video JSON',
70 headers=self.geo_verification_headers())
71 except ExtractorError as e:
3d2623a8 72 if isinstance(e.cause, HTTPError) and e.cause.status == 403:
8fd2a7be
S
73 self.raise_geo_restricted()
74 raise
75
162bcc68 76 urls = []
8fd2a7be 77 formats = []
162bcc68 78
8fd2a7be
S
79 for video in videos['data']['videos']:
80 media_url = url_or_none(video.get('url'))
162bcc68 81 if not media_url or media_url in urls:
8fd2a7be 82 continue
162bcc68
RA
83 urls.append(media_url)
84
8fd2a7be 85 playlist = video.get('is_playlist')
162bcc68 86 if (video.get('stream_type') == 'hls' and playlist is True) or 'playlist.m3u8' in media_url:
f7f30491 87 formats.extend(self._extract_m3u8_formats(
8fd2a7be 88 media_url, video_id, 'mp4', entry_protocol='m3u8_native',
f7f30491 89 m3u8_id='hls', fatal=False))
8fd2a7be 90 continue
162bcc68 91
8fd2a7be
S
92 quality = int_or_none(video.get('quality'))
93 f = {
94 'url': media_url,
95 'ext': 'mp4',
add96eb9 96 'height': quality,
8fd2a7be
S
97 }
98 video_format = video.get('video_format')
162bcc68
RA
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:
8fd2a7be
S
101 format_id = 'hls'
102 f['protocol'] = 'm3u8_native'
103 elif video_format == 'mp4':
104 format_id = 'http'
8fd2a7be
S
105 else:
106 continue
107 if quality:
add96eb9 108 format_id += f'-{quality}p'
8fd2a7be
S
109 f['format_id'] = format_id
110 formats.append(f)
8fd2a7be 111
8fd2a7be 112 creator = try_get(
add96eb9 113 show, lambda x: x['producer']['name'], str)
8fd2a7be 114
162bcc68 115 content = info.get('content') or {}
8fd2a7be
S
116
117 images = try_get(
162bcc68 118 content, lambda x: x['images']['wide'], dict) or {}
8fd2a7be
S
119 thumbnails = []
120 for image_id, image_url in images.items():
add96eb9 121 if not isinstance(image_url, str):
8fd2a7be
S
122 continue
123 if not image_url.startswith(('http', '//')):
add96eb9 124 image_url = f'https://{image_url}'
8fd2a7be
S
125 t = parse_resolution(image_id)
126 t.update({
127 'id': image_id,
add96eb9 128 'url': image_url,
8fd2a7be
S
129 })
130 thumbnails.append(t)
131
6de82b44 132 tags = []
162bcc68 133 for genre in show.get('genres') or []:
8fd2a7be 134 if not isinstance(genre, dict):
6de82b44 135 continue
8fd2a7be 136 genre_name = genre.get('name')
add96eb9 137 if genre_name and isinstance(genre_name, str):
8fd2a7be 138 tags.append(genre_name)
6de82b44
E
139
140 subtitles = {}
162bcc68 141 for subtitle in content.get('subtitles') or []:
6de82b44
E
142 if not isinstance(subtitle, dict):
143 continue
144 lang = subtitle.get('language')
162bcc68 145 sub_url = url_or_none(subtitle.get('url') or subtitle.get('file'))
add96eb9 146 if not lang or not isinstance(lang, str) or not sub_url:
6de82b44
E
147 continue
148 subtitles[self._SUBTITLE_LANGS.get(lang, lang)] = [{
add96eb9 149 'url': sub_url,
6de82b44
E
150 }]
151
6de82b44
E
152 return {
153 'id': video_id,
154 'display_id': display_id,
155 'title': title,
162bcc68
RA
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')),
8fd2a7be 162 'creator': creator,
162bcc68
RA
163 'view_count': int_or_none(content.get('watch_count')),
164 'duration': float_or_none(content.get('duration_in_ms'), 1000),
6de82b44
E
165 'tags': tags,
166 'subtitles': subtitles,
6de82b44 167 'thumbnails': thumbnails,
add96eb9 168 'formats': formats,
6de82b44
E
169 }
170
171
172class PuhuTVSerieIE(InfoExtractor):
8fd2a7be 173 _VALID_URL = r'https?://(?:www\.)?puhutv\.com/(?P<id>[^/?#&]+)-detay'
6de82b44 174 IE_NAME = 'puhutv:serie'
8fd2a7be
S
175 _TESTS = [{
176 'url': 'https://puhutv.com/deniz-yildizi-detay',
177 'info_dict': {
178 'title': 'Deniz Yıldızı',
179 'id': 'deniz-yildizi',
6de82b44 180 },
8fd2a7be
S
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,
186 }]
187
188 def _extract_entries(self, seasons):
6de82b44 189 for season in seasons:
8fd2a7be
S
190 season_id = season.get('id')
191 if not season_id:
192 continue
193 page = 1
6de82b44
E
194 has_more = True
195 while has_more is True:
8fd2a7be 196 season = self._download_json(
add96eb9 197 f'https://galadriel.puhutv.com/seasons/{season_id}',
198 season_id, f'Downloading page {page}', query={
8fd2a7be 199 'page': page,
6de82b44
E
200 'per': 40,
201 })
8fd2a7be
S
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(
add96eb9 210 f'https://puhutv.com/{slug_path}',
8fd2a7be
S
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')
6de82b44
E
215
216 def _real_extract(self, url):
217 playlist_id = self._match_id(url)
218
219 info = self._download_json(
add96eb9 220 urljoin(url, f'/api/slug/{playlist_id}-detay'),
8fd2a7be 221 playlist_id)['data']
6de82b44 222
6de82b44
E
223 seasons = info.get('seasons')
224 if seasons:
8fd2a7be
S
225 return self.playlist_result(
226 self._extract_entries(seasons), playlist_id, info.get('name'))
227
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(
add96eb9 231 f'https://puhutv.com/{video_id}-izle',
8fd2a7be 232 PuhuTVIE.ie_key(), video_id)