File size: 2,356 Bytes
76887e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests

class WikiDBAPI:
    def __init__(self, api_endpoint):
        """Initialize the database with API endpoint URL"""
        self.api_endpoint = api_endpoint.rstrip('/')
        # Verify connection and get initial article count
        self._article_count = self._get_article_count()
        print(f"Connected to Wiki API at {api_endpoint} with {self._article_count} articles")
    
    def __del__(self):
        """Clean up resources when object is destroyed"""
        # No persistent connection to close in the API version
        pass
    
    def _get_article_count(self):
        """Get the number of articles via API"""
        response = requests.get(f"{self.api_endpoint}/article_count")
        response.raise_for_status()
        return response.json()["count"]
    
    def get_article_count(self):
        """Return the number of articles"""
        return self._article_count
        
    def get_all_article_titles(self):
        """Return a list of all article titles"""
        response = requests.get(f"{self.api_endpoint}/article_titles")
        response.raise_for_status()
        return response.json()["titles"]
        
    def get_article(self, title):
        """Get article data by title"""
        response = requests.get(f"{self.api_endpoint}/article", params={"title": title})
        if response.status_code == 404:
            return {}
        response.raise_for_status()
        return response.json()
        
    def article_exists(self, title):
        """Check if an article exists"""
        response = requests.get(f"{self.api_endpoint}/article_exists", params={"title": title})
        response.raise_for_status()
        return response.json()["exists"]
        
    def get_article_text(self, title):
        """Get the text of an article"""
        response = requests.get(f"{self.api_endpoint}/article_text", params={"title": title})
        if response.status_code == 404:
            return ''
        response.raise_for_status()
        return response.json()["text"]
        
    def get_article_links(self, title):
        """Get the links of an article"""
        response = requests.get(f"{self.api_endpoint}/article_links", params={"title": title})
        if response.status_code == 404:
            return []
        response.raise_for_status()
        return response.json()["links"]