-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRand_Wiki.py
75 lines (70 loc) · 2.52 KB
/
Rand_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
# Rand_Wiki.py Version 1.1
# The site to query
SITE = "https://en.wikipedia.org/wiki/"
# the page on the site to query
URL = SITE + "Wikipedia:Featured_articles"
# note that if you want to change either of these, you'll probably
# have to update the parsing code in the "except" below
SAVEFILE = 'wiki_pages.txt'
# Version History
# V 1 2021-08-01 Released
# V 1.1 2021-08-02 Fixed a couple small functionality problems
import webbrowser
from random import choice
try:
f = open(SAVEFILE,'r')
raw = f.readline()
f.close()
rem_pgs = eval(raw)
except: #rebuild the list
rem_pgs = []
from urllib.request import urlopen
with urlopen(URL) as f: html = f.read()
#second place you get 'class="hlist"'
cursor = html.find(b'class="hlist"')
cursor = html.find(b'class="hlist"',cursor+1)
# then make a list of all the html links
sstr = b'<a href="'
offset = len(sstr)
cursor = html.find(sstr,cursor) + offset
while cursor > offset:
end = html.find(b'"',cursor)
page = str(html[cursor:end])[2:-1]
if page[:6] != '/wiki/': break
if page.find('User:') > 0: break
if page.find('Help:') > 0: break
if page.find('Category:') > 0: break
page = page[6:]
rem_pgs.append(page)
cursor = html.find(sstr,end) + offset
inchoice = "?"
while True:
if len(rem_pgs) == 0:
input("There are no more pages. Press return to close.")
break
printflag = False
if len(inchoice)!= 0:
inchoice = inchoice.lower()
initial = inchoice[0]
if initial == 's':
print("Saving")
f = open(SAVEFILE,'w')
f.write(str(rem_pgs))
f.close()
if inchoice == 'sc': break
else: print('{} pages left'.format(len(rem_pgs)))
inchoice = input('Saved: ')
continue
elif initial == 'p':
print(rem_pgs)
inchoice = input('Those are the currently loaded remaining pages :')
continue
elif inchoice[-1] == '?':
print('{} pages left'.format(len(rem_pgs)))
print('Enter to bring up a Featured Wikipedia page\n"s" to save, "sc" to save and close, "p" to print')
inchoice = input('What would you like to do: ')
continue
idx = choice(range(len(rem_pgs)))
chosen_page = rem_pgs.pop(idx)
webbrowser.open(SITE + chosen_page)
inchoice = input('Page {} queued: '.format(chosen_page))