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