Merge pull request #69 from milandeepak/main

Add remove_404.js
pull/59/head
Nilesh 2023-03-28 23:33:44 +05:30 zatwierdzone przez GitHub
commit 3a03bc7cf0
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 56 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,56 @@
import fs from 'fs';
import https from 'https';
import http from 'http';
import items from "../db/hello.json" assert {type: "json"};
function checkLink(url) {
return new Promise((resolve, reject) => {
let protocol = url.startsWith('https') ? https : http;
protocol.get(url, (res) => {
if (res.statusCode === 404) {
resolve(false);
} else {
resolve(true);
}
}).on('error', (err) => {
reject(err);
});
});
}
async function checkLinks(item, dryRun) {
if (!item.links || item.links.length === 0) {
return;
}
for (let i = 0; i < item.links.length; i++) {
const linkParts = item.links[i].split('|');
const url = linkParts[1];
try {
const linkExists = await checkLink(url);
if (!linkExists) {
if (dryRun) {
console.log(`[DRY RUN] Would remove 404 link from ${item.name}: ${url}`);
} else {
item.links.splice(i, 1);
i--;
console.log(`Removed 404 link from ${item.name}: ${url}`);
}
}
} catch (error) {
console.error(`Error checking link ${url}: ${error}`);
}
}
}
async function checkAllLinks(dryRun) {
for (const item of items) {
await checkLinks(item, dryRun);
}
if (!dryRun) {
fs.writeFileSync('db/hello.json', JSON.stringify(items));
console.log('Updated hello.json');
}
}
const dryRun = process.argv.includes('--dry-run');
checkAllLinks(dryRun);