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