Spaces:
Paused
Paused
Update app/api/movies4u/route.ts
Browse files- app/api/movies4u/route.ts +560 -551
app/api/movies4u/route.ts
CHANGED
|
@@ -1,551 +1,560 @@
|
|
| 1 |
-
import { NextRequest, NextResponse } from 'next/server';
|
| 2 |
-
import { load } from 'cheerio';
|
| 3 |
-
import { getMovies4UUrl } from '@/lib/utils/providers';
|
| 4 |
-
|
| 5 |
-
// Interface for actual download link
|
| 6 |
-
interface ActualDownloadLink {
|
| 7 |
-
url: string;
|
| 8 |
-
label: string;
|
| 9 |
-
}
|
| 10 |
-
|
| 11 |
-
// Interface for download link structure
|
| 12 |
-
interface DownloadLink {
|
| 13 |
-
type: 'episodes' | 'batch';
|
| 14 |
-
url: string;
|
| 15 |
-
label: string;
|
| 16 |
-
batchSize?: string;
|
| 17 |
-
extractedLinks?: ActualDownloadLink[];
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
// Interface for quality options
|
| 21 |
-
interface QualityOption {
|
| 22 |
-
quality: string;
|
| 23 |
-
format: string;
|
| 24 |
-
size: string;
|
| 25 |
-
language: string;
|
| 26 |
-
links: DownloadLink[];
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
// Interface for season structure
|
| 30 |
-
interface Season {
|
| 31 |
-
name: string;
|
| 32 |
-
qualityOptions: QualityOption[];
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
// Interface for the complete content data
|
| 36 |
-
interface ContentData {
|
| 37 |
-
title: string;
|
| 38 |
-
url: string;
|
| 39 |
-
posterUrl?: string;
|
| 40 |
-
seasons: Season[];
|
| 41 |
-
}
|
| 42 |
-
|
| 43 |
-
interface Movies4UItem {
|
| 44 |
-
id: string;
|
| 45 |
-
title: string;
|
| 46 |
-
url: string;
|
| 47 |
-
image: string;
|
| 48 |
-
videoLabel: string;
|
| 49 |
-
hasVideoIcon: boolean;
|
| 50 |
-
altText: string;
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
interface Movies4UResponse {
|
| 54 |
-
success: boolean;
|
| 55 |
-
data?: {
|
| 56 |
-
items: Movies4UItem[];
|
| 57 |
-
pagination?: {
|
| 58 |
-
currentPage: number;
|
| 59 |
-
hasNextPage: boolean;
|
| 60 |
-
};
|
| 61 |
-
};
|
| 62 |
-
error?: string;
|
| 63 |
-
message?: string;
|
| 64 |
-
remainingRequests?: number;
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
-
interface StreamResponse {
|
| 68 |
-
success: boolean;
|
| 69 |
-
data?: ContentData;
|
| 70 |
-
error?: string;
|
| 71 |
-
message?: string;
|
| 72 |
-
remainingRequests?: number;
|
| 73 |
-
seasonCount?: number;
|
| 74 |
-
qualityOptionCount?: number;
|
| 75 |
-
linkCount?: number;
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
// Function to normalize image URLs
|
| 79 |
-
async function
|
| 80 |
-
if (!url) return undefined;
|
| 81 |
-
if (url.startsWith('//')) return 'https:' + url;
|
| 82 |
-
if (url.startsWith('/')) {
|
| 83 |
-
const baseUrl = await getMovies4UUrl();
|
| 84 |
-
return baseUrl + url;
|
| 85 |
-
}
|
| 86 |
-
return url;
|
| 87 |
-
}
|
| 88 |
-
|
| 89 |
-
// Function to extract ID from URL
|
| 90 |
-
function extractIdFromUrl(url: string): string {
|
| 91 |
-
try {
|
| 92 |
-
const urlParts = url.split('/');
|
| 93 |
-
const relevantPart = urlParts.find(part =>
|
| 94 |
-
part.includes('-') &&
|
| 95 |
-
(part.includes('season') || part.length > 10)
|
| 96 |
-
);
|
| 97 |
-
return relevantPart ? relevantPart.replace(/[^a-zA-Z0-9-]/g, '') : '';
|
| 98 |
-
} catch {
|
| 99 |
-
return '';
|
| 100 |
-
}
|
| 101 |
-
}
|
| 102 |
-
|
| 103 |
-
// Main function to scrape Movies4U data
|
| 104 |
-
async function scrapeMovies4UData(page: number = 1, searchQuery?: string): Promise<Movies4UItem[]> {
|
| 105 |
-
try {
|
| 106 |
-
const baseUrl = await getMovies4UUrl();
|
| 107 |
-
let url = baseUrl;
|
| 108 |
-
|
| 109 |
-
if (searchQuery) {
|
| 110 |
-
url += `?s=${encodeURIComponent(searchQuery)}`;
|
| 111 |
-
} else if (page > 1) {
|
| 112 |
-
url += `page/${page}/`;
|
| 113 |
-
}
|
| 114 |
-
|
| 115 |
-
console.log(`Fetching Movies4U content from: ${url}`);
|
| 116 |
-
|
| 117 |
-
const response = await fetch(url, {
|
| 118 |
-
cache: 'no-cache',
|
| 119 |
-
headers: {
|
| 120 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 121 |
-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
| 122 |
-
'Accept-Language': 'en-US,en;q=0.5',
|
| 123 |
-
'Referer':
|
| 124 |
-
},
|
| 125 |
-
next: { revalidate: 0 }
|
| 126 |
-
});
|
| 127 |
-
|
| 128 |
-
if (!response.ok) {
|
| 129 |
-
throw new Error(`Failed to fetch content: ${response.status}`);
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
const html = await response.text();
|
| 133 |
-
const $ = load(html);
|
| 134 |
-
const items: Movies4UItem[] = [];
|
| 135 |
-
|
| 136 |
-
console.log(`Received HTML content (length: ${html.length})`);
|
| 137 |
-
|
| 138 |
-
// Extract figure elements with post-thumbnail
|
| 139 |
-
$('figure').each((_, element) => {
|
| 140 |
-
const $figure = $(element);
|
| 141 |
-
const $postThumbnail = $figure.find('.post-thumbnail');
|
| 142 |
-
|
| 143 |
-
if ($postThumbnail.length > 0) {
|
| 144 |
-
try {
|
| 145 |
-
// Extract URL from the anchor tag
|
| 146 |
-
const url = $postThumbnail.attr('href') || '';
|
| 147 |
-
|
| 148 |
-
// Extract image information
|
| 149 |
-
const $img = $postThumbnail.find('img');
|
| 150 |
-
let imageUrl = $img.attr('src') || $img.attr('data-src') || '';
|
| 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 |
-
const
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
console.
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
const
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
let
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
if (headerText.includes('
|
| 312 |
-
else if (headerText.includes('
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
if (
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
const
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
size
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
}
|
| 333 |
-
|
| 334 |
-
//
|
| 335 |
-
const
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
if (headerText.includes('
|
| 387 |
-
else if (headerText.includes('
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
if (
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
//
|
| 405 |
-
const
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
}
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
);
|
| 550 |
-
|
| 551 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { NextRequest, NextResponse } from 'next/server';
|
| 2 |
+
import { load } from 'cheerio';
|
| 3 |
+
import { getMovies4UUrl } from '@/lib/utils/providers';
|
| 4 |
+
|
| 5 |
+
// Interface for actual download link
|
| 6 |
+
interface ActualDownloadLink {
|
| 7 |
+
url: string;
|
| 8 |
+
label: string;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
// Interface for download link structure
|
| 12 |
+
interface DownloadLink {
|
| 13 |
+
type: 'episodes' | 'batch';
|
| 14 |
+
url: string;
|
| 15 |
+
label: string;
|
| 16 |
+
batchSize?: string;
|
| 17 |
+
extractedLinks?: ActualDownloadLink[];
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
// Interface for quality options
|
| 21 |
+
interface QualityOption {
|
| 22 |
+
quality: string;
|
| 23 |
+
format: string;
|
| 24 |
+
size: string;
|
| 25 |
+
language: string;
|
| 26 |
+
links: DownloadLink[];
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
// Interface for season structure
|
| 30 |
+
interface Season {
|
| 31 |
+
name: string;
|
| 32 |
+
qualityOptions: QualityOption[];
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// Interface for the complete content data
|
| 36 |
+
interface ContentData {
|
| 37 |
+
title: string;
|
| 38 |
+
url: string;
|
| 39 |
+
posterUrl?: string;
|
| 40 |
+
seasons: Season[];
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
interface Movies4UItem {
|
| 44 |
+
id: string;
|
| 45 |
+
title: string;
|
| 46 |
+
url: string;
|
| 47 |
+
image: string;
|
| 48 |
+
videoLabel: string;
|
| 49 |
+
hasVideoIcon: boolean;
|
| 50 |
+
altText: string;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
interface Movies4UResponse {
|
| 54 |
+
success: boolean;
|
| 55 |
+
data?: {
|
| 56 |
+
items: Movies4UItem[];
|
| 57 |
+
pagination?: {
|
| 58 |
+
currentPage: number;
|
| 59 |
+
hasNextPage: boolean;
|
| 60 |
+
};
|
| 61 |
+
};
|
| 62 |
+
error?: string;
|
| 63 |
+
message?: string;
|
| 64 |
+
remainingRequests?: number;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
interface StreamResponse {
|
| 68 |
+
success: boolean;
|
| 69 |
+
data?: ContentData;
|
| 70 |
+
error?: string;
|
| 71 |
+
message?: string;
|
| 72 |
+
remainingRequests?: number;
|
| 73 |
+
seasonCount?: number;
|
| 74 |
+
qualityOptionCount?: number;
|
| 75 |
+
linkCount?: number;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
// Function to normalize image URLs
|
| 79 |
+
async function normalizeImageUrl(url: string | undefined): Promise<string | undefined> {
|
| 80 |
+
if (!url) return undefined;
|
| 81 |
+
if (url.startsWith('//')) return 'https:' + url;
|
| 82 |
+
if (url.startsWith('/')) {
|
| 83 |
+
const baseUrl = await getMovies4UUrl();
|
| 84 |
+
return baseUrl + url;
|
| 85 |
+
}
|
| 86 |
+
return url;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
// Function to extract ID from URL
|
| 90 |
+
function extractIdFromUrl(url: string): string {
|
| 91 |
+
try {
|
| 92 |
+
const urlParts = url.split('/');
|
| 93 |
+
const relevantPart = urlParts.find(part =>
|
| 94 |
+
part.includes('-') &&
|
| 95 |
+
(part.includes('season') || part.length > 10)
|
| 96 |
+
);
|
| 97 |
+
return relevantPart ? relevantPart.replace(/[^a-zA-Z0-9-]/g, '') : '';
|
| 98 |
+
} catch {
|
| 99 |
+
return '';
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
// Main function to scrape Movies4U data
|
| 104 |
+
async function scrapeMovies4UData(page: number = 1, searchQuery?: string): Promise<Movies4UItem[]> {
|
| 105 |
+
try {
|
| 106 |
+
const baseUrl = await getMovies4UUrl();
|
| 107 |
+
let url = baseUrl;
|
| 108 |
+
|
| 109 |
+
if (searchQuery) {
|
| 110 |
+
url += `?s=${encodeURIComponent(searchQuery)}`;
|
| 111 |
+
} else if (page > 1) {
|
| 112 |
+
url += `page/${page}/`;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
console.log(`Fetching Movies4U content from: ${url}`);
|
| 116 |
+
|
| 117 |
+
const response = await fetch(url, {
|
| 118 |
+
cache: 'no-cache',
|
| 119 |
+
headers: {
|
| 120 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 121 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
| 122 |
+
'Accept-Language': 'en-US,en;q=0.5',
|
| 123 |
+
'Referer': `${baseUrl}/`,
|
| 124 |
+
},
|
| 125 |
+
next: { revalidate: 0 }
|
| 126 |
+
});
|
| 127 |
+
|
| 128 |
+
if (!response.ok) {
|
| 129 |
+
throw new Error(`Failed to fetch content: ${response.status}`);
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
const html = await response.text();
|
| 133 |
+
const $ = load(html);
|
| 134 |
+
const items: Movies4UItem[] = [];
|
| 135 |
+
|
| 136 |
+
console.log(`Received HTML content (length: ${html.length})`);
|
| 137 |
+
|
| 138 |
+
// Extract figure elements with post-thumbnail
|
| 139 |
+
$('figure').each((_, element) => {
|
| 140 |
+
const $figure = $(element);
|
| 141 |
+
const $postThumbnail = $figure.find('.post-thumbnail');
|
| 142 |
+
|
| 143 |
+
if ($postThumbnail.length > 0) {
|
| 144 |
+
try {
|
| 145 |
+
// Extract URL from the anchor tag
|
| 146 |
+
const url = $postThumbnail.attr('href') || '';
|
| 147 |
+
|
| 148 |
+
// Extract image information
|
| 149 |
+
const $img = $postThumbnail.find('img');
|
| 150 |
+
let imageUrl = $img.attr('src') || $img.attr('data-src') || '';
|
| 151 |
+
|
| 152 |
+
// Extract alt text (title)
|
| 153 |
+
const altText = $img.attr('alt') || '';
|
| 154 |
+
|
| 155 |
+
// Extract video label
|
| 156 |
+
const videoLabel = $postThumbnail.find('.video-label').text().trim() || '';
|
| 157 |
+
|
| 158 |
+
// Check if video icon exists
|
| 159 |
+
const hasVideoIcon = $postThumbnail.find('.video-icon').length > 0;
|
| 160 |
+
|
| 161 |
+
// Generate ID from URL
|
| 162 |
+
const id = extractIdFromUrl(url) || `item-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
| 163 |
+
|
| 164 |
+
// Only add if we have essential information
|
| 165 |
+
if (url && imageUrl && altText) {
|
| 166 |
+
// We need to resolve the promise for normalizeImageUrl here or handle it differently
|
| 167 |
+
// Since we can't await inside forEach, we'll collect raw data first or use a normal loop
|
| 168 |
+
// For now, let's just push a promise or handle async properly.
|
| 169 |
+
// Actually, best practice with cheerio loops and async is `for...of` or `Promise.all`
|
| 170 |
+
// But since this structure is provided, let's fix the logic
|
| 171 |
+
items.push({
|
| 172 |
+
id,
|
| 173 |
+
title: altText,
|
| 174 |
+
url,
|
| 175 |
+
image: imageUrl, // Temporarily store raw, we will normalize later or assume it's absolute for now if sync is needed
|
| 176 |
+
videoLabel,
|
| 177 |
+
hasVideoIcon,
|
| 178 |
+
altText
|
| 179 |
+
});
|
| 180 |
+
}
|
| 181 |
+
} catch (itemError) {
|
| 182 |
+
console.error('Error parsing figure item:', itemError);
|
| 183 |
+
}
|
| 184 |
+
}
|
| 185 |
+
});
|
| 186 |
+
|
| 187 |
+
// If no items found with figure selector, try alternative selectors
|
| 188 |
+
if (items.length === 0) {
|
| 189 |
+
console.log('No items found with figure selector, trying alternative patterns...');
|
| 190 |
+
|
| 191 |
+
// Try article or post selectors as fallback
|
| 192 |
+
$('article, .post-item, .item').each((_, element) => {
|
| 193 |
+
const $element = $(element);
|
| 194 |
+
const $link = $element.find('a').first();
|
| 195 |
+
const $img = $element.find('img').first();
|
| 196 |
+
|
| 197 |
+
if ($link.length > 0 && $img.length > 0) {
|
| 198 |
+
const url = $link.attr('href') || '';
|
| 199 |
+
let imageUrl = $img.attr('src') || $img.attr('data-src') || '';
|
| 200 |
+
const altText = $img.attr('alt') || $link.text().trim() || '';
|
| 201 |
+
|
| 202 |
+
const videoLabel = $element.find('.video-label, .quality, .format').text().trim() || '';
|
| 203 |
+
const hasVideoIcon = $element.find('.video-icon, .play-icon').length > 0;
|
| 204 |
+
const id = extractIdFromUrl(url) || `item-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
| 205 |
+
|
| 206 |
+
if (url && imageUrl && altText) {
|
| 207 |
+
items.push({
|
| 208 |
+
id,
|
| 209 |
+
title: altText,
|
| 210 |
+
url,
|
| 211 |
+
image: imageUrl,
|
| 212 |
+
videoLabel,
|
| 213 |
+
hasVideoIcon,
|
| 214 |
+
altText
|
| 215 |
+
});
|
| 216 |
+
}
|
| 217 |
+
}
|
| 218 |
+
});
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
// Normalize images asynchronously after collection
|
| 222 |
+
for (const item of items) {
|
| 223 |
+
item.image = (await normalizeImageUrl(item.image)) || item.image;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
console.log(`Successfully parsed ${items.length} items from Movies4U`);
|
| 227 |
+
return items;
|
| 228 |
+
|
| 229 |
+
} catch (error) {
|
| 230 |
+
console.error('Error scraping Movies4U data:', error);
|
| 231 |
+
throw error;
|
| 232 |
+
}
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
// Function to scrape download links from a specific movie/show page
|
| 236 |
+
async function scrapeDownloadLinks(url: string): Promise<ContentData> {
|
| 237 |
+
try {
|
| 238 |
+
const baseUrl = await getMovies4UUrl();
|
| 239 |
+
console.log(`Fetching download links from: ${url}`);
|
| 240 |
+
|
| 241 |
+
const response = await fetch(url, {
|
| 242 |
+
cache: 'no-cache',
|
| 243 |
+
headers: {
|
| 244 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 245 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
| 246 |
+
'Accept-Language': 'en-US,en;q=0.5',
|
| 247 |
+
'Referer': `${baseUrl}/`,
|
| 248 |
+
},
|
| 249 |
+
next: { revalidate: 0 }
|
| 250 |
+
});
|
| 251 |
+
|
| 252 |
+
if (!response.ok) {
|
| 253 |
+
throw new Error(`Failed to fetch page: ${response.status}`);
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
const html = await response.text();
|
| 257 |
+
const $ = load(html);
|
| 258 |
+
|
| 259 |
+
// Extract page title
|
| 260 |
+
const pageTitle = $('h1.single-title').text().trim() ||
|
| 261 |
+
$('h1').first().text().trim() ||
|
| 262 |
+
$('title').text().trim().replace(' - Movies4u', '');
|
| 263 |
+
|
| 264 |
+
// Extract poster image
|
| 265 |
+
let posterUrl = '';
|
| 266 |
+
const posterImg = $('.post-thumbnail img, .featured-image img').first();
|
| 267 |
+
if (posterImg.length) {
|
| 268 |
+
posterUrl = posterImg.attr('src') || '';
|
| 269 |
+
console.log(`Found poster image: ${posterUrl}`);
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
// Extract all seasons and download links
|
| 273 |
+
const seasons: Season[] = [];
|
| 274 |
+
let currentSeason: Season | null = null;
|
| 275 |
+
|
| 276 |
+
// Find the download links div
|
| 277 |
+
$('.download-links-div').each((_, downloadSection) => {
|
| 278 |
+
// Process each h4 header (represents a quality option for a season)
|
| 279 |
+
$(downloadSection).find('h4').each((_, headerElement) => {
|
| 280 |
+
const headerText = $(headerElement).text().trim();
|
| 281 |
+
|
| 282 |
+
console.log('Processing header:', headerText);
|
| 283 |
+
|
| 284 |
+
// Skip horizontal rules
|
| 285 |
+
if (headerText === '<hr>') return;
|
| 286 |
+
|
| 287 |
+
// Extract season name
|
| 288 |
+
const seasonMatch = headerText.match(/Season\s+(\d+)/i);
|
| 289 |
+
|
| 290 |
+
if (seasonMatch) {
|
| 291 |
+
const seasonName = `Season ${seasonMatch[1]}`;
|
| 292 |
+
|
| 293 |
+
// Check if we need to create a new season object
|
| 294 |
+
if (!currentSeason || currentSeason.name !== seasonName) {
|
| 295 |
+
currentSeason = {
|
| 296 |
+
name: seasonName,
|
| 297 |
+
qualityOptions: []
|
| 298 |
+
};
|
| 299 |
+
seasons.push(currentSeason);
|
| 300 |
+
console.log(`Found season: ${seasonName}`);
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
// Extract language info (text in curly braces)
|
| 304 |
+
const languageMatch = headerText.match(/\{([^{}]+)\}/);
|
| 305 |
+
const language = languageMatch ? languageMatch[0] : '';
|
| 306 |
+
|
| 307 |
+
// Extract quality
|
| 308 |
+
let quality = '';
|
| 309 |
+
if (headerText.includes('480p')) quality = '480p';
|
| 310 |
+
else if (headerText.includes('720p')) quality = '720p';
|
| 311 |
+
else if (headerText.includes('1080p')) quality = '1080p';
|
| 312 |
+
else if (headerText.includes('2160p') || headerText.includes('4K')) quality = '2160p 4K';
|
| 313 |
+
|
| 314 |
+
// Extract format and codec
|
| 315 |
+
let format = '';
|
| 316 |
+
if (headerText.includes('WEB-DL')) format = 'WEB-DL';
|
| 317 |
+
else if (headerText.includes('WEBRip')) format = 'WEBRip';
|
| 318 |
+
else if (headerText.includes('BluRay')) format = 'BluRay';
|
| 319 |
+
|
| 320 |
+
if (headerText.includes('x264')) format += ' x264';
|
| 321 |
+
else if (headerText.includes('HEVC') || headerText.includes('x265')) format += ' HEVC x265';
|
| 322 |
+
if (headerText.includes('10bit')) format += ' 10bit';
|
| 323 |
+
|
| 324 |
+
// Extract size per episode
|
| 325 |
+
let size = '';
|
| 326 |
+
const sizeMatch = headerText.match(/\[([^[\]]+)(?:\/E)?\]/);
|
| 327 |
+
if (sizeMatch) {
|
| 328 |
+
size = sizeMatch[1];
|
| 329 |
+
if (!size.includes('/E') && !headerText.includes('BATCH')) {
|
| 330 |
+
size += '/E';
|
| 331 |
+
}
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
// Create a new quality option
|
| 335 |
+
const qualityOption: QualityOption = {
|
| 336 |
+
quality,
|
| 337 |
+
format,
|
| 338 |
+
size,
|
| 339 |
+
language,
|
| 340 |
+
links: []
|
| 341 |
+
};
|
| 342 |
+
|
| 343 |
+
// Find the download buttons div that follows this header
|
| 344 |
+
const downloadsDiv = $(headerElement).next('.downloads-btns-div');
|
| 345 |
+
if (downloadsDiv.length) {
|
| 346 |
+
// Extract episode links only (no batch links)
|
| 347 |
+
downloadsDiv.find('a.btn:not(.btn-zip)').each((_, link) => {
|
| 348 |
+
const linkUrl = $(link).attr('href') || '';
|
| 349 |
+
const linkText = $(link).text().trim().replace(/\s+/g, ' ');
|
| 350 |
+
|
| 351 |
+
// Skip if link text contains BATCH or ZIP
|
| 352 |
+
if (linkUrl && !linkText.toLowerCase().includes('batch') && !linkText.toLowerCase().includes('zip')) {
|
| 353 |
+
qualityOption.links.push({
|
| 354 |
+
type: 'episodes',
|
| 355 |
+
url: linkUrl,
|
| 356 |
+
label: linkText || 'Download Links'
|
| 357 |
+
});
|
| 358 |
+
console.log(` - Found episode link: ${linkUrl}`);
|
| 359 |
+
}
|
| 360 |
+
});
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
// Add this quality option to the current season
|
| 364 |
+
if (currentSeason) {
|
| 365 |
+
currentSeason.qualityOptions.push(qualityOption);
|
| 366 |
+
}
|
| 367 |
+
} else {
|
| 368 |
+
// Special case for standalone quality options (not tied to a numbered season)
|
| 369 |
+
const specialTitle = headerText.replace(/<[^>]*>/g, '').trim();
|
| 370 |
+
|
| 371 |
+
if (specialTitle && specialTitle.length > 0) {
|
| 372 |
+
// Create a special "season" for this standalone quality
|
| 373 |
+
const specialSeason: Season = {
|
| 374 |
+
name: "Special",
|
| 375 |
+
qualityOptions: []
|
| 376 |
+
};
|
| 377 |
+
|
| 378 |
+
// Extract language info (text in curly braces)
|
| 379 |
+
const languageMatch = headerText.match(/\{([^{}]+)\}/);
|
| 380 |
+
const language = languageMatch ? languageMatch[0] : '';
|
| 381 |
+
|
| 382 |
+
// Extract quality
|
| 383 |
+
let quality = '';
|
| 384 |
+
if (headerText.includes('480p')) quality = '480p';
|
| 385 |
+
else if (headerText.includes('720p')) quality = '720p';
|
| 386 |
+
else if (headerText.includes('1080p')) quality = '1080p';
|
| 387 |
+
else if (headerText.includes('2160p') || headerText.includes('4K')) quality = '2160p 4K';
|
| 388 |
+
|
| 389 |
+
// Extract format and codec
|
| 390 |
+
let format = '';
|
| 391 |
+
if (headerText.includes('WEB-DL')) format = 'WEB-DL';
|
| 392 |
+
else if (headerText.includes('WEBRip')) format = 'WEBRip';
|
| 393 |
+
else if (headerText.includes('BluRay')) format = 'BluRay';
|
| 394 |
+
|
| 395 |
+
if (headerText.includes('x264')) format += ' x264';
|
| 396 |
+
else if (headerText.includes('HEVC') || headerText.includes('x265')) format += ' HEVC x265';
|
| 397 |
+
if (headerText.includes('10bit')) format += ' 10bit';
|
| 398 |
+
|
| 399 |
+
// Extract size
|
| 400 |
+
let size = '';
|
| 401 |
+
const sizeMatch = headerText.match(/\[([^[\]]+)\]/);
|
| 402 |
+
if (sizeMatch) size = sizeMatch[1];
|
| 403 |
+
|
| 404 |
+
// Create quality option
|
| 405 |
+
const qualityOption: QualityOption = {
|
| 406 |
+
quality,
|
| 407 |
+
format,
|
| 408 |
+
size,
|
| 409 |
+
language,
|
| 410 |
+
links: []
|
| 411 |
+
};
|
| 412 |
+
|
| 413 |
+
// Find the download buttons div that follows this header
|
| 414 |
+
const downloadsDiv = $(headerElement).next('.downloads-btns-div');
|
| 415 |
+
if (downloadsDiv.length) {
|
| 416 |
+
// Extract download links
|
| 417 |
+
downloadsDiv.find('a.btn').each((_, link) => {
|
| 418 |
+
const linkUrl = $(link).attr('href') || '';
|
| 419 |
+
const linkText = $(link).text().trim().replace(/\s+/g, ' ');
|
| 420 |
+
|
| 421 |
+
if (linkUrl) {
|
| 422 |
+
qualityOption.links.push({
|
| 423 |
+
type: 'episodes',
|
| 424 |
+
url: linkUrl,
|
| 425 |
+
label: linkText || 'Download Links'
|
| 426 |
+
});
|
| 427 |
+
console.log(` - Found special link: ${linkUrl}`);
|
| 428 |
+
}
|
| 429 |
+
});
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
// Add this quality option to the special season
|
| 433 |
+
specialSeason.qualityOptions.push(qualityOption);
|
| 434 |
+
|
| 435 |
+
// Add the special season to our list
|
| 436 |
+
if (specialSeason.qualityOptions.length > 0 &&
|
| 437 |
+
specialSeason.qualityOptions[0].links.length > 0) {
|
| 438 |
+
seasons.push(specialSeason);
|
| 439 |
+
console.log(`Found special quality option: ${specialTitle}`);
|
| 440 |
+
}
|
| 441 |
+
}
|
| 442 |
+
}
|
| 443 |
+
});
|
| 444 |
+
});
|
| 445 |
+
|
| 446 |
+
// Create the final content data
|
| 447 |
+
const contentData: ContentData = {
|
| 448 |
+
title: pageTitle,
|
| 449 |
+
url,
|
| 450 |
+
posterUrl: posterUrl || undefined,
|
| 451 |
+
seasons
|
| 452 |
+
};
|
| 453 |
+
|
| 454 |
+
console.log(`Successfully extracted ${seasons.length} seasons with download links`);
|
| 455 |
+
return contentData;
|
| 456 |
+
|
| 457 |
+
} catch (error) {
|
| 458 |
+
console.error('Error scraping download links:', error);
|
| 459 |
+
throw error;
|
| 460 |
+
}
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
export async function GET(request: NextRequest): Promise<NextResponse<Movies4UResponse | StreamResponse>> {
|
| 464 |
+
try {
|
| 465 |
+
// Validate API key
|
| 466 |
+
|
| 467 |
+
|
| 468 |
+
const { searchParams } = new URL(request.url);
|
| 469 |
+
const streamId = searchParams.get('stream');
|
| 470 |
+
|
| 471 |
+
// Handle stream request for download links
|
| 472 |
+
if (streamId) {
|
| 473 |
+
try {
|
| 474 |
+
const contentData = await scrapeDownloadLinks(streamId);
|
| 475 |
+
|
| 476 |
+
if (contentData.seasons.length === 0) {
|
| 477 |
+
return NextResponse.json<StreamResponse>({
|
| 478 |
+
success: false,
|
| 479 |
+
error: 'No download links found',
|
| 480 |
+
message: `No download links found for: ${streamId}`,
|
| 481 |
+
});
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
return NextResponse.json<StreamResponse>({
|
| 485 |
+
success: true,
|
| 486 |
+
data: contentData,
|
| 487 |
+
seasonCount: contentData.seasons.length,
|
| 488 |
+
qualityOptionCount: contentData.seasons.reduce((total, season) => total + season.qualityOptions.length, 0),
|
| 489 |
+
linkCount: contentData.seasons.reduce((total, season) => {
|
| 490 |
+
return total + season.qualityOptions.reduce((subtotal, option) => {
|
| 491 |
+
return subtotal + option.links.length;
|
| 492 |
+
}, 0);
|
| 493 |
+
}, 0)
|
| 494 |
+
});
|
| 495 |
+
|
| 496 |
+
} catch (error) {
|
| 497 |
+
console.error('Stream request error:', error);
|
| 498 |
+
return NextResponse.json<StreamResponse>(
|
| 499 |
+
{
|
| 500 |
+
success: false,
|
| 501 |
+
error: 'Failed to fetch download links',
|
| 502 |
+
message: error instanceof Error ? error.message : 'Unknown error occurred'
|
| 503 |
+
},
|
| 504 |
+
{ status: 500 }
|
| 505 |
+
);
|
| 506 |
+
}
|
| 507 |
+
}
|
| 508 |
+
|
| 509 |
+
// Handle regular listing request
|
| 510 |
+
const page = parseInt(searchParams.get('page') || '1');
|
| 511 |
+
const searchQuery = searchParams.get('search');
|
| 512 |
+
|
| 513 |
+
if (page < 1) {
|
| 514 |
+
return NextResponse.json<Movies4UResponse>(
|
| 515 |
+
{
|
| 516 |
+
success: false,
|
| 517 |
+
error: 'Page number must be 1 or greater'
|
| 518 |
+
},
|
| 519 |
+
{ status: 400 }
|
| 520 |
+
);
|
| 521 |
+
}
|
| 522 |
+
|
| 523 |
+
console.log('Processing Movies4U request:', { page, searchQuery });
|
| 524 |
+
|
| 525 |
+
const items = await scrapeMovies4UData(page, searchQuery || undefined);
|
| 526 |
+
|
| 527 |
+
if (!items || items.length === 0) {
|
| 528 |
+
return NextResponse.json<Movies4UResponse>({
|
| 529 |
+
success: false,
|
| 530 |
+
error: 'No items found',
|
| 531 |
+
message: searchQuery
|
| 532 |
+
? `No items found for search query: "${searchQuery}"`
|
| 533 |
+
: `No items found on page ${page}`,
|
| 534 |
+
});
|
| 535 |
+
}
|
| 536 |
+
|
| 537 |
+
return NextResponse.json<Movies4UResponse>({
|
| 538 |
+
success: true,
|
| 539 |
+
data: {
|
| 540 |
+
items,
|
| 541 |
+
pagination: {
|
| 542 |
+
currentPage: page,
|
| 543 |
+
hasNextPage: items.length >= 10
|
| 544 |
+
}
|
| 545 |
+
},
|
| 546 |
+
});
|
| 547 |
+
|
| 548 |
+
} catch (error: unknown) {
|
| 549 |
+
console.error('Movies4U API error:', error);
|
| 550 |
+
|
| 551 |
+
return NextResponse.json<Movies4UResponse>(
|
| 552 |
+
{
|
| 553 |
+
success: false,
|
| 554 |
+
error: 'Failed to fetch content from Movies4U',
|
| 555 |
+
message: error instanceof Error ? error.message : 'Unknown error occurred'
|
| 556 |
+
},
|
| 557 |
+
{ status: 500 }
|
| 558 |
+
);
|
| 559 |
+
}
|
| 560 |
+
}
|