mirror of
https://github.com/himanshu8443/providers.git
synced 2026-04-17 21:41:45 +00:00
Update url-checker.js
This commit is contained in:
51
.github/scripts/url-checker.js
vendored
51
.github/scripts/url-checker.js
vendored
@@ -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}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user