-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.py
105 lines (91 loc) · 4.28 KB
/
entity.py
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
from load_page import Load_page
from bs4 import BeautifulSoup
from color import Color
import os
class Entity:
def __init__(self,query):
self.parsed_page = Load_page(query)
self.soup = self.parsed_page.get_soup()
def get_title(self):
total_entities = self.soup.find('div',{'class':'entity-results'})
if total_entities==None:return False
self.entity_results = []
for i in total_entities:
if self.soup.find('div',{'class':'entity-results','class':'entity-title'})==None:return False
result = self.soup.find('div',{'class':'entity-results','class':'entity-title'}).text
if result not in self.entity_results:
self.entity_results.append(result)
return self.entity_results
def get_type(self):
total_entities = self.soup.find('div',{'class':'entity-results'})
if self.soup.find('div',{'class':'entity-results'})==None:return False
self.entity_results = []
for i in total_entities:
if self.soup.find('div',{'class':'entity-results','class':'entity-type'})==None:return False
result = self.soup.find('div',{'class':'entity-results','class':'entity-type'}).text
if result not in self.entity_results:
self.entity_results.append(result)
return self.entity_results
def get_description(self):
total_entities = self.soup.find('div',{'class':'entity-results'})
if total_entities==None:return False
self.entity_results = []
for i in total_entities:
if self.soup.find('div',{'class':'entity-results','class':'entity-description'})==None:return False
result = self.soup.find('div',{'class':'entity-results','class':'entity-description'}).text
try: result = result.replace('\n','')
except: pass
try: result = result.replace('Read more','')
except: pass
result = result.strip()
if result not in self.entity_results:
self.entity_results.append(result)
return self.entity_results
def get_url(self):
if self.soup.find('div',{'class':'entity-section','class':'entity-url'})==None:return False
total_entities = self.soup.find('div',{'class':'entity-section','class':'entity-url'}).find_all('a')
self.entity_results = []
for i in total_entities:
result = i.get('href')
if result not in self.entity_results:
self.entity_results.append(result)
return self.entity_results
def get_wikipedia_link(self):
if self.soup.find('div',{'class':'entity-content','class':'entity-contract'})==None:return False
total_entities = self.soup.find('div',{'class':'entity-content','class':'entity-contract'}).find_all('a')
self.entity_results = []
for i in total_entities:
result = i.get('href')
if result not in self.entity_results:
self.entity_results.append(result)
return self.entity_results
def return_soup(self):
return self.soup
def get_spell_title(self):
return self.parsed_page.get_spell_title()
def get_spell_description(self):
return self.parsed_page.get_spell_description()
# for testing purpose
if __name__ == "__main__":
entity = Entity(input('Query:: '))
os.system('clear')
if not entity.get_title():
print('Results Not Found!..')
exit()
color = Color()
if entity.get_spell_title():
print()
print(color.GREEN+entity.get_spell_title()+color.DEFAULT)
if entity.get_spell_description():
print(color.RED+entity.get_spell_description()+color.DEFAULT)
print()
print(f'Title :: {entity.get_title()[0]}')
type_ = entity.get_type()[0] if entity.get_type() else "Unknown"
print(f'Type :: {type_}')
description = entity.get_description()[0] if entity.get_description() else "Unknown"
print('\nDescription :: '+color.YELLOW+f'{description}'+color.DEFAULT)
print()
url = entity.get_url()[0] if entity.get_url() else "Unknown"
print(f'URL :: {url}')
wikipedia = entity.get_wikipedia_link()[0] if entity.get_wikipedia_link() else "Unknown"
print(f'Wikipedia :: {wikipedia}\n')