Update url-checker.js

This commit is contained in:
8man
2025-04-18 20:38:27 +05:30
committed by GitHub
parent ac5cf4ade2
commit 581b57a56f

View File

@@ -14,7 +14,7 @@ function readModflixJson() {
} }
} }
// Extract domain (origin) from URL // Extract domain (origin) from URL without trailing slash
function getDomain(url) { function getDomain(url) {
try { try {
const urlObj = new URL(url); const urlObj = new URL(url);
@@ -25,21 +25,13 @@ function getDomain(url) {
} }
} }
// Extract ONLY the path from the original URL // Check if original URL has a trailing slash in path
function getOriginalPath(url) { function hasTrailingSlash(url) {
try { return url.endsWith('/') && !url.endsWith('://');
const urlObj = new URL(url);
return urlObj.pathname + urlObj.search + urlObj.hash;
} catch (error) {
console.error(`Error extracting path from ${url}:`, error);
return '';
}
} }
// Check URL and return new URL if domain redirected // Check URL and return new URL if domain redirected
async function checkUrl(url) { async function checkUrl(url) {
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, {
@@ -65,17 +57,19 @@ async function checkUrl(url) {
console.log(`🔄 ${url} redirects to ${fullRedirectUrl}`); console.log(`🔄 ${url} redirects to ${fullRedirectUrl}`);
// IMPORTANT: Only extract the domain/origin from the redirect URL // Get the new domain
const newDomain = getDomain(fullRedirectUrl); const newDomain = getDomain(fullRedirectUrl);
// Get the ORIGINAL path from the modflix.json URL (might be empty, might have trailing slash) // Check if original URL had a trailing slash
const originalPath = getOriginalPath(originalUrl); const needsTrailingSlash = hasTrailingSlash(url);
// Combine new domain with original path // Create new URL: new domain + trailing slash if the original had one
const finalUrl = newDomain + originalPath; let finalUrl = newDomain;
if (needsTrailingSlash) {
finalUrl += '/';
}
// Log the change console.log(`Will update to: ${finalUrl} (preserved trailing slash: ${needsTrailingSlash})`);
console.log(`Will update to: ${finalUrl} (new domain + original path)`);
return finalUrl; return finalUrl;
} }
} else { } else {
@@ -105,16 +99,19 @@ async function checkUrl(url) {
fullRedirectUrl = new URL(newLocation, baseUrl.origin).toString(); fullRedirectUrl = new URL(newLocation, baseUrl.origin).toString();
} }
// IMPORTANT: Only extract the domain/origin // Get the new domain
const newDomain = getDomain(fullRedirectUrl); const newDomain = getDomain(fullRedirectUrl);
// Keep ORIGINAL path // Check if original URL had a trailing slash
const originalPath = getOriginalPath(originalUrl); const needsTrailingSlash = hasTrailingSlash(url);
// Combine new domain with original path // Create new URL: new domain + trailing slash if the original had one
const finalUrl = newDomain + originalPath; let finalUrl = newDomain;
if (needsTrailingSlash) {
finalUrl += '/';
}
console.log(`Will update to: ${finalUrl} (new domain + original path)`); console.log(`Will update to: ${finalUrl} (preserved trailing slash: ${needsTrailingSlash})`);
return finalUrl; return finalUrl;
} }
} else { } else {
@@ -161,7 +158,9 @@ async function main() {
// Write changes back to file if needed // Write changes back to file if needed
if (hasChanges) { if (hasChanges) {
fs.writeFileSync(FILE_PATH, JSON.stringify(providers, null, 2)); // Use a space-efficient JSON format but with proper formatting
const jsonString = JSON.stringify(providers, null, 2);
fs.writeFileSync(FILE_PATH, jsonString);
console.log(`✅ Updated ${FILE_PATH} with new URLs`); console.log(`✅ Updated ${FILE_PATH} with new URLs`);
} else { } else {
console.log(` No changes needed for ${FILE_PATH}`); console.log(` No changes needed for ${FILE_PATH}`);