Update url-checker.js

This commit is contained in:
8man
2025-04-18 20:30:06 +05:30
committed by GitHub
parent de4624c036
commit 55fbf2b9cb

View File

@@ -14,7 +14,7 @@ function readModflixJson() {
} }
} }
// Extract domain from URL // Extract domain (origin) from URL
function getDomain(url) { function getDomain(url) {
try { try {
const urlObj = new URL(url); const urlObj = new URL(url);
@@ -25,35 +25,27 @@ function getDomain(url) {
} }
} }
// Extract full path from URL including trailing slash // Extract ONLY the path from the original URL
function getPath(url) { function getOriginalPath(url) {
try { try {
const urlObj = new URL(url); const urlObj = new URL(url);
let pathWithQuery = urlObj.pathname + urlObj.search + urlObj.hash; return 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 '';
} }
} }
// Check URL and return new URL if redirected // Check URL and return new URL if domain redirected
async function checkUrl(url) { async function checkUrl(url) {
const originalUrl = url; // Store the exact original URL to check for trailing slash later const originalUrl = url;
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, {
maxRedirects: 0, maxRedirects: 0,
timeout: 10000, timeout: 10000,
validateStatus: status => true // Accept all status codes to handle them manually validateStatus: status => true
}); });
// If status is 200, no change needed // If status is 200, no change needed
@@ -73,28 +65,24 @@ async function checkUrl(url) {
console.log(`🔄 ${url} redirects to ${fullRedirectUrl}`); console.log(`🔄 ${url} redirects to ${fullRedirectUrl}`);
// Get new domain but keep original path // IMPORTANT: Only extract the domain/origin from the redirect URL
const newDomain = getDomain(fullRedirectUrl); const newDomain = getDomain(fullRedirectUrl);
const originalPath = getPath(originalUrl); // Use the original URL for path extraction
// Construct new URL with original path, preserving trailing slashes exactly // Get the ORIGINAL path from the modflix.json URL (might be empty, might have trailing slash)
let finalUrl = newDomain; const originalPath = getOriginalPath(originalUrl);
if (originalPath) {
finalUrl += originalPath;
}
// Double-check if we preserved trailing slash correctly when present in original // Combine new domain with original path
if (originalUrl.endsWith('/') && !finalUrl.endsWith('/')) { const finalUrl = newDomain + originalPath;
finalUrl += '/';
}
// Log the change
console.log(`Will update to: ${finalUrl} (new domain + original path)`);
return finalUrl; return finalUrl;
} }
} else { } else {
console.log(`⚠️ ${url} returned status ${response.status}`); console.log(`⚠️ ${url} returned status ${response.status}`);
} }
} catch (error) { } catch (error) {
// Try GET request if HEAD fails (some servers don't properly support HEAD) // Try GET request if HEAD fails
try { try {
const response = await axios.get(url, { const response = await axios.get(url, {
maxRedirects: 0, maxRedirects: 0,
@@ -111,26 +99,22 @@ async function checkUrl(url) {
if (newLocation) { if (newLocation) {
console.log(`🔄 ${url} redirects to ${newLocation}`); console.log(`🔄 ${url} redirects to ${newLocation}`);
// Process redirect similar to above
let fullRedirectUrl = newLocation; let fullRedirectUrl = newLocation;
if (!newLocation.startsWith('http')) { if (!newLocation.startsWith('http')) {
const baseUrl = new URL(url); const baseUrl = new URL(url);
fullRedirectUrl = new URL(newLocation, baseUrl.origin).toString(); fullRedirectUrl = new URL(newLocation, baseUrl.origin).toString();
} }
// IMPORTANT: Only extract the domain/origin
const newDomain = getDomain(fullRedirectUrl); const newDomain = getDomain(fullRedirectUrl);
const originalPath = getPath(originalUrl); // Use original URL
let finalUrl = newDomain; // Keep ORIGINAL path
if (originalPath) { const originalPath = getOriginalPath(originalUrl);
finalUrl += originalPath;
}
// Double-check trailing slash preservation // Combine new domain with original path
if (originalUrl.endsWith('/') && !finalUrl.endsWith('/')) { const finalUrl = newDomain + originalPath;
finalUrl += '/';
}
console.log(`Will update to: ${finalUrl} (new domain + original path)`);
return finalUrl; return finalUrl;
} }
} else { } else {
@@ -166,9 +150,6 @@ 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}`);