File size: 3,570 Bytes
5775a1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# modules/api_utils.py
"""API utility functions for Wikimedia services"""

import requests
from typing import Dict, List, Optional
import time
from config.settings import (
    WIKIPEDIA_API,
    WIKIDATA_API,
    WIKIBOOKS_API,
    WIKI_REST_API,
    CACHE_TIMEOUT,
)

# Cache for API responses
_cache = {}


def _get_cached_or_fetch(url: str, params: Dict = None) -> Optional[Dict]:
    """Get cached response or fetch from API. Uses a simple in-memory cache."""
    cache_key = f"{url}_{str(params)}"

    if cache_key in _cache:
        cached_data, timestamp = _cache[cache_key]
        if time.time() - timestamp < CACHE_TIMEOUT: # Fixed: changed _cache_timeout to CACHE_TIMEOUT
            return cached_data

    try:
        response = requests.get(
            url, params=params, timeout=10
        )  # Increased timeout for robustness
        if response.status_code == 200:
            data = response.json()
            _cache[cache_key] = (data, time.time() + CACHE_TIMEOUT)
            return data
    except requests.exceptions.RequestException as e:
        print(f"API request error: {e}")

    return None


def fetch_wikipedia_summary(topic: str) -> Optional[Dict]:
    """Fetch Wikipedia page summary with caching"""
    return _get_cached_or_fetch(f"{WIKI_REST_API}{topic}")


def search_wikipedia(query: str, limit: int = 5) -> List[str]:
    """Search Wikipedia for topics"""
    params = {"action": "opensearch", "search": query, "limit": limit, "format": "json"}

    data = _get_cached_or_fetch(WIKIPEDIA_API, params)
    if data and len(data) > 1:
        return data[1]
    return []


def fetch_wikidata_entity(entity_id: str) -> Optional[Dict]:
    """Fetch Wikidata entity information"""
    params = {
        "action": "wbgetentities",
        "ids": entity_id,
        "format": "json",
        "languages": "en",
    }

    return _get_cached_or_fetch(WIKIDATA_API, params)


def fetch_wikipedia_categories(page_title: str) -> List[str]:
    """Fetch categories for a Wikipedia page"""
    params = {
        "action": "query",
        "prop": "categories",
        "titles": page_title,
        "format": "json",
        "cllimit": 10,
    }

    data = _get_cached_or_fetch(WIKIPEDIA_API, params)
    if data:
        pages = data.get("query", {}).get("pages", {})
        for page_id, page_data in pages.items():
            categories = page_data.get("categories", [])
            return [cat["title"].replace("Category:", "") for cat in categories]
    return []


def fetch_related_topics(topic: str, limit: int = 5) -> List[str]:
    """Fetch related topics from Wikipedia"""
    params = {
        "action": "query",
        "list": "search",
        "srsearch": topic,
        "srlimit": limit,
        "format": "json",
    }

    data = _get_cached_or_fetch(WIKIPEDIA_API, params)
    if data:
        search_results = data.get("query", {}).get("search", [])
        return [
            result["title"] for result in search_results if result["title"] != topic
        ]
    return []


def fetch_wikibooks_content(topic: str) -> Optional[str]:
    """Fetch content from Wikibooks"""
    params = {"action": "query", "list": "search", "srsearch": topic, "format": "json"}

    data = _get_cached_or_fetch(WIKIBOOKS_API, params)
    if data:
        search_results = data.get("query", {}).get("search", [])
        if search_results:
            return search_results[0].get("snippet", "")
    return None