Created
December 30, 2024 19:56
-
-
Save avinayak/d8256a6a04e270348213522c1ff19a9f to your computer and use it in GitHub Desktop.
random wiki article cloudflare worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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