Spaces:
Running
Running
Upload cinemar.js
Browse files- cinemar.js +33 -1
cinemar.js
CHANGED
|
@@ -194,6 +194,7 @@ function parseCinemarData(html) {
|
|
| 194 |
if (!buf || buf.length < 20) return null;
|
| 195 |
|
| 196 |
const isSerial = buf.indexOf(FOLDER_MARKER_BUF) >= 0;
|
|
|
|
| 197 |
|
| 198 |
if (!isSerial) {
|
| 199 |
try {
|
|
@@ -240,12 +241,17 @@ function parseCinemarData(html) {
|
|
| 240 |
allEpPositions.push({ id: fullId, seasonId: seasonMatch[1], epNum: parseInt(seasonMatch[2], 10), title: t ? t.str : ('小械褉懈褟 ' + seasonMatch[2]), pos });
|
| 241 |
});
|
| 242 |
|
|
|
|
|
|
|
| 243 |
const seasonMap = {};
|
| 244 |
allEpPositions.forEach(ep => {
|
| 245 |
if (!seasonMap[ep.seasonId]) seasonMap[ep.seasonId] = [];
|
| 246 |
if (!seasonMap[ep.seasonId].find(e => e.id === ep.id)) seasonMap[ep.seasonId].push(ep);
|
| 247 |
});
|
| 248 |
const seasonIds = Object.keys(seasonMap).sort((a, b) => parseInt(a.substring(1)) - parseInt(b.substring(1)));
|
|
|
|
|
|
|
|
|
|
| 249 |
if (!seasonIds.length) return null;
|
| 250 |
|
| 251 |
const episodes = {};
|
|
@@ -369,7 +375,11 @@ async function getCinemarStream(pageUrl) {
|
|
| 369 |
if (embedHtml && embedHtml.includes('"file"') && embedHtml.includes('W3s')) {
|
| 370 |
console.log('[cinemar] Puppeteer success on attempt', attempts);
|
| 371 |
firstParsed = parseCinemarData(embedHtml);
|
| 372 |
-
if (firstParsed)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
}
|
| 374 |
|
| 375 |
console.log('[cinemar] Puppeteer attempt', attempts, 'failed, no file data');
|
|
@@ -389,6 +399,28 @@ async function getCinemarStream(pageUrl) {
|
|
| 389 |
throw new Error('no file data after worker and Puppeteer attempts (Cloudflare blocked?)');
|
| 390 |
}
|
| 391 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
|
| 393 |
// Step 3: build response
|
| 394 |
if (!firstParsed.isSerial) {
|
|
|
|
| 194 |
if (!buf || buf.length < 20) return null;
|
| 195 |
|
| 196 |
const isSerial = buf.indexOf(FOLDER_MARKER_BUF) >= 0;
|
| 197 |
+
console.log('[cinemar/parse] buf length:', buf.length, 'isSerial:', isSerial);
|
| 198 |
|
| 199 |
if (!isSerial) {
|
| 200 |
try {
|
|
|
|
| 241 |
allEpPositions.push({ id: fullId, seasonId: seasonMatch[1], epNum: parseInt(seasonMatch[2], 10), title: t ? t.str : ('小械褉懈褟 ' + seasonMatch[2]), pos });
|
| 242 |
});
|
| 243 |
|
| 244 |
+
console.log('[cinemar/parse] found', allEpPositions.length, 'episode positions');
|
| 245 |
+
|
| 246 |
const seasonMap = {};
|
| 247 |
allEpPositions.forEach(ep => {
|
| 248 |
if (!seasonMap[ep.seasonId]) seasonMap[ep.seasonId] = [];
|
| 249 |
if (!seasonMap[ep.seasonId].find(e => e.id === ep.id)) seasonMap[ep.seasonId].push(ep);
|
| 250 |
});
|
| 251 |
const seasonIds = Object.keys(seasonMap).sort((a, b) => parseInt(a.substring(1)) - parseInt(b.substring(1)));
|
| 252 |
+
|
| 253 |
+
console.log('[cinemar/parse] seasonIds:', seasonIds.length, seasonIds.slice(0, 3));
|
| 254 |
+
|
| 255 |
if (!seasonIds.length) return null;
|
| 256 |
|
| 257 |
const episodes = {};
|
|
|
|
| 375 |
if (embedHtml && embedHtml.includes('"file"') && embedHtml.includes('W3s')) {
|
| 376 |
console.log('[cinemar] Puppeteer success on attempt', attempts);
|
| 377 |
firstParsed = parseCinemarData(embedHtml);
|
| 378 |
+
if (firstParsed) {
|
| 379 |
+
// Add Puppeteer data to merged episodes for serials
|
| 380 |
+
addEpisodes(firstParsed);
|
| 381 |
+
break;
|
| 382 |
+
}
|
| 383 |
}
|
| 384 |
|
| 385 |
console.log('[cinemar] Puppeteer attempt', attempts, 'failed, no file data');
|
|
|
|
| 399 |
throw new Error('no file data after worker and Puppeteer attempts (Cloudflare blocked?)');
|
| 400 |
}
|
| 401 |
}
|
| 402 |
+
|
| 403 |
+
// If serial and we have Puppeteer data, try to get more episodes via Puppeteer
|
| 404 |
+
if (firstParsed && firstParsed.isSerial && Object.keys(merged).length > 0) {
|
| 405 |
+
console.log('[cinemar] serial detected, fetching more episodes via Puppeteer...');
|
| 406 |
+
const PUPBATCH = 3;
|
| 407 |
+
for (let pb = 0; pb < PUPBATCH; pb++) {
|
| 408 |
+
const prevCount = Object.keys(merged).length;
|
| 409 |
+
try {
|
| 410 |
+
const moreHtml = await getEmbedHtmlViaPuppeteer(embedUrl);
|
| 411 |
+
if (moreHtml && moreHtml.includes('"file"')) {
|
| 412 |
+
const moreParsed = parseCinemarData(moreHtml);
|
| 413 |
+
if (moreParsed) addEpisodes(moreParsed);
|
| 414 |
+
}
|
| 415 |
+
} catch(e) {
|
| 416 |
+
console.log('[cinemar] Puppeteer batch', pb+1, 'error:', e.message);
|
| 417 |
+
}
|
| 418 |
+
const newCount = Object.keys(merged).length;
|
| 419 |
+
console.log(`[cinemar] Puppeteer batch ${pb+1}: ${newCount} eps (+${newCount - prevCount})`);
|
| 420 |
+
if (newCount === prevCount) break;
|
| 421 |
+
await new Promise(r => setTimeout(r, 500));
|
| 422 |
+
}
|
| 423 |
+
}
|
| 424 |
|
| 425 |
// Step 3: build response
|
| 426 |
if (!firstParsed.isSerial) {
|