-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDramaboxApi.php
More file actions
204 lines (178 loc) · 6.89 KB
/
Copy pathDramaboxApi.php
File metadata and controls
204 lines (178 loc) · 6.89 KB
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
<?php
require_once __DIR__ . '/DramaboxApp.php';
const DRBX_BEARER = "Bearer token";
const DRBX_DEVICE = "DEVICE";
const DRBX_ANDROID = "ANDROID";
const DRBX_USER_ID = "XXXXXXXX";
const DEFAULT_TYPE_LIST = [
["type" => 1, "value" => ""],
["type" => 2, "value" => ""],
["type" => 4, "value" => ""],
["type" => 4, "value" => ""],
["type" => 5, "value" => "2"],
];
function now_local_time(): string {
$dt = new DateTime("now", new DateTimeZone("Asia/Bangkok"));
return $dt->format("Y-m-d H:i:s.v O");
}
function base_headers(): array {
return [
"version" => "451",
"package-name" => "com.storymatrix.drama",
"p" => "46",
"cid" => "DASEO1000000",
"apn" => "0",
"mchid" => "DBDASEO1000000",
"mbid" => "41000122043",
"mcc" => "310",
"device-id" => DRBX_DEVICE,
"local-time" => now_local_time(),
"time-zone" => "+0700",
"userid" => DRBX_USER_ID,
"android-id" => DRBX_ANDROID,
"tn" => DRBX_BEARER,
"content-type" => "application/json; charset=UTF-8",
"user-agent" => "okhttp/4.10.0",
];
}
function create_signature(array $body): ?string {
$bodyJson = json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$toSign = $bodyJson . DRBX_DEVICE . DRBX_ANDROID . DRBX_BEARER;
return DramaboxApp::dramabox($toSign);
}
/** POST JSON helper */
function http_post_json(string $url, array $body, array $headers, int $timeoutSec = 20): array {
$ch = curl_init($url);
$httpHeaders = [];
foreach ($headers as $k => $v) {
$httpHeaders[] = $k . ': ' . $v;
}
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
CURLOPT_HTTPHEADER => $httpHeaders,
CURLOPT_TIMEOUT => $timeoutSec,
CURLOPT_ENCODING => ''
]);
$resp = curl_exec($ch);
$errno = curl_errno($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($errno) {
throw new RuntimeException("cURL error #$errno");
}
$data = json_decode($resp, true);
if (!is_array($data)) {
throw new RuntimeException("Invalid JSON response");
}
return $data;
}
/** ======================= API ======================= */
function api_listChapters(string $bookId, string $lang = "th"): array {
$body = ["bookId" => $bookId];
$headers = base_headers();
$headers['sn'] = create_signature($body);
$headers['language'] = $lang;
$headers['current-language'] = $lang;
$url = "https://sapi.dramaboxdb.com/drama-box/chapterv2/list";
return http_post_json($url, $body, $headers, 20);
}
function api_batchDownload(string $bookId, array $chapterIdList, string $lang = "th"): array {
$body = ["bookId" => $bookId, "chapterIdList" => array_values($chapterIdList)];
$headers = base_headers();
$headers['sn'] = create_signature($body);
$headers['language'] = $lang;
$headers['current-language'] = $lang;
$url = "https://sapi.dramaboxdb.com/drama-box/chapterv2/batchDownload";
return http_post_json($url, $body, $headers, 20);
}
function api_searchSuggest(string $keyword, string $lang = "th"): array {
$body = ["keyword" => $keyword];
$headers = base_headers();
$headers['sn'] = create_signature($body);
$headers['language'] = $lang;
$headers['current-language'] = $lang;
$url = "https://sapi.dramaboxdb.com/drama-box/search/suggest";
$resp = http_post_json($url, $body, $headers, 20);
return $resp['data']['suggestList'] ?? [];
}
function api_searchByKeyword(string $keyword, int $pageNo = 1, int $pageSize = 20, string $lang = "th"): array {
$body = [
"keyword" => $keyword,
"searchSource" => "搜索按钮",
"pageNo" => $pageNo,
"pageSize" => $pageSize,
"from" => "search_sug",
];
$headers = base_headers();
$headers['sn'] = create_signature($body);
$headers['language'] = $lang;
$headers['current-language'] = $lang;
$url = "https://sapi.dramaboxdb.com/drama-box/search/search";
$resp = http_post_json($url, $body, $headers, 20);
$data = $resp['data'] ?? [];
$rawList = [];
if (!empty($data['searchList']) && is_array($data['searchList'])) {
$rawList = $data['searchList'];
} elseif (!empty($resp['data']['searchList']) && is_array($resp['data']['searchList'])) {
$rawList = $resp['data']['searchList'];
}
$list = [];
foreach ($rawList as $it) {
$list[] = [
"bookId" => strval($it['bookId'] ?? ($it['id'] ?? "")),
"bookName" => $it['bookName'] ?? ($it['title'] ?? ""),
"introduction" => $it['introduction'] ?? "",
"cover" => $it['cover'] ?? ($it['bookCover'] ?? ""),
"corner" => $it['corner'] ?? null,
"playCount" => $it['playCount'] ?? "",
"chapterCount" => $it['chapterCount'] ?? ($it['totalEpisode'] ?? 0),
];
}
$list = array_values(array_filter($list, fn($x) => !empty($x['bookId'])));
$isMore = isset($data['isMore'])
? (intval($data['isMore']) === 1)
: ((intval($data['totalSize'] ?? count($list))) > $pageNo * $pageSize);
return [
"list" => $list,
"isMore"=> $isMore,
"total" => intval($data['totalSize'] ?? count($list)) ?: count($list),
"raw" => $resp,
];
}
function api_NewList(int $pageNo = 1, int $pageSize = 15, array $typeList = DEFAULT_TYPE_LIST, string $lang = "th"): array {
$url = "https://sapi.dramaboxdb.com/drama-box/home/classify";
$data = [
"pageSize" => $pageSize,
"typeList" => array_values($typeList),
"pageNo" => $pageNo,
"showLabels" => false,
];
$headers = base_headers();
$headers['sn'] = create_signature($data);
$headers['language'] = $lang;
$headers['current-language'] = $lang;
$res = http_post_json($url, $data, $headers, 20);
$records = $res['data']['classifyBookList']['records'] ?? [];
$isMore = (int)($res['data']['classifyBookList']['isMore'] ?? 0) === 1;
$outList = [];
foreach ($records as $r) {
$outList[] = [
"bookId" => (string)$r['bookId'],
"bookName" => $r['bookName'] ?? '',
"introduction" => $r['introduction'] ?? '',
"cover" => $r['coverWap'] ?? ($r['cover'] ?? ''),
"chapterCount" => $r['chapterCount'] ?? 0,
"playCount" => $r['playCount'] ?? '',
"corner" => $r['corner'] ?? null,
];
}
return [
"list" => $outList,
"isMore"=> $isMore,
"total" => count($outList),
"raw" => $res,
];
}
?>