Created
March 15, 2025 02:12
-
-
Save MarketingPip/e7723b2d6cac3fe674fbd691239dcbb4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import wtf from "https://esm.sh/wtf_wikipedia"; | |
import wtf_plugin from "https://esm.sh/wtf-plugin-api"; | |
wtf.extend(wtf_plugin); | |
class CategoryPageBuilder { | |
constructor(category, maxDepth) { | |
this.category = category; | |
this.maxDepth = maxDepth; | |
this.results = []; | |
this.subcategoryResults = {}; // To store pages by subcategory titles | |
} | |
// Recursive function to fetch category pages | |
async getCategoryPagesWithDepth(category, depth, maxDepth) { | |
if (depth > maxDepth) return []; | |
let results = []; | |
let pages = await wtf.getCategoryPages(category); | |
console.log(pages) | |
let pageResults = pages.filter(item => item.type === "page"); | |
let subcategories = pages.filter(item => item.type === "subcat"); | |
// Add pages to the results | |
results.push(...pageResults); | |
// Recursively fetch subcategories and their pages | |
let subcategoryPromises = subcategories.map(subcat => | |
this.getCategoryPagesWithDepth(subcat.title, depth + 1, maxDepth) | |
); | |
let subcategoryResults = await Promise.all(subcategoryPromises); | |
subcategoryResults.flat().forEach(item => results.push(item)); | |
// Store subcategories and their respective pages | |
for (let subcat of subcategories) { | |
this.subcategoryResults[subcat.title] = []; | |
} | |
// Fetch pages for subcategories and correctly associate them | |
for (let subcat of subcategories) { | |
let subcategoryPages = await wtf.getCategoryPages(subcat.title); | |
let subcategoryEvents = subcategoryPages.filter(page => page.type === "page"); | |
this.subcategoryResults[subcat.title].push(...subcategoryEvents); | |
} | |
if( this.subcategoryResults?.length === 0 || this.subcategoryResults != null){ | |
this.subcategoryResults["Catergory:uncategorized"] = pageResults | |
} | |
return results; | |
} | |
// Start the recursive fetching | |
async build() { | |
this.results = await this.getCategoryPagesWithDepth(this.category, 1, this.maxDepth); | |
return this; // Return the instance to allow chaining | |
} | |
// Return the results as JSON | |
json() { | |
return JSON.stringify(this.results, null, 2); | |
} | |
// Return the results in a prettified format with subcategories organized | |
prettify() { | |
let organized = { | |
categoryTitle: this.category.split(':')[1], // Remove "Category:" prefix | |
subcategories: [] | |
}; | |
// Organize the subcategories and their events | |
for (let title in this.subcategoryResults) { | |
organized.subcategories.push({ | |
[title.split(':')[1]]: { // Key is subcategory title | |
events: this.subcategoryResults[title] // Subcategory pages | |
} | |
}); | |
} | |
return JSON.stringify(organized, null, 2); | |
} | |
} | |
// Main function to initiate the builder and process the request | |
async function getAllCategoryPages(category, maxDepth = 5) { | |
const builder = new CategoryPageBuilder(category, maxDepth); | |
await builder.build(); // Start the process of fetching and building results | |
return builder; // Return the builder instance for chaining methods | |
} | |
async function getCrimes(year, country) { | |
// Validate inputs | |
if (!Number.isInteger(year)) { | |
throw new Error("Year must be an integer."); | |
} | |
if (typeof country !== "string" || country.trim() === "") { | |
throw new Error("Country must be a non-empty string."); | |
} | |
// Format the category string | |
let category = `Category:${year}_crimes_in_${country.replace(/\s+/g, "_")}`; | |
// Create a CategoryPages instance | |
let categoryFetcher = await getAllCategoryPages(category, 3) | |
// Return both raw JSON and prettified results | |
return categoryFetcher | |
} | |
// Usage | |
// Australia | |
// the United States | |
// Canada | |
// Germany | |
// Europe | |
// Poland | |
// the United Kingdom | |
// China | |
// Asia | |
getCrimes(2007, "Canada").then(result => { | |
console.log(result.json()); // Output raw JSON | |
console.log(result.prettify()); // Output prettified structure | |
}); | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sports fetcher.