-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki.py
228 lines (196 loc) Β· 7.05 KB
/
wiki.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import wikipedia, sys
import time
def Main():
Wiki = WikipediaSearch()
Wiki.Start()
class WikipediaSearch:
def __init__(self):
self.title = ""
self.url = ""
self.content = ""
self.images = []
self.links = []
def Start(self):
self.StartDisplay()
StartMenuChoice = self.StartMenu()
if StartMenuChoice == 1:
Query = self.RetrieveUserQuery()
self.Search(Query)
elif StartMenuChoice == 2:
self.RandomArticle()
self.DisplayContentOrSummary()
def PageDetails(self):
while True:
print()
print("π What would you like to see?")
print("1. π Title")
print("2. π URL")
print("3. πΌοΈ Images")
print("4. π Links")
print()
choice = input("Enter your choice (1, 2, 3, or 4): ").strip()
if choice == "1":
print(f"π Title: {self.ReturnTitle()}")
print()
break
elif choice == "2":
print(f"π URL: {self.ReturnUrl()}")
print()
break
elif choice == "3":
print(self.ReturnImages())
print()
break
elif choice == "4":
print(self.ReturnLinks())
print()
break
else:
print("β Invalid choice. Please try again.")
def DisplayContentOrSummary(self):
while True:
print()
print(
"π Do you want to see the content, the summary, the details of the article, or choose another article?"
)
print("1. π Content")
print("2. π Summary")
print("3. π Details")
print("4. π Choose another article")
print("Q. β Quit")
print()
choice = input("Enter your choice (1, 2, 3, 4, or Q): ").strip()
print()
if choice == "1":
content = self.ReturnContent()
print(content)
time.sleep(5)
elif choice == "2":
summary = self.ReturnSummary()
print(summary)
time.sleep(5)
elif choice == "3":
self.PageDetails()
elif choice == "4":
self.Start()
elif choice.upper() == "Q":
self.Quit()
else:
print("β Invalid choice. Please try again.")
def StartMenu(self):
print()
print("Please choose an option:")
print("1. π Search for an article")
print("2. π² Get a random article")
print()
while True:
choice = input("Enter your choice (1 or 2): ").strip()
if choice.isdigit() and int(choice) in [1, 2]:
return int(choice)
else:
print("β Invalid choice. Please try again.")
def StartDisplay(self):
print()
print("===========================")
print("π Welcome to Wikipedia π")
print("===========================")
print()
def Search(self, query):
print()
QueryResults = self.SearchWikipedia(query)
if len(QueryResults) > 0:
query = self.Options(QueryResults)
if query:
self.SearchValues(query)
def Options(self, options):
print(f"There are {len(options)} options available.")
for i, option in enumerate(options, start=1):
print(f"{i}. {option}")
print()
while True:
choice = input("Please select an option by number: ").strip()
if choice.isdigit() and 1 <= int(choice) <= len(options):
return options[int(choice) - 1]
else:
print("β Invalid choice. Please try again.")
def SearchWikipedia(self, query):
print()
try:
search = wikipedia.search(query, results=5)
return search
except wikipedia.exceptions.PageError:
return "β Page not found."
except wikipedia.exceptions.DisambiguationError as e:
return f"β Disambiguation error: {e.options}"
except wikipedia.exceptions.RedirectError:
return "β Redirect error."
except wikipedia.exceptions.HTTPTimeoutError:
return "β HTTP timeout error."
except wikipedia.exceptions.WikipediaException as e:
return f"β An error occurred: {e}"
def SearchValues(self, query):
try:
Result = wikipedia.page(query)
self.title = Result.title
self.url = Result.url
self.content = Result.content
self.images = Result.images
self.links = Result.links
except wikipedia.exceptions.PageError:
print("β Page not found.")
except wikipedia.exceptions.DisambiguationError as e:
print(f"β Disambiguation error: {e.options}")
except wikipedia.exceptions.RedirectError:
print("β Redirect error.")
except wikipedia.exceptions.HTTPTimeoutError:
print("β HTTP timeout error.")
except wikipedia.exceptions.WikipediaException as e:
print(f"β An error occurred: {e}")
def ReturnSummary(self):
try:
summary = wikipedia.summary(self.title, auto_suggest=False)
return summary
except wikipedia.exceptions.DisambiguationError as e:
return f"β Disambiguation error: {e.options}"
except wikipedia.exceptions.PageError:
return "β Page not found."
except wikipedia.exceptions.RedirectError:
return "β Redirect error."
except wikipedia.exceptions.HTTPTimeoutError:
return "β HTTP timeout error."
except wikipedia.exceptions.WikipediaException as e:
return f"β An error occurred: {e}"
def ReturnContent(self):
return self.content
def ReturnTitle(self):
return self.title
def ReturnUrl(self):
return self.url
def ReturnLinks(self):
formatted_links = "\n".join(self.links)
return f"π Links:\n{formatted_links}"
def ReturnImages(self):
formatted_images = "\n".join(self.images)
return f"πΌοΈ Images:\n{formatted_images}"
def RandomArticle(self):
query = wikipedia.random(pages=1)
self.SearchValues(query)
print(f"π Title: {self.ReturnTitle()}")
def RetrieveUserQuery(self):
query = input("π What do you want to search for? (Q for exit) ").strip()
if query.upper() == "Q":
self.Quit()
if not query:
print("β Please enter a query")
print()
return self.RetrieveUserQuery()
return query
def Quit(self):
print()
print("β" * 6)
print("β Exiting β")
print("β" * 6)
print()
sys.exit(0)
if __name__ == "__main__":
Main()