File size: 872 Bytes
456f698
 
 
 
 
 
 
182e542
456f698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests


_WIKTIONARY_CACHE = {}

class WiktionaryClient:
    """Simple wrapper for Wiktionary API queries."""
    BASE_URL = "https://en.wiktionary.org/w/api.php"
    HEADERS = {"User-Agent": "MyChineseIdiomApp/1.0 ([email protected])"}

    def exists(self, term: str) -> bool:
        import requests

        if term in _WIKTIONARY_CACHE:
            return _WIKTIONARY_CACHE[term]

        try:
            params = {"action": "query", "titles": term, "format": "json"}
            response = requests.get(self.BASE_URL, params=params, headers=self.HEADERS, timeout=5)
            response.raise_for_status()
            data = response.json()
            pages = data.get("query", {}).get("pages", {})
            exists = "-1" not in pages
        except Exception:
            exists = False

        _WIKTIONARY_CACHE[term] = exists
        return exists