From 22c74cd5f6e57193288e89922afd84f0ed163318 Mon Sep 17 00:00:00 2001 From: 8man Date: Fri, 18 Apr 2025 20:24:15 +0530 Subject: [PATCH] Update url-checker.js --- .github/scripts/url-checker.js | 38 +++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/.github/scripts/url-checker.js b/.github/scripts/url-checker.js index 5574df0..be8be4c 100644 --- a/.github/scripts/url-checker.js +++ b/.github/scripts/url-checker.js @@ -1,6 +1,5 @@ const fs = require('fs'); const axios = require('axios'); -const path = require('path'); const FILE_PATH = 'modflix.json'; @@ -26,11 +25,19 @@ function getDomain(url) { } } -// Extract path from URL +// Extract full path from URL including trailing slash function getPath(url) { try { const urlObj = new URL(url); - return urlObj.pathname + urlObj.search + urlObj.hash; + let pathWithQuery = urlObj.pathname + urlObj.search + urlObj.hash; + + // Important: Check the original URL for trailing slash to preserve it exactly + const endsWithSlash = url.endsWith('/'); + if (endsWithSlash && !pathWithQuery.endsWith('/')) { + pathWithQuery = pathWithQuery + '/'; + } + + return pathWithQuery; } catch (error) { console.error(`Error extracting path from ${url}:`, error); return ''; @@ -39,6 +46,8 @@ function getPath(url) { // Check URL and return new URL if redirected async function checkUrl(url) { + const originalUrl = url; // Store the exact original URL to check for trailing slash later + try { // Set timeout to 10 seconds to avoid hanging const response = await axios.head(url, { @@ -66,14 +75,19 @@ async function checkUrl(url) { // Get new domain but keep original path const newDomain = getDomain(fullRedirectUrl); - const originalPath = getPath(url); + const originalPath = getPath(originalUrl); // Use the original URL for path extraction - // Construct new URL with original path + // Construct new URL with original path, preserving trailing slashes exactly let finalUrl = newDomain; - if (originalPath && originalPath !== '/') { + if (originalPath) { finalUrl += originalPath; } + // Double-check if we preserved trailing slash correctly when present in original + if (originalUrl.endsWith('/') && !finalUrl.endsWith('/')) { + finalUrl += '/'; + } + return finalUrl; } } else { @@ -105,13 +119,18 @@ async function checkUrl(url) { } const newDomain = getDomain(fullRedirectUrl); - const originalPath = getPath(url); + const originalPath = getPath(originalUrl); // Use original URL let finalUrl = newDomain; - if (originalPath && originalPath !== '/') { + if (originalPath) { finalUrl += originalPath; } + // Double-check trailing slash preservation + if (originalUrl.endsWith('/') && !finalUrl.endsWith('/')) { + finalUrl += '/'; + } + return finalUrl; } } else { @@ -147,6 +166,9 @@ async function main() { try { const newUrl = await checkUrl(url); if (newUrl && newUrl !== url) { + // Before updating, log the exact change for verification + console.log(`URL change details: Original="${url}" → New="${newUrl}"`); + provider.url = newUrl; hasChanges = true; console.log(`Updated ${provider.name} URL from ${url} to ${newUrl}`);