forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki_random.py
58 lines (49 loc) · 1.44 KB
/
wiki_random.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
"""Author Anurag Kumar(mailto:[email protected])
Module for Fetching Random Wiki Pages and asking user for opening one of them
Python:
- 3.5
Requirements:
- requests
- json
- webbrowser
Usage:
- $python3 wiki_random.py
enter index of article you would like to see, or 'r' for retry and 'n' for exit.
"""
import requests
import webbrowser
page_count = 10
url = (
"https://en.wikipedia.org/w/api.php?action=query&list=random&rnnamespace=0&rnlimit="
+ str(page_count)
+ "&format=json"
)
def load():
response = requests.get(url)
if response.ok:
jsonData = response.json()["query"]["random"]
print("10 Random generted WIKI pages...")
for idx, j in enumerate(jsonData):
print(str(idx) + ": ", j["title"])
i = input(
"Which page you want to see, enter index..[r: for retry,n: exit]?"
).lower()
if i == "r":
print("Loading randoms again...")
elif i == "n":
print("Auf Wiedersehen!")
return
else:
try:
jsonData[int(i)]["id"]
except Exception:
raise Exception("Wrong Input...")
print("taking you to the browser...")
webbrowser.get().open(
"https://en.wikipedia.org/wiki?curid=" + str(jsonData[int(i)]["id"])
)
load()
else:
response.raise_for_status()
if __name__ == "__main__":
load()