]>
Commit | Line | Data |
---|---|---|
1 | import hashlib | |
2 | import json | |
3 | import re | |
4 | import time | |
5 | ||
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 | |
10 | ||
11 | ||
12 | class DangalPlayBaseIE(InfoExtractor): | |
13 | _NETRC_MACHINE = 'dangalplay' | |
14 | _OTV_USER_ID = None | |
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 | |
19 | ||
20 | def _perform_login(self, username, password): | |
21 | if self._OTV_USER_ID: | |
22 | return | |
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 | |
26 | ||
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) | |
30 | ||
31 | def _extract_episode_info(self, metadata, episode_slug, series_slug): | |
32 | return { | |
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}), | |
47 | }), | |
48 | } | |
49 | ||
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, | |
55 | 'region': 'IN', | |
56 | **query, | |
57 | }) | |
58 | ||
59 | ||
60 | class DangalPlayIE(DangalPlayBaseIE): | |
61 | IE_NAME = 'dangalplay' | |
62 | _VALID_URL = r'https?://(?:www\.)?dangalplay.com/shows/(?P<series>[^/?#]+)/(?P<id>(?!episodes)[^/?#]+)/?(?:$|[?#])' | |
63 | _TESTS = [{ | |
64 | 'url': 'https://www.dangalplay.com/shows/kitani-mohabbat-hai-season-2/kitani-mohabbat-hai-season-2-ep-number-01', | |
65 | 'info_dict': { | |
66 | 'id': '647c61dc1e7171310dcd49b4', | |
67 | 'ext': 'mp4', | |
68 | 'release_timestamp': 1262304000, | |
69 | 'episode_number': 1, | |
70 | 'episode': 'EP 1 | KITANI MOHABBAT HAI SEASON 2', | |
71 | 'series': 'kitani-mohabbat-hai-season-2', | |
72 | 'season_number': 2, | |
73 | 'title': 'EP 1 | KITANI MOHABBAT HAI SEASON 2', | |
74 | 'release_date': '20100101', | |
75 | 'duration': 2325, | |
76 | 'season': 'Season 2', | |
77 | 'display_id': 'kitani-mohabbat-hai-season-2-ep-number-01', | |
78 | 'series_id': '645c9ea41e717158ca574966', | |
79 | }, | |
80 | }, { | |
81 | 'url': 'https://www.dangalplay.com/shows/milke-bhi-hum-na-mile/milke-bhi-hum-na-mile-ep-number-01', | |
82 | 'info_dict': { | |
83 | 'id': '65d31d9ba73b9c3abd14a7f3', | |
84 | 'ext': 'mp4', | |
85 | 'episode': 'EP 1 | MILKE BHI HUM NA MILE', | |
86 | 'release_timestamp': 1708367411, | |
87 | 'episode_number': 1, | |
88 | 'season': 'Season 1', | |
89 | 'title': 'EP 1 | MILKE BHI HUM NA MILE', | |
90 | 'duration': 156048, | |
91 | 'release_date': '20240219', | |
92 | 'season_number': 1, | |
93 | 'series': 'MILKE BHI HUM NA MILE', | |
94 | 'series_id': '645c9ea41e717158ca574966', | |
95 | 'display_id': 'milke-bhi-hum-na-mile-ep-number-01', | |
96 | }, | |
97 | }] | |
98 | ||
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)) | |
104 | ||
105 | return json.dumps({ | |
106 | 'catalog_id': catalog_id, | |
107 | 'content_id': content_id, | |
108 | 'category': '', | |
109 | 'region': 'IN', | |
110 | 'auth_token': self._AUTH_TOKEN, | |
111 | 'id': self._OTV_USER_ID, | |
112 | 'md5': hashlib.md5(unhashed.encode()).hexdigest(), | |
113 | 'ts': timestamp, | |
114 | }, separators=(',', ':')).encode() | |
115 | ||
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'] | |
121 | ||
122 | try: | |
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) | |
137 | raise | |
138 | ||
139 | m3u8_url = traverse_obj(details, ( | |
140 | ('adaptive_url', ('adaptive_urls', 'hd', 'hls', ..., 'playback_url')), {url_or_none}, any)) | |
141 | formats, subtitles = self._extract_m3u8_formats_and_subtitles(m3u8_url, episode_slug, 'mp4') | |
142 | ||
143 | return { | |
144 | 'formats': formats, | |
145 | 'subtitles': subtitles, | |
146 | **self._extract_episode_info(metadata, episode_slug, series_slug), | |
147 | } | |
148 | ||
149 | ||
150 | class DangalPlaySeasonIE(DangalPlayBaseIE): | |
151 | IE_NAME = 'dangalplay:season' | |
152 | _VALID_URL = r'https?://(?:www\.)?dangalplay.com/shows/(?P<id>[^/?#]+)(?:/(?P<sub>ep-[^/?#]+)/episodes)?/?(?:$|[?#])' | |
153 | _TESTS = [{ | |
154 | 'url': 'https://www.dangalplay.com/shows/kitani-mohabbat-hai-season-1', | |
155 | 'playlist_mincount': 170, | |
156 | 'info_dict': { | |
157 | 'id': 'kitani-mohabbat-hai-season-1', | |
158 | }, | |
159 | }, { | |
160 | 'url': 'https://www.dangalplay.com/shows/kitani-mohabbat-hai-season-1/ep-01-30-1/episodes', | |
161 | 'playlist_count': 30, | |
162 | 'info_dict': { | |
163 | 'id': 'kitani-mohabbat-hai-season-1-ep-01-30-1', | |
164 | }, | |
165 | }, { | |
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, | |
169 | 'info_dict': { | |
170 | 'id': 'milke-bhi-hum-na-mile', | |
171 | }, | |
172 | }] | |
173 | ||
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={ | |
179 | 'order_by': 'asc', | |
180 | 'status': 'published', | |
181 | }) | |
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)) | |
187 | ||
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( | |
191 | self._call_api( | |
192 | f'catalogs/shows/items/{series_slug}.gzip', series_slug, | |
193 | 'Downloading season info JSON', query={'item_language': ''}), | |
194 | ('data', 'subcategories', ..., 'friendly_id', {str})) | |
195 | ||
196 | return self.playlist_result( | |
197 | self._entries(subcategories, series_slug), join_nonempty(series_slug, subcategory)) |