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:
119
.github/scripts/url-checker.js
vendored
119
.github/scripts/url-checker.js
vendored
@@ -1,13 +1,13 @@
|
|||||||
const fs = require("fs");
|
const fs = require('fs');
|
||||||
const axios = require("axios");
|
const axios = require('axios');
|
||||||
const path = require("path");
|
const path = require('path');
|
||||||
|
|
||||||
const FILE_PATH = "modflix.json";
|
const FILE_PATH = 'modflix.json';
|
||||||
|
|
||||||
// Read the modflix.json file
|
// Read the modflix.json file
|
||||||
async function readModflixJson() {
|
function readModflixJson() {
|
||||||
try {
|
try {
|
||||||
const data = fs.readFileSync(FILE_PATH, "utf8");
|
const data = fs.readFileSync(FILE_PATH, 'utf8');
|
||||||
return JSON.parse(data);
|
return JSON.parse(data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error reading ${FILE_PATH}:`, error);
|
console.error(`Error reading ${FILE_PATH}:`, error);
|
||||||
@@ -33,37 +33,31 @@ function getPath(url) {
|
|||||||
return urlObj.pathname + urlObj.search + urlObj.hash;
|
return urlObj.pathname + urlObj.search + urlObj.hash;
|
||||||
} 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 redirected
|
||||||
async function checkUrl(url) {
|
async function checkUrl(url) {
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(url, {
|
// Set timeout to 10 seconds to avoid hanging
|
||||||
|
const response = await axios.head(url, {
|
||||||
maxRedirects: 0,
|
maxRedirects: 0,
|
||||||
validateStatus: (status) => status >= 200 && status < 400,
|
timeout: 10000,
|
||||||
|
validateStatus: status => true // Accept all status codes to handle them manually
|
||||||
});
|
});
|
||||||
|
|
||||||
// If status is 200, no change needed
|
// If status is 200, no change needed
|
||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
console.log(`✅ ${url} is valid (200 OK)`);
|
console.log(`✅ ${url} is valid (200 OK)`);
|
||||||
return null;
|
return null;
|
||||||
}
|
} else if (response.status >= 300 && response.status < 400) {
|
||||||
} catch (error) {
|
// Handle redirects
|
||||||
// Handle redirects
|
const newLocation = response.headers.location;
|
||||||
if (
|
|
||||||
error.response &&
|
|
||||||
(error.response.status === 301 ||
|
|
||||||
error.response.status === 302 ||
|
|
||||||
error.response.status === 307 ||
|
|
||||||
error.response.status === 308)
|
|
||||||
) {
|
|
||||||
const newLocation = error.response.headers.location;
|
|
||||||
if (newLocation) {
|
if (newLocation) {
|
||||||
// If it's a relative redirect, construct the full URL
|
// If it's a relative redirect, construct the full URL
|
||||||
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();
|
||||||
}
|
}
|
||||||
@@ -76,18 +70,63 @@ async function checkUrl(url) {
|
|||||||
|
|
||||||
// Construct new URL with original path
|
// Construct new URL with original path
|
||||||
let finalUrl = newDomain;
|
let finalUrl = newDomain;
|
||||||
if (originalPath && originalPath !== "/") {
|
if (originalPath && originalPath !== '/') {
|
||||||
finalUrl += originalPath;
|
finalUrl += originalPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
return finalUrl;
|
return finalUrl;
|
||||||
}
|
}
|
||||||
} else if (error.response) {
|
|
||||||
console.log(`⚠️ ${url} returned status ${error.response.status}`);
|
|
||||||
} else if (error.request) {
|
|
||||||
console.log(`❌ ${url} failed to respond`);
|
|
||||||
} else {
|
} else {
|
||||||
console.log(`❌ Error checking ${url}: ${error.message}`);
|
console.log(`⚠️ ${url} returned status ${response.status}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Try GET request if HEAD fails (some servers don't properly support HEAD)
|
||||||
|
try {
|
||||||
|
const response = await axios.get(url, {
|
||||||
|
maxRedirects: 0,
|
||||||
|
timeout: 10000,
|
||||||
|
validateStatus: status => true
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.status === 200) {
|
||||||
|
console.log(`✅ ${url} is valid (200 OK)`);
|
||||||
|
return null;
|
||||||
|
} else if (response.status >= 300 && response.status < 400) {
|
||||||
|
// Handle redirects
|
||||||
|
const newLocation = response.headers.location;
|
||||||
|
if (newLocation) {
|
||||||
|
console.log(`🔄 ${url} redirects to ${newLocation}`);
|
||||||
|
|
||||||
|
// Process redirect similar to above
|
||||||
|
let fullRedirectUrl = newLocation;
|
||||||
|
if (!newLocation.startsWith('http')) {
|
||||||
|
const baseUrl = new URL(url);
|
||||||
|
fullRedirectUrl = new URL(newLocation, baseUrl.origin).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
const newDomain = getDomain(fullRedirectUrl);
|
||||||
|
const originalPath = getPath(url);
|
||||||
|
|
||||||
|
let finalUrl = newDomain;
|
||||||
|
if (originalPath && originalPath !== '/') {
|
||||||
|
finalUrl += originalPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
return finalUrl;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log(`⚠️ ${url} returned status ${response.status}`);
|
||||||
|
}
|
||||||
|
} catch (getError) {
|
||||||
|
if (getError.response) {
|
||||||
|
console.log(`⚠️ ${url} returned status ${getError.response.status}`);
|
||||||
|
} else if (getError.code === 'ECONNABORTED') {
|
||||||
|
console.log(`⌛ ${url} request timed out`);
|
||||||
|
} else if (getError.code === 'ENOTFOUND') {
|
||||||
|
console.log(`❌ ${url} domain not found`);
|
||||||
|
} else {
|
||||||
|
console.log(`❌ Error checking ${url}: ${getError.message}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +136,7 @@ async function checkUrl(url) {
|
|||||||
|
|
||||||
// Main function
|
// Main function
|
||||||
async function main() {
|
async function main() {
|
||||||
const providers = await readModflixJson();
|
const providers = readModflixJson();
|
||||||
let hasChanges = false;
|
let hasChanges = false;
|
||||||
|
|
||||||
// Process each provider
|
// Process each provider
|
||||||
@@ -105,25 +144,29 @@ async function main() {
|
|||||||
const url = provider.url;
|
const url = provider.url;
|
||||||
console.log(`Checking ${provider.name} (${url})...`);
|
console.log(`Checking ${provider.name} (${url})...`);
|
||||||
|
|
||||||
const newUrl = await checkUrl(url);
|
try {
|
||||||
if (newUrl && newUrl !== url) {
|
const newUrl = await checkUrl(url);
|
||||||
provider.url = newUrl;
|
if (newUrl && newUrl !== url) {
|
||||||
hasChanges = true;
|
provider.url = newUrl;
|
||||||
console.log(`Updated ${provider.name} URL to ${newUrl}`);
|
hasChanges = true;
|
||||||
|
console.log(`Updated ${provider.name} URL from ${url} to ${newUrl}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`❌ Error processing ${url}: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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));
|
fs.writeFileSync(FILE_PATH, JSON.stringify(providers, null, 2));
|
||||||
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}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute main function
|
// Execute main function with error handling
|
||||||
main().catch((error) => {
|
main().catch(error => {
|
||||||
console.error("Unhandled error:", error);
|
console.error('Unhandled error:', error);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user