-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWiki.py
64 lines (62 loc) · 1.67 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
"""This plays a game based on the notion that all 1st links on wikipedia lead to https://en.wikipedia.org/wiki/Philosophy
https://en.wikipedia.org/wiki/Wikipedia:Getting_to_Philosophy this is a page better explaining the game
Created by Jordan Camilletti"""
import webbrowser
import urllib
from urllib.request import urlopen
randomUrl="https://en.wikipedia.org/wiki/Special:Random"
url="https://en.wikipedia.org/wiki/Special:Random"
prompt=""
goAgain="N"
while(goAgain!="Y"):
print("Do you want to use a random article?(Y/N)")
rndPage=input()
print("Do you want the wiki pages to open?(Y/N)")
open_page=input()
if(rndPage=="N"):
print("Enter starting prompt")
prompt=input()
route=[]
while(prompt!="Philosophy" and prompt!="Philosophical"):
if(rndPage=="N"):
route.append(prompt)
url="https://en.wikipedia.org/wiki/"+prompt
prompt=""
lnk_found=False
if(open_page=="Y"):
webbrowser.open(url)
page=urllib.request.urlopen(url)
while(page.readline() or lnk_found==False):
lne=page.readline()
if(b'<p>' in lne and b'<a href="/wiki/' in lne):
lnk_found=True
break
lnk_found=False
par=False
line=lne.decode("utf-8")
for x in range(len(line)):
if(line[x]=="("):
par=True
if(line[x]==")"):
par=False
if(not par):
if(line[x-15:x]=='<a href="/wiki/'):
newWrd=line[x:]
for n in range(len(newWrd)):
try:
if(newWrd[n]!='"'):
prompt+=newWrd[n]
else:
newWrd=""
except IndexError:
lnk_found=True
break
if(lnk_found):
break
print(prompt)
if(prompt in route):
print("Endless loop encountered!")
break
print(route)
print("\nDo you want to go again?")
goAgain=input()