Skip to content

Instantly share code, notes, and snippets.

@avinayak
Created December 30, 2024 19:56
Show Gist options
  • Save avinayak/d8256a6a04e270348213522c1ff19a9f to your computer and use it in GitHub Desktop.
Save avinayak/d8256a6a04e270348213522c1ff19a9f to your computer and use it in GitHub Desktop.
random wiki article cloudflare worker
export default {
async fetch(request) {
// Define the topics and corresponding Wikipedia random URLs
const topics = [
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Data_structures",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Algorithms",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Computer_science",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Cognitive_biases",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Mathematics",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Data_structures",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Algorithms",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Computer_science",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Cognitive_biases",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Mathematics",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Data_structures",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Algorithms",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Computer_science",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Cognitive_biases",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Mathematics",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Data_structures",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Algorithms",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Computer_science",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Cognitive_biases",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Mathematics",
// -- i like these less but i want them
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Philosophy",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Engineering",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Linguistics",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Psychology",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Sociology",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/Astronomy",
"https://en.wikipedia.org/wiki/Special:RandomInCategory/The_arts",
];
// Function to get a random topic URL
function getRandomTopicUrl() {
const randomIndex = Math.floor(Math.random() * topics.length);
const randomUrl = topics[randomIndex];
console.log(`Selected random topic URL: ${randomUrl}`);
return randomUrl;
}
// Function to check if the URL is a category page
function isCategoryPage(url) {
const isCategory = url.includes("/wiki/Category:");
console.log(`Checking if URL is a category: ${url} -> ${isCategory}`);
return isCategory;
}
// Follow redirects and resolve to an article or retry within a category
async function resolveToArticle(url) {
let currentUrl = url;
while (true) {
console.log(`Fetching URL: ${currentUrl}`);
const response = await fetch(currentUrl, { redirect: "manual" });
console.log(`Response status: ${response.status}`);
if (response.status === 302) {
currentUrl = response.headers.get("Location");
console.log(`Redirected to: ${currentUrl}`);
if (!currentUrl) {
console.error("Redirect URL is missing, stopping resolution.");
break;
}
if (!isCategoryPage(currentUrl)) {
console.log(`Resolved to article: ${currentUrl}`);
return currentUrl;
} else {
console.log(`Still in category, navigating deeper with Special:RandomInCategory.`);
const categoryName = currentUrl.split("/wiki/Category:")[1];
currentUrl = `https://en.wikipedia.org/wiki/Special:RandomInCategory/${categoryName}`;
}
} else if (response.status === 200) {
console.log(`Loaded content successfully from: ${currentUrl}`);
const html = await response.text();
if (!html.includes("/wiki/Category:")) {
console.log(`Resolved to article: ${currentUrl}`);
return currentUrl;
} else {
console.log(`Still in category, extracting random link.`);
const match = html.match(/href="(\/wiki\/[^:]+)"/);
if (match) {
currentUrl = "https://en.wikipedia.org" + match[1];
console.log(`Navigating to extracted link: ${currentUrl}`);
} else {
console.error("No valid links found in category page, stopping resolution.");
break;
}
}
} else {
console.error(`Failed to fetch URL: ${currentUrl} with status: ${response.status}`);
break;
}
}
console.log("Fallback to a truly random page: https://en.wikipedia.org/wiki/Special:Random");
return "https://en.wikipedia.org/wiki/Special:Random"; // Fallback to a truly random page
}
// Get a random topic URL and resolve it
const randomUrl = getRandomTopicUrl();
console.log(`Starting resolution for random URL: ${randomUrl}`);
const resolvedUrl = await resolveToArticle(randomUrl);
console.log(`Final resolved URL: ${resolvedUrl}`);
console.log("--------------------------------------")
return Response.redirect(resolvedUrl, 302);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment