aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/puhutv.py
blob: 8abdab52a49f6867bca13463004063f6c2ecb942 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# coding: utf-8
from __future__ import unicode_literals

from .common import InfoExtractor
from ..compat import compat_str
from ..utils import (
    int_or_none,
    float_or_none,
    determine_ext,
    str_or_none,
    url_or_none,
    unified_strdate,
    unified_timestamp,
    try_get,
    url_basename,
    remove_end
)


class PuhuTVIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?puhutv\.com/(?P<id>[a-z0-9-]+)-izle'
    IE_NAME = 'puhutv'
    _TESTS = [
        {
            # A Film
            'url': 'https://puhutv.com/sut-kardesler-izle',
            'md5': 'a347470371d56e1585d1b2c8dab01c96',
            'info_dict': {
                'id': 'sut-kardesler',
                'display_id': '5085',
                'ext': 'mp4',
                'title': 'Süt Kardeşler',
                'thumbnail': r're:^https?://.*\.jpg$',
                'uploader': 'Arzu Film',
                'description': 'md5:405fd024df916ca16731114eb18e511a',
                'uploader_id': '43',
                'upload_date': '20160729',
                'timestamp': int,
            },
        },
        {
            # An Episode and geo restricted
            'url': 'https://puhutv.com/jet-sosyete-1-bolum-izle',
            'only_matching': True,
        },
        {
            # Has subtitle
            'url': 'https://puhutv.com/dip-1-bolum-izle',
            'only_matching': True,
        }
    ]
    _SUBTITLE_LANGS = {
        'English': 'en',
        'Deutsch': 'de',
        'عربى': 'ar'
    }

    def _real_extract(self, url):
        video_id = self._match_id(url)
        info = self._download_json(
            'https://puhutv.com/api/slug/%s-izle' % video_id, video_id)['data']

        display_id = compat_str(info['id'])
        title = info['title']['name']
        if info.get('display_name'):
            title = '%s %s' % (title, info.get('display_name'))

        description = try_get(info, lambda x: x['title']['description'], compat_str) or info.get('description')
        timestamp = unified_timestamp(info.get('created_at'))
        upload_date = unified_strdate(info.get('created_at'))
        uploader = try_get(info, lambda x: x['title']['producer']['name'], compat_str)
        uploader_id = str_or_none(try_get(info, lambda x: x['title']['producer']['id']))
        view_count = int_or_none(try_get(info, lambda x: x['content']['watch_count']))
        duration = float_or_none(try_get(info, lambda x: x['content']['duration_in_ms']), scale=1000)
        thumbnail = try_get(info, lambda x: x['content']['images']['wide']['main'], compat_str)
        release_year = int_or_none(try_get(info, lambda x: x['title']['released_at']))
        webpage_url = info.get('web_url')

        season_number = int_or_none(info.get('season_number'))
        season_id = int_or_none(info.get('season_id'))
        episode_number = int_or_none(info.get('episode_number'))

        tags = []
        for tag in try_get(info, lambda x: x['title']['genres'], list) or []:
            if isinstance(tag.get('name'), compat_str):
                tags.append(tag.get('name'))

        thumbnails = []
        thumbs_dict = try_get(info, lambda x: x['content']['images']['wide'], dict) or {}
        for id, url in thumbs_dict.items():
            if not url_or_none(url):
                continue
            thumbnails.append({
                'url': 'https://%s' % url,
                'id': id
            })

        subtitles = {}
        for subtitle in try_get(info, lambda x: x['content']['subtitles'], list) or []:
            if not isinstance(subtitle, dict):
                continue
            lang = subtitle.get('language')
            sub_url = url_or_none(subtitle.get('url'))
            if not lang or not isinstance(lang, compat_str) or not sub_url:
                continue
            subtitles[self._SUBTITLE_LANGS.get(lang, lang)] = [{
                'url': sub_url
            }]

        # Some of videos are geo restricted upon request copyright owner and returns 403
        req_formats = self._download_json(
            'https://puhutv.com/api/assets/%s/videos' % display_id,
            video_id, 'Downloading video JSON')

        formats = []
        for format in req_formats['data']['videos']:
            media_url = url_or_none(format.get('url'))
            if not media_url:
                continue
            ext = format.get('video_format') or determine_ext(media_url)
            quality = format.get('quality')
            if format.get('stream_type') == 'hls' and format.get('is_playlist') is True:
                m3u8_id = remove_end(url_basename(media_url), '.m3u8')
                formats.append(self._m3u8_meta_format(media_url, ext, m3u8_id=m3u8_id))
            elif ext == 'mp4' and format.get('is_playlist', False) is False:
                formats.append({
                    'url': media_url,
                    'format_id': 'http-%s' % quality,
                    'ext': ext,
                    'height': quality
                })
        self._sort_formats(formats)

        return {
            'id': video_id,
            'display_id': display_id,
            'title': title,
            'description': description,
            'season_id': season_id,
            'season_number': season_number,
            'episode_number': episode_number,
            'release_year': release_year,
            'upload_date': upload_date,
            'timestamp': timestamp,
            'uploader': uploader,
            'uploader_id': uploader_id,
            'view_count': view_count,
            'duration': duration,
            'tags': tags,
            'subtitles': subtitles,
            'webpage_url': webpage_url,
            'thumbnail': thumbnail,
            'thumbnails': thumbnails,
            'formats': formats
        }


class PuhuTVSerieIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?puhutv\.com/(?P<id>[a-z0-9-]+)-detay'
    IE_NAME = 'puhutv:serie'
    _TESTS = [
        {
            'url': 'https://puhutv.com/deniz-yildizi-detay',
            'info_dict': {
                'title': 'Deniz Yıldızı',
                'id': 'deniz-yildizi',
                'uploader': 'Focus Film',
                'uploader_id': 61,
            },
            'playlist_mincount': 234,
        },
        {
            # a film detail page which is using same url with serie page
            'url': 'https://puhutv.com/kaybedenler-kulubu-detay',
            'info_dict': {
                'title': 'Kaybedenler Kulübü',
                'id': 'kaybedenler-kulubu',
                'uploader': 'Tolga Örnek, Murat Dörtbudak, Neslihan Dörtbudak, Kemal Kaplanoğlu',
                'uploader_id': 248,
            },
            'playlist_mincount': 1,
        },
    ]

    def _extract_entries(self, playlist_id, seasons):
        for season in seasons:
            season_id = season['id']
            season_number = season.get('position')
            pagenum = 1
            has_more = True
            while has_more is True:
                season_info = self._download_json(
                    'https://galadriel.puhutv.com/seasons/%s' % season_id,
                    playlist_id, 'Downloading season %s page %s' % (season_number, pagenum), query={
                        'page': pagenum,
                        'per': 40,
                    })
                for episode in season_info.get('episodes'):
                    video_id = episode['slugPath'].replace('-izle', '')
                    yield self.url_result(
                        'https://puhutv.com/%s-izle' % video_id,
                        PuhuTVIE.ie_key(), video_id)
                pagenum = pagenum + 1
                has_more = season_info.get('hasMore', False)

    def _real_extract(self, url):
        playlist_id = self._match_id(url)

        info = self._download_json(
            'https://puhutv.com/api/slug/%s-detay' % playlist_id, playlist_id)['data']

        title = info.get('name')
        uploader = try_get(info, lambda x: x['producer']['name'], compat_str)
        uploader_id = try_get(info, lambda x: x['producer']['id'])
        seasons = info.get('seasons')
        if seasons:
            entries = self._extract_entries(playlist_id, seasons)
        else:
            # For films, these are using same url with series
            video_id = info['assets'][0]['slug']
            return self.url_result(
                'https://puhutv.com/%s-izle' % video_id,
                PuhuTVIE.ie_key(), video_id)

        return {
            '_type': 'playlist',
            'id': playlist_id,
            'title': title,
            'uploader': uploader,
            'uploader_id': uploader_id,
            'entries': entries,
        }