| 9 | |
| 10 | |
| 11 | class WikidataQueryClient: |
| 12 | def __init__(self, url: str): |
| 13 | self.url = url |
| 14 | self.server = xmlrpc.client.ServerProxy(url) |
| 15 | |
| 16 | def label2qid(self, label: str) -> str: |
| 17 | return self.server.label2qid(label) |
| 18 | |
| 19 | def label2pid(self, label: str) -> str: |
| 20 | return self.server.label2pid(label) |
| 21 | |
| 22 | def pid2label(self, pid: str) -> str: |
| 23 | return self.server.pid2label(pid) |
| 24 | |
| 25 | def qid2label(self, qid: str) -> str: |
| 26 | return self.server.qid2label(qid) |
| 27 | |
| 28 | def get_all_relations_of_an_entity( |
| 29 | self, entity_qid: str |
| 30 | ) -> tp.Dict[str, tp.List]: |
| 31 | return self.server.get_all_relations_of_an_entity(entity_qid) |
| 32 | |
| 33 | def get_tail_entities_given_head_and_relation( |
| 34 | self, head_qid: str, relation_pid: str |
| 35 | ) -> tp.Dict[str, tp.List]: |
| 36 | return self.server.get_tail_entities_given_head_and_relation( |
| 37 | head_qid, relation_pid |
| 38 | ) |
| 39 | |
| 40 | def get_tail_values_given_head_and_relation( |
| 41 | self, head_qid: str, relation_pid: str |
| 42 | ) -> tp.List[str]: |
| 43 | return self.server.get_tail_values_given_head_and_relation( |
| 44 | head_qid, relation_pid |
| 45 | ) |
| 46 | |
| 47 | def get_external_id_given_head_and_relation( |
| 48 | self, head_qid: str, relation_pid: str |
| 49 | ) -> tp.List[str]: |
| 50 | return self.server.get_external_id_given_head_and_relation( |
| 51 | head_qid, relation_pid |
| 52 | ) |
| 53 | |
| 54 | def get_wikipedia_page(self, qid: str, section: str = None) -> str: |
| 55 | wikipedia_url = self.server.get_wikipedia_link(qid) |
| 56 | if wikipedia_url == "Not Found!": |
| 57 | return "Not Found!" |
| 58 | else: |
| 59 | response = requests.get(wikipedia_url) |
| 60 | if response.status_code != 200: |
| 61 | raise Exception(f"Failed to retrieve page: {wikipedia_url}") |
| 62 | |
| 63 | soup = BeautifulSoup(response.content, "html.parser") |
| 64 | content_div = soup.find("div", {"id": "bodyContent"}) |
| 65 | |
| 66 | # Remove script and style elements |
| 67 | for script_or_style in content_div.find_all(["script", "style"]): |
| 68 | script_or_style.decompose() |