Skip to content

Instantly share code, notes, and snippets.

@HusseinMorsy
Last active October 24, 2019 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HusseinMorsy/da3bda5af71dcbacb8c673f5c6f8b035 to your computer and use it in GitHub Desktop.
Save HusseinMorsy/da3bda5af71dcbacb8c673f5c6f8b035 to your computer and use it in GitHub Desktop.
Sitemap checker
// using:
// deno sitemap-checker.ts 400
// start at number 400 and tests the next 200
import readFileStrSync from "https://deno.land/std/fs/mod.ts";
async function checkUrl(url: string): Promise<boolean> {
const res = await fetch(url);
return res.status == 200;
}
function extractUrl(line: string): string {
let a = line.match(/https:\/\/www.flibco.com\/[^<\"]*/);
if (a) {
return a[0];
} else {
return "";
}
}
const content = readFileStrSync("./public/sitemap.xml", { encoding: "utf8" });
const lines = content.split("\n");
let i = 1;
let counter = 200;
let myurl: string;
let startLine: number = Number(Deno.args[1]);
for (const line of lines) {
myurl = extractUrl(line);
try {
if (i < startLine) {
i++;
} else if (counter > 0 && myurl.length > 0) {
i++;
if (!(await checkUrl(myurl))) console.log(myurl);
counter--;
}
} catch (error) {
console.log(`Error: ${myurl}`);
console.log(error);
Deno.exit(1);
}
}
Deno.exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment