[puhutv] Add extractor (closes #16010)
[youtube-dl] / youtube_dl / extractor / puhutv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_str
6 from ..utils import (
7     int_or_none,
8     float_or_none,
9     determine_ext,
10     str_or_none,
11     url_or_none,
12     unified_strdate,
13     unified_timestamp,
14     try_get,
15     url_basename,
16     remove_end
17 )
18
19
20 class PuhuTVIE(InfoExtractor):
21     _VALID_URL = r'https?://(?:www\.)?puhutv\.com/(?P<id>[a-z0-9-]+)-izle'
22     IE_NAME = 'puhutv'
23     _TESTS = [
24         {
25             # A Film
26             'url': 'https://puhutv.com/sut-kardesler-izle',
27             'md5': 'a347470371d56e1585d1b2c8dab01c96',
28             'info_dict': {
29                 'id': 'sut-kardesler',
30                 'display_id': '5085',
31                 'ext': 'mp4',
32                 'title': 'Süt Kardeşler',
33                 'thumbnail': r're:^https?://.*\.jpg$',
34                 'uploader': 'Arzu Film',
35                 'description': 'md5:405fd024df916ca16731114eb18e511a',
36                 'uploader_id': '43',
37                 'upload_date': '20160729',
38                 'timestamp': int,
39             },
40         },
41         {
42             # An Episode and geo restricted
43             'url': 'https://puhutv.com/jet-sosyete-1-bolum-izle',
44             'only_matching': True,
45         },
46         {
47             # Has subtitle
48             'url': 'https://puhutv.com/dip-1-bolum-izle',
49             'only_matching': True,
50         }
51     ]
52     _SUBTITLE_LANGS = {
53         'English': 'en',
54         'Deutsch': 'de',
55         'عربى': 'ar'
56     }
57
58     def _real_extract(self, url):
59         video_id = self._match_id(url)
60         info = self._download_json(
61             'https://puhutv.com/api/slug/%s-izle' % video_id, video_id)['data']
62
63         display_id = compat_str(info['id'])
64         title = info['title']['name']
65         if info.get('display_name'):
66             title = '%s %s' % (title, info.get('display_name'))
67
68         description = try_get(info, lambda x: x['title']['description'], compat_str) or info.get('description')
69         timestamp = unified_timestamp(info.get('created_at'))
70         upload_date = unified_strdate(info.get('created_at'))
71         uploader = try_get(info, lambda x: x['title']['producer']['name'], compat_str)
72         uploader_id = str_or_none(try_get(info, lambda x: x['title']['producer']['id']))
73         view_count = int_or_none(try_get(info, lambda x: x['content']['watch_count']))
74         duration = float_or_none(try_get(info, lambda x: x['content']['duration_in_ms']), scale=1000)
75         thumbnail = try_get(info, lambda x: x['content']['images']['wide']['main'], compat_str)
76         release_year = int_or_none(try_get(info, lambda x: x['title']['released_at']))
77         webpage_url = info.get('web_url')
78
79         season_number = int_or_none(info.get('season_number'))
80         season_id = int_or_none(info.get('season_id'))
81         episode_number = int_or_none(info.get('episode_number'))
82
83         tags = []
84         for tag in try_get(info, lambda x: x['title']['genres'], list) or []:
85             if isinstance(tag.get('name'), compat_str):
86                 tags.append(tag.get('name'))
87
88         thumbnails = []
89         thumbs_dict = try_get(info, lambda x: x['content']['images']['wide'], dict) or {}
90         for id, url in thumbs_dict.items():
91             if not url_or_none(url):
92                 continue
93             thumbnails.append({
94                 'url': 'https://%s' % url,
95                 'id': id
96             })
97
98         subtitles = {}
99         for subtitle in try_get(info, lambda x: x['content']['subtitles'], list) or []:
100             if not isinstance(subtitle, dict):
101                 continue
102             lang = subtitle.get('language')
103             sub_url = url_or_none(subtitle.get('url'))
104             if not lang or not isinstance(lang, compat_str) or not sub_url:
105                 continue
106             subtitles[self._SUBTITLE_LANGS.get(lang, lang)] = [{
107                 'url': sub_url
108             }]
109
110         # Some of videos are geo restricted upon request copyright owner and returns 403
111         req_formats = self._download_json(
112             'https://puhutv.com/api/assets/%s/videos' % display_id,
113             video_id, 'Downloading video JSON')
114
115         formats = []
116         for format in req_formats['data']['videos']:
117             media_url = url_or_none(format.get('url'))
118             if not media_url:
119                 continue
120             ext = format.get('video_format') or determine_ext(media_url)
121             quality = format.get('quality')
122             if format.get('stream_type') == 'hls' and format.get('is_playlist') is True:
123                 m3u8_id = remove_end(url_basename(media_url), '.m3u8')
124                 formats.append(self._m3u8_meta_format(media_url, ext, m3u8_id=m3u8_id))
125             elif ext == 'mp4' and format.get('is_playlist', False) is False:
126                 formats.append({
127                     'url': media_url,
128                     'format_id': 'http-%s' % quality,
129                     'ext': ext,
130                     'height': quality
131                 })
132         self._sort_formats(formats)
133
134         return {
135             'id': video_id,
136             'display_id': display_id,
137             'title': title,
138             'description': description,
139             'season_id': season_id,
140             'season_number': season_number,
141             'episode_number': episode_number,
142             'release_year': release_year,
143             'upload_date': upload_date,
144             'timestamp': timestamp,
145             'uploader': uploader,
146             'uploader_id': uploader_id,
147             'view_count': view_count,
148             'duration': duration,
149             'tags': tags,
150             'subtitles': subtitles,
151             'webpage_url': webpage_url,
152             'thumbnail': thumbnail,
153             'thumbnails': thumbnails,
154             'formats': formats
155         }
156
157
158 class PuhuTVSerieIE(InfoExtractor):
159     _VALID_URL = r'https?://(?:www\.)?puhutv\.com/(?P<id>[a-z0-9-]+)-detay'
160     IE_NAME = 'puhutv:serie'
161     _TESTS = [
162         {
163             'url': 'https://puhutv.com/deniz-yildizi-detay',
164             'info_dict': {
165                 'title': 'Deniz Yıldızı',
166                 'id': 'deniz-yildizi',
167                 'uploader': 'Focus Film',
168                 'uploader_id': 61,
169             },
170             'playlist_mincount': 234,
171         },
172         {
173             # a film detail page which is using same url with serie page
174             'url': 'https://puhutv.com/kaybedenler-kulubu-detay',
175             'info_dict': {
176                 'title': 'Kaybedenler Kulübü',
177                 'id': 'kaybedenler-kulubu',
178                 'uploader': 'Tolga Örnek, Murat Dörtbudak, Neslihan Dörtbudak, Kemal Kaplanoğlu',
179                 'uploader_id': 248,
180             },
181             'playlist_mincount': 1,
182         },
183     ]
184
185     def _extract_entries(self, playlist_id, seasons):
186         for season in seasons:
187             season_id = season['id']
188             season_number = season.get('position')
189             pagenum = 1
190             has_more = True
191             while has_more is True:
192                 season_info = self._download_json(
193                     'https://galadriel.puhutv.com/seasons/%s' % season_id,
194                     playlist_id, 'Downloading season %s page %s' % (season_number, pagenum), query={
195                         'page': pagenum,
196                         'per': 40,
197                     })
198                 for episode in season_info.get('episodes'):
199                     video_id = episode['slugPath'].replace('-izle', '')
200                     yield self.url_result(
201                         'https://puhutv.com/%s-izle' % video_id,
202                         PuhuTVIE.ie_key(), video_id)
203                 pagenum = pagenum + 1
204                 has_more = season_info.get('hasMore', False)
205
206     def _real_extract(self, url):
207         playlist_id = self._match_id(url)
208
209         info = self._download_json(
210             'https://puhutv.com/api/slug/%s-detay' % playlist_id, playlist_id)['data']
211
212         title = info.get('name')
213         uploader = try_get(info, lambda x: x['producer']['name'], compat_str)
214         uploader_id = try_get(info, lambda x: x['producer']['id'])
215         seasons = info.get('seasons')
216         if seasons:
217             entries = self._extract_entries(playlist_id, seasons)
218         else:
219             # For films, these are using same url with series
220             video_id = info['assets'][0]['slug']
221             return self.url_result(
222                 'https://puhutv.com/%s-izle' % video_id,
223                 PuhuTVIE.ie_key(), video_id)
224
225         return {
226             '_type': 'playlist',
227             'id': playlist_id,
228             'title': title,
229             'uploader': uploader,
230             'uploader_id': uploader_id,
231             'entries': entries,
232         }