Skip to content

Commit 7e641f4

Browse files
added wiki_random.py, use for generating 10 random WIKI pages,
and open a page according to request.
1 parent b707a97 commit 7e641f4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

wiki_random.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import requests, json, webbrowser
2+
3+
url = 'https://en.wikipedia.org/w/api.php?action=query&list=random&rnnamespace=0&rnlimit=10&format=json'
4+
5+
def load():
6+
response = requests.get(url)
7+
if response.ok:
8+
jsonData = response.json()['query']['random']
9+
print("10 Random generted WIKI pages...")
10+
for idx,j in enumerate(jsonData):
11+
print(str(idx)+": ",j['title'])
12+
i = input("Which page you want to see, enter index..[r: for retry,n: exit]?").lower()
13+
if i == 'r':
14+
print('Loading randoms again...')
15+
elif i=='n':
16+
print('Auf Wiedersehen!')
17+
return
18+
else:
19+
try: int(i)
20+
except Exception: raise Exception("Wrong Input...")
21+
print('taking you to the browser...')
22+
webbrowser.get().open('https://en.wikipedia.org/wiki?curid='+str(jsonData[int(i)]['id']))
23+
load()
24+
else:
25+
response.raise_for_status()
26+
27+
if __name__=='__main__':
28+
load()

0 commit comments

Comments
 (0)