import requests from bs4 import BeautifulSoup def fetch_facts(game_mode, path): """Fetch facts about a country from the Kids World Travel Guide website""" base_url = "https://www.kids-world-travel-guide.com" if game_mode == "Countries of the World" else "https://kids.nationalgeographic.com/geography/states/article/" full_url = base_url + path try: response = requests.get(full_url, timeout=10) response.raise_for_status() # Parse the HTML content soup = BeautifulSoup(response.content, 'html.parser') # Extract relevant facts - looking for common patterns in the website facts = [] # Look for fact sections, lists, and key information # This is a basic parser - you might need to adjust based on the actual HTML structure # Try to find paragraphs with factual content paragraphs = soup.find_all('p') for p in paragraphs[:10]: # Limit to first 10 paragraphs to avoid too much content text = p.get_text().strip() if len(text) > 50 and not text.startswith('Related'): # Filter out short texts and navigation facts.append(text) # Look for list items that might contain facts list_items = soup.find_all('li') for li in list_items[:20]: # Limit to avoid too much content text = li.get_text().strip() if len(text) > 20 and len(text) < 450: # Filter for reasonable fact lengths facts.append(text) # Join facts with newlines, limit total length facts_text = '\n'.join(facts[:20]) # Limit to 20 facts # Truncate if too long to avoid token limits if len(facts_text) > 2000: facts_text = facts_text[:2000] + "..." return facts_text except Exception as e: print(f"Error fetching facts for {path}: {str(e)}") return "Unable to fetch additional facts about this country."