]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/zee5.py
[ZEE5] Fix extractor.
[yt-dlp.git] / yt_dlp / extractor / zee5.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9 ExtractorError,
10 int_or_none,
11 parse_age_limit,
12 str_or_none,
13 try_get,
14 unified_strdate,
15 unified_timestamp,
16 url_or_none,
17 )
18
19
20 class Zee5IE(InfoExtractor):
21 _VALID_URL = r'''(?x)
22 (?:
23 zee5:|
24 https?://(?:www\.)?zee5\.com/(?:[^#?]+/)?
25 (?:
26 (?:tv-shows|kids|web-series|zee5originals)(?:/[^#/?]+){3}
27 |movies/[^#/?]+
28 )/(?P<display_id>[^#/?]+)/
29 )
30 (?P<id>[^#/?]+)/?(?:$|[?#])
31 '''
32 _TESTS = [{
33 'url': 'https://www.zee5.com/movies/details/krishna-the-birth/0-0-63098',
34 'info_dict': {
35 'id': '0-0-63098',
36 'ext': 'mp4',
37 'display_id': 'krishna-the-birth',
38 'title': 'Krishna - The Birth',
39 'duration': 4368,
40 'description': compat_str,
41 'alt_title': 'Krishna - The Birth',
42 'uploader': 'Zee Entertainment Enterprises Ltd',
43 'release_date': '20060101',
44 'upload_date': '20060101',
45 'timestamp': 1136073600,
46 'thumbnail': r're:^https?://.*\.jpg$',
47 'episode_number': 0,
48 'episode': 'Episode 0',
49 'tags': list
50 },
51 'params': {
52 'format': 'bv',
53 },
54 }, {
55 'url': 'https://www.zee5.com/kids/kids-shows/bandbudh-aur-budbak/0-6-1899/yoga-se-hoga-bandbudh-aur-budbak/0-1-239839',
56 'info_dict': {
57 'id': '0-1-239839',
58 'ext': 'mp4',
59 'display_id': 'yoga-se-hoga-bandbudh-aur-budbak',
60 'title': 'Yoga Se Hoga-Bandbudh aur Budbak',
61 'duration': 659,
62 'description': compat_str,
63 'alt_title': 'Yoga Se Hoga-Bandbudh aur Budbak',
64 'uploader': 'Zee Entertainment Enterprises Ltd',
65 'release_date': '20150101',
66 'upload_date': '20150101',
67 'timestamp': 1420070400,
68 'thumbnail': r're:^https?://.*\.jpg$',
69 'series': 'Bandbudh Aur Budbak',
70 'season_number': 1,
71 'episode_number': 1,
72 'episode': 'Episode 1',
73 'season': 'Season 1',
74 'tags': list,
75 },
76 'params': {
77 'format': 'bv',
78 },
79 }, {
80 'url': 'https://www.zee5.com/hi/tv-shows/details/kundali-bhagya/0-6-366/kundali-bhagya-march-08-2021/0-1-manual_7g9jv1os7730?country=IN',
81 'only_matching': True
82 }, {
83 'url': 'https://www.zee5.com/global/hi/tv-shows/details/kundali-bhagya/0-6-366/kundali-bhagya-march-08-2021/0-1-manual_7g9jv1os7730',
84 'only_matching': True
85 }, {
86 'url': 'https://www.zee5.com/web-series/details/mithya/0-6-4z587408/maine-dekhi-hai-uski-mrityu/0-1-6z587412',
87 'only_matching': True
88 }]
89 _DETAIL_API_URL = 'https://spapi.zee5.com/singlePlayback/getDetails/secure?content_id={}&device_id={}&platform_name=desktop_web&country=IN&check_parental_control=false'
90 _DEVICE_ID = 'TszZPYPuY9Pq2cJizV0U000000000000'
91 _USER_TOKEN = None
92 _LOGIN_HINT = 'Use "--username <mobile_number>" to login using otp or "--username token" and "--password <user_token>" to login using user token.'
93 _NETRC_MACHINE = 'zee5'
94 _GEO_COUNTRIES = ['IN']
95
96 def _perform_login(self, username, password):
97 if len(username) == 10 and username.isdigit() and self._USER_TOKEN is None:
98 self.report_login()
99 otp_request_json = self._download_json('https://b2bapi.zee5.com/device/sendotp_v1.php?phoneno=91{}'.format(username),
100 None, note='Sending OTP')
101 if otp_request_json['code'] == 0:
102 self.to_screen(otp_request_json['message'])
103 else:
104 raise ExtractorError(otp_request_json['message'], expected=True)
105 otp_code = self._get_tfa_info('OTP')
106 otp_verify_json = self._download_json('https://b2bapi.zee5.com/device/verifyotp_v1.php?phoneno=91{}&otp={}&guest_token={}&platform=web'.format(username, otp_code, self._DEVICE_ID),
107 None, note='Verifying OTP', fatal=False)
108 if not otp_verify_json:
109 raise ExtractorError('Unable to verify OTP.', expected=True)
110 self._USER_TOKEN = otp_verify_json.get('token')
111 if not self._USER_TOKEN:
112 raise ExtractorError(otp_request_json['message'], expected=True)
113 elif username.lower() == 'token' and len(password) > 1198:
114 self._USER_TOKEN = password
115 else:
116 raise ExtractorError(self._LOGIN_HINT, expected=True)
117
118 def _real_extract(self, url):
119 video_id, display_id = self._match_valid_url(url).group('id', 'display_id')
120 access_token_request = self._download_json(
121 'https://useraction.zee5.com/token/platform_tokens.php?platform_name=web_app',
122 video_id, note='Downloading access token')
123 data = {
124 'x-access-token': access_token_request['token']
125 }
126 if self._USER_TOKEN:
127 data['Authorization'] = 'bearer %s' % self._USER_TOKEN
128 else:
129 data['X-Z5-Guest-Token'] = self._DEVICE_ID
130
131 json_data = self._download_json(
132 self._DETAIL_API_URL.format(video_id, self._DEVICE_ID),
133 video_id, headers={'content-type': 'application/json'}, data=json.dumps(data).encode('utf-8'))
134 asset_data = json_data['assetDetails']
135 show_data = json_data.get('showDetails', {})
136 if 'premium' in asset_data['business_type']:
137 raise ExtractorError('Premium content is DRM protected.', expected=True)
138 if not asset_data.get('hls_url'):
139 self.raise_login_required(self._LOGIN_HINT, metadata_available=True, method=None)
140 formats, m3u8_subs = self._extract_m3u8_formats_and_subtitles(asset_data['hls_url'], video_id, 'mp4', fatal=False)
141 self._sort_formats(formats)
142
143 subtitles = {}
144 for sub in asset_data.get('subtitle_url', []):
145 sub_url = sub.get('url')
146 if not sub_url:
147 continue
148 subtitles.setdefault(sub.get('language', 'en'), []).append({
149 'url': self._proto_relative_url(sub_url),
150 })
151 subtitles = self._merge_subtitles(subtitles, m3u8_subs)
152 return {
153 'id': video_id,
154 'display_id': display_id,
155 'title': asset_data['title'],
156 'formats': formats,
157 'subtitles': subtitles,
158 'duration': int_or_none(asset_data.get('duration')),
159 'description': str_or_none(asset_data.get('description')),
160 'alt_title': str_or_none(asset_data.get('original_title')),
161 'uploader': str_or_none(asset_data.get('content_owner')),
162 'age_limit': parse_age_limit(asset_data.get('age_rating')),
163 'release_date': unified_strdate(asset_data.get('release_date')),
164 'timestamp': unified_timestamp(asset_data.get('release_date')),
165 'thumbnail': url_or_none(asset_data.get('image_url')),
166 'series': str_or_none(asset_data.get('tvshow_name')),
167 'season': try_get(show_data, lambda x: x['seasons']['title'], str),
168 'season_number': int_or_none(try_get(show_data, lambda x: x['seasons'][0]['orderid'])),
169 'episode_number': int_or_none(try_get(asset_data, lambda x: x['orderid'])),
170 'tags': try_get(asset_data, lambda x: x['tags'], list)
171 }
172
173
174 class Zee5SeriesIE(InfoExtractor):
175 IE_NAME = 'zee5:series'
176 _VALID_URL = r'''(?x)
177 (?:
178 zee5:series:|
179 https?://(?:www\.)?zee5\.com/(?:[^#?]+/)?
180 (?:tv-shows|web-series|kids|zee5originals)(?:/[^#/?]+){2}/
181 )
182 (?P<id>[^#/?]+)(?:/episodes)?/?(?:$|[?#])
183 '''
184 _TESTS = [{
185 'url': 'https://www.zee5.com/kids/kids-shows/bandbudh-aur-budbak/0-6-1899',
186 'playlist_mincount': 156,
187 'info_dict': {
188 'id': '0-6-1899',
189 },
190 }, {
191 'url': 'https://www.zee5.com/tv-shows/details/bhabi-ji-ghar-par-hai/0-6-199',
192 'playlist_mincount': 1500,
193 'info_dict': {
194 'id': '0-6-199',
195 },
196 }, {
197 'url': 'https://www.zee5.com/tv-shows/details/agent-raghav-crime-branch/0-6-965',
198 'playlist_mincount': 24,
199 'info_dict': {
200 'id': '0-6-965',
201 },
202 }, {
203 'url': 'https://www.zee5.com/ta/tv-shows/details/nagabhairavi/0-6-3201',
204 'playlist_mincount': 3,
205 'info_dict': {
206 'id': '0-6-3201',
207 },
208 }, {
209 'url': 'https://www.zee5.com/global/hi/tv-shows/details/khwaabon-ki-zamin-par/0-6-270',
210 'playlist_mincount': 150,
211 'info_dict': {
212 'id': '0-6-270',
213 },
214 }, {
215 'url': 'https://www.zee5.com/tv-shows/details/chala-hawa-yeu-dya-ladies-zindabaad/0-6-2943/episodes',
216 'only_matching': True,
217 }, {
218 'url': 'https://www.zee5.com/web-series/details/mithya/0-6-4z587408',
219 'only_matching': True,
220 }]
221
222 def _entries(self, show_id):
223 access_token_request = self._download_json(
224 'https://useraction.zee5.com/token/platform_tokens.php?platform_name=web_app',
225 show_id, note='Downloading access token')
226 headers = {
227 'X-Access-Token': access_token_request['token'],
228 'Referer': 'https://www.zee5.com/',
229 }
230 show_url = 'https://gwapi.zee5.com/content/tvshow/{}?translation=en&country=IN'.format(show_id)
231
232 page_num = 0
233 show_json = self._download_json(show_url, video_id=show_id, headers=headers)
234 for season in show_json.get('seasons') or []:
235 season_id = try_get(season, lambda x: x['id'], compat_str)
236 next_url = 'https://gwapi.zee5.com/content/tvshow/?season_id={}&type=episode&translation=en&country=IN&on_air=false&asset_subtype=tvshow&page=1&limit=100'.format(season_id)
237 while next_url:
238 page_num += 1
239 episodes_json = self._download_json(
240 next_url, video_id=show_id, headers=headers,
241 note='Downloading JSON metadata page %d' % page_num)
242 for episode in try_get(episodes_json, lambda x: x['episode'], list) or []:
243 video_id = episode.get('id')
244 yield self.url_result(
245 'zee5:%s' % video_id,
246 ie=Zee5IE.ie_key(), video_id=video_id)
247 next_url = url_or_none(episodes_json.get('next_episode_api'))
248
249 def _real_extract(self, url):
250 show_id = self._match_id(url)
251 return self.playlist_result(self._entries(show_id), playlist_id=show_id)