From e0c7e9d912d2073c73c6f48661aa62048d57c0e9 Mon Sep 17 00:00:00 2001 From: Milan Deepak Date: Sun, 5 Mar 2023 14:59:59 +0530 Subject: [PATCH 1/2] add remove_404.js --- scripts/remove_404.js | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 scripts/remove_404.js diff --git a/scripts/remove_404.js b/scripts/remove_404.js new file mode 100644 index 0000000..17717e7 --- /dev/null +++ b/scripts/remove_404.js @@ -0,0 +1,48 @@ +import https from 'https'; +import http from 'http'; +import items from "../db/items.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) { + 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) { + 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() { + for (const item of items) { + await checkLinks(item); + } + fs.writeFileSync('db/items.json', JSON.stringify(items)); + console.log('Updated items.json'); +} + +checkAllLinks(); From a2b257d37fb179448bbab01b7e95027cdb3a14e1 Mon Sep 17 00:00:00 2001 From: Milan Deepak Date: Sun, 5 Mar 2023 15:05:43 +0530 Subject: [PATCH 2/2] update remove_404.js --- scripts/remove_404.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/scripts/remove_404.js b/scripts/remove_404.js index 17717e7..470036c 100644 --- a/scripts/remove_404.js +++ b/scripts/remove_404.js @@ -1,6 +1,7 @@ +import fs from 'fs'; import https from 'https'; import http from 'http'; -import items from "../db/items.json" assert {type: "json"}; +import items from "../db/hello.json" assert {type: "json"}; function checkLink(url) { return new Promise((resolve, reject) => { @@ -17,7 +18,7 @@ function checkLink(url) { }); } -async function checkLinks(item) { +async function checkLinks(item, dryRun) { if (!item.links || item.links.length === 0) { return; } @@ -27,9 +28,13 @@ async function checkLinks(item) { try { const linkExists = await checkLink(url); if (!linkExists) { - item.links.splice(i, 1); - i--; - console.log(`Removed 404 link from ${item.name}: ${url}`); + 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}`); @@ -37,12 +42,15 @@ async function checkLinks(item) { } } -async function checkAllLinks() { +async function checkAllLinks(dryRun) { for (const item of items) { - await checkLinks(item); + await checkLinks(item, dryRun); + } + if (!dryRun) { + fs.writeFileSync('db/hello.json', JSON.stringify(items)); + console.log('Updated hello.json'); } - fs.writeFileSync('db/items.json', JSON.stringify(items)); - console.log('Updated items.json'); } -checkAllLinks(); +const dryRun = process.argv.includes('--dry-run'); +checkAllLinks(dryRun);