6 from .common
import InfoExtractor
7 from ..networking
.exceptions
import HTTPError
8 from ..utils
import ExtractorError
, int_or_none
, join_nonempty
, url_or_none
9 from ..utils
.traversal
import traverse_obj
12 class DangalPlayBaseIE(InfoExtractor
):
13 _NETRC_MACHINE
= 'dangalplay'
15 _LOGIN_HINT
= 'Pass credentials as -u "token" -p "USER_ID" where USER_ID is the `otv_user_id` in browser local storage'
16 _API_BASE
= 'https://ottapi.dangalplay.com'
17 _AUTH_TOKEN
= 'jqeGWxRKK7FK5zEk3xCM' # from https://www.dangalplay.com/main.48ad19e24eb46acccef3.js
18 _SECRET_KEY
= 'f53d31a4377e4ef31fa0' # same as above
20 def _perform_login(self
, username
, password
):
23 if username
!= 'token' or not re
.fullmatch(r
'[\da-f]{32}', password
):
24 raise ExtractorError(self
._LOGIN
_HINT
, expected
=True)
25 self
._OTV
_USER
_ID
= password
27 def _real_initialize(self
):
28 if not self
._OTV
_USER
_ID
:
29 self
.raise_login_required(f
'Login required. {self._LOGIN_HINT}', method
=None)
31 def _extract_episode_info(self
, metadata
, episode_slug
, series_slug
):
33 'display_id': episode_slug
,
34 'episode_number': int_or_none(self
._search
_regex
(
35 r
'ep-(?:number-)?(\d+)', episode_slug
, 'episode number', default
=None)),
36 'season_number': int_or_none(self
._search
_regex
(
37 r
'season-(\d+)', series_slug
, 'season number', default
='1')),
38 'series': series_slug
,
39 **traverse_obj(metadata
, {
40 'id': ('content_id', {str}
),
41 'title': ('display_title', {str}
),
42 'episode': ('title', {str}
),
43 'series': ('show_name', {str}
, {lambda x: x or None}
),
44 'series_id': ('catalog_id', {str}
),
45 'duration': ('duration', {int_or_none}
),
46 'release_timestamp': ('release_date_uts', {int_or_none}
),
50 def _call_api(self
, path
, display_id
, note
='Downloading JSON metadata', fatal
=True, query
={}):
51 return self
._download
_json
(
52 f
'{self._API_BASE}/{path}', display_id
, note
, fatal
=fatal
,
53 headers
={'Accept': 'application/json'}
, query
={
54 'auth_token': self
._AUTH
_TOKEN
,
60 class DangalPlayIE(DangalPlayBaseIE
):
61 IE_NAME
= 'dangalplay'
62 _VALID_URL
= r
'https?://(?:www\.)?dangalplay.com/shows/(?P<series>[^/?#]+)/(?P<id>(?!episodes)[^/?#]+)/?(?:$|[?#])'
64 'url': 'https://www.dangalplay.com/shows/kitani-mohabbat-hai-season-2/kitani-mohabbat-hai-season-2-ep-number-01',
66 'id': '647c61dc1e7171310dcd49b4',
68 'release_timestamp': 1262304000,
70 'episode': 'EP 1 | KITANI MOHABBAT HAI SEASON 2',
71 'series': 'kitani-mohabbat-hai-season-2',
73 'title': 'EP 1 | KITANI MOHABBAT HAI SEASON 2',
74 'release_date': '20100101',
77 'display_id': 'kitani-mohabbat-hai-season-2-ep-number-01',
78 'series_id': '645c9ea41e717158ca574966',
81 'url': 'https://www.dangalplay.com/shows/milke-bhi-hum-na-mile/milke-bhi-hum-na-mile-ep-number-01',
83 'id': '65d31d9ba73b9c3abd14a7f3',
85 'episode': 'EP 1 | MILKE BHI HUM NA MILE',
86 'release_timestamp': 1708367411,
89 'title': 'EP 1 | MILKE BHI HUM NA MILE',
91 'release_date': '20240219',
93 'series': 'MILKE BHI HUM NA MILE',
94 'series_id': '645c9ea41e717158ca574966',
95 'display_id': 'milke-bhi-hum-na-mile-ep-number-01',
99 def _generate_api_data(self
, data
):
100 catalog_id
= data
['catalog_id']
101 content_id
= data
['content_id']
102 timestamp
= str(int(time
.time()))
103 unhashed
= ''.join((catalog_id
, content_id
, self
._OTV
_USER
_ID
, timestamp
, self
._SECRET
_KEY
))
106 'catalog_id': catalog_id
,
107 'content_id': content_id
,
110 'auth_token': self
._AUTH
_TOKEN
,
111 'id': self
._OTV
_USER
_ID
,
112 'md5': hashlib
.md5(unhashed
.encode()).hexdigest(),
114 }, separators
=(',', ':')).encode()
116 def _real_extract(self
, url
):
117 series_slug
, episode_slug
= self
._match
_valid
_url
(url
).group('series', 'id')
118 metadata
= self
._call
_api
(
119 f
'catalogs/shows/{series_slug}/episodes/{episode_slug}.gzip',
120 episode_slug
, query
={'item_language': ''}
)['data']
123 details
= self
._download
_json
(
124 f
'{self._API_BASE}/v2/users/get_all_details.gzip', episode_slug
,
125 'Downloading playback details JSON', headers
={
126 'Accept': 'application/json',
127 'Content-Type': 'application/json',
128 }, data
=self
._generate
_api
_data
(metadata
))['data']
129 except ExtractorError
as e
:
130 if isinstance(e
.cause
, HTTPError
) and e
.cause
.status
== 422:
131 error_info
= traverse_obj(e
.cause
.response
.read().decode(), ({json.loads}
, 'error', {dict}
)) or {}
132 if error_info
.get('code') == '1016':
133 self
.raise_login_required(
134 f
'Your token has expired or is invalid. {self._LOGIN_HINT}', method
=None)
135 elif msg
:= error_info
.get('message'):
136 raise ExtractorError(msg
)
139 m3u8_url
= traverse_obj(details
, (
140 ('adaptive_url', ('adaptive_urls', 'hd', 'hls', ..., 'playback_url')), {url_or_none}
, any
))
141 formats
, subtitles
= self
._extract
_m
3u8_formats
_and
_subtitles
(m3u8_url
, episode_slug
, 'mp4')
145 'subtitles': subtitles
,
146 **self
._extract
_episode
_info
(metadata
, episode_slug
, series_slug
),
150 class DangalPlaySeasonIE(DangalPlayBaseIE
):
151 IE_NAME
= 'dangalplay:season'
152 _VALID_URL
= r
'https?://(?:www\.)?dangalplay.com/shows/(?P<id>[^/?#]+)(?:/(?P<sub>ep-[^/?#]+)/episodes)?/?(?:$|[?#])'
154 'url': 'https://www.dangalplay.com/shows/kitani-mohabbat-hai-season-1',
155 'playlist_mincount': 170,
157 'id': 'kitani-mohabbat-hai-season-1',
160 'url': 'https://www.dangalplay.com/shows/kitani-mohabbat-hai-season-1/ep-01-30-1/episodes',
161 'playlist_count': 30,
163 'id': 'kitani-mohabbat-hai-season-1-ep-01-30-1',
166 # 1 season only, series page is season page
167 'url': 'https://www.dangalplay.com/shows/milke-bhi-hum-na-mile',
168 'playlist_mincount': 15,
170 'id': 'milke-bhi-hum-na-mile',
174 def _entries(self
, subcategories
, series_slug
):
175 for subcategory
in subcategories
:
176 data
= self
._call
_api
(
177 f
'catalogs/shows/items/{series_slug}/subcategories/{subcategory}/episodes.gzip',
178 series_slug
, f
'Downloading episodes JSON for {subcategory}', fatal
=False, query
={
180 'status': 'published',
182 for ep
in traverse_obj(data
, ('data', 'items', lambda _
, v
: v
['friendly_id'])):
183 episode_slug
= ep
['friendly_id']
184 yield self
.url_result(
185 f
'https://www.dangalplay.com/shows/{series_slug}/{episode_slug}',
186 DangalPlayIE
, **self
._extract
_episode
_info
(ep
, episode_slug
, series_slug
))
188 def _real_extract(self
, url
):
189 series_slug
, subcategory
= self
._match
_valid
_url
(url
).group('id', 'sub')
190 subcategories
= [subcategory
] if subcategory
else traverse_obj(
192 f
'catalogs/shows/items/{series_slug}.gzip', series_slug
,
193 'Downloading season info JSON', query
={'item_language': ''}
),
194 ('data', 'subcategories', ..., 'friendly_id', {str}
))
196 return self
.playlist_result(
197 self
._entries
(subcategories
, series_slug
), join_nonempty(series_slug
, subcategory
))