import requests import urllib.parse from typing import Dict, Any def fetch_wikipedia_summary(query: str) -> Dict[str, Any]: """Fetches a Wikipedia summary using the REST API (No Key).""" encoded_query = urllib.parse.quote(query) url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{encoded_query}" try: response = requests.get(url, headers={"User-Agent": "Friday-Omega-AI/1.0"}) if response.status_code == 200: data = response.json() return {"status": "success", "title": data.get("title"), "extract": data.get("extract")} elif response.status_code == 404: return {"status": "error", "message": f"Wikipedia page not found for '{query}'"} else: return {"status": "error", "message": f"HTTP {response.status_code}"} except Exception as e: return {"status": "error", "message": str(e)} def fetch_nominatim_geocode(location: str) -> Dict[str, Any]: """Geocodes a location to latitude and longitude using Nominatim (No Key).""" url = "https://nominatim.openstreetmap.org/search" params = {"q": location, "format": "json", "limit": 1} try: response = requests.get(url, params=params, headers={"User-Agent": "Friday-Omega-AI/1.0"}) if response.status_code == 200: data = response.json() if data and len(data) > 0: return { "status": "success", "lat": data[0]["lat"], "lon": data[0]["lon"], "display_name": data[0]["display_name"] } return {"status": "error", "message": "Location not found."} return {"status": "error", "message": f"HTTP {response.status_code}"} except Exception as e: return {"status": "error", "message": str(e)} def fetch_open_meteo_weather(lat: float, lon: float) -> Dict[str, Any]: """Fetches current weather from Open-Meteo (No Key).""" url = "https://api.open-meteo.com/v1/forecast" params = { "latitude": lat, "longitude": lon, "current_weather": "true" } try: response = requests.get(url, params=params) if response.status_code == 200: return {"status": "success", "weather": response.json().get("current_weather", {})} return {"status": "error", "message": f"HTTP {response.status_code}"} except Exception as e: return {"status": "error", "message": str(e)} def fetch_exchange_rate(base_currency: str = "USD") -> Dict[str, Any]: """Fetches exchange rates via ExchangeRate-API free tier (No Key).""" url = f"https://open.er-api.com/v6/latest/{base_currency.upper()}" try: response = requests.get(url) if response.status_code == 200: return {"status": "success", "rates": response.json().get("rates", {})} return {"status": "error", "message": f"HTTP {response.status_code}"} except Exception as e: return {"status": "error", "message": str(e)} def fetch_public_ip() -> Dict[str, Any]: """Fetches public IP address via IPify (No Key).""" url = "https://api.ipify.org?format=json" try: response = requests.get(url) if response.status_code == 200: return {"status": "success", "ip": response.json().get("ip")} return {"status": "error", "message": f"HTTP {response.status_code}"} except Exception as e: return {"status": "error", "message": str(e)} def fetch_world_time(timezone: str) -> Dict[str, Any]: """Fetches current time for a timezone from WorldTimeAPI (No Key). Example: Europe/London""" url = f"http://worldtimeapi.org/api/timezone/{timezone}" try: response = requests.get(url) if response.status_code == 200: data = response.json() return {"status": "success", "datetime": data.get("datetime"), "timezone": data.get("timezone")} return {"status": "error", "message": f"HTTP {response.status_code}"} except Exception as e: return {"status": "error", "message": str(e)}