Update url-checker.js

This commit is contained in:
8man
2025-04-18 20:24:15 +05:30
committed by GitHub
parent 4e166a7028
commit 22c74cd5f6

View File

@@ -1,6 +1,5 @@
const fs = require('fs'); const fs = require('fs');
const axios = require('axios'); const axios = require('axios');
const path = require('path');
const FILE_PATH = 'modflix.json'; 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) { function getPath(url) {
try { try {
const urlObj = new URL(url); 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) { } catch (error) {
console.error(`Error extracting path from ${url}:`, error); console.error(`Error extracting path from ${url}:`, error);
return ''; return '';
@@ -39,6 +46,8 @@ function getPath(url) {
// Check URL and return new URL if redirected // Check URL and return new URL if redirected
async function checkUrl(url) { async function checkUrl(url) {
const originalUrl = url; // Store the exact original URL to check for trailing slash later
try { try {
// Set timeout to 10 seconds to avoid hanging // Set timeout to 10 seconds to avoid hanging
const response = await axios.head(url, { const response = await axios.head(url, {
@@ -66,14 +75,19 @@ async function checkUrl(url) {
// Get new domain but keep original path // Get new domain but keep original path
const newDomain = getDomain(fullRedirectUrl); 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; let finalUrl = newDomain;
if (originalPath && originalPath !== '/') { if (originalPath) {
finalUrl += originalPath; finalUrl += originalPath;
} }
// Double-check if we preserved trailing slash correctly when present in original
if (originalUrl.endsWith('/') && !finalUrl.endsWith('/')) {
finalUrl += '/';
}
return finalUrl; return finalUrl;
} }
} else { } else {
@@ -105,13 +119,18 @@ async function checkUrl(url) {
} }
const newDomain = getDomain(fullRedirectUrl); const newDomain = getDomain(fullRedirectUrl);
const originalPath = getPath(url); const originalPath = getPath(originalUrl); // Use original URL
let finalUrl = newDomain; let finalUrl = newDomain;
if (originalPath && originalPath !== '/') { if (originalPath) {
finalUrl += originalPath; finalUrl += originalPath;
} }
// Double-check trailing slash preservation
if (originalUrl.endsWith('/') && !finalUrl.endsWith('/')) {
finalUrl += '/';
}
return finalUrl; return finalUrl;
} }
} else { } else {
@@ -147,6 +166,9 @@ async function main() {
try { try {
const newUrl = await checkUrl(url); const newUrl = await checkUrl(url);
if (newUrl && newUrl !== 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; provider.url = newUrl;
hasChanges = true; hasChanges = true;
console.log(`Updated ${provider.name} URL from ${url} to ${newUrl}`); console.log(`Updated ${provider.name} URL from ${url} to ${newUrl}`);