-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSRS_World_Pop_Tracker
136 lines (89 loc) · 3.59 KB
/
OSRS_World_Pop_Tracker
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
import urllib.request
import time
import string
worldDict = {}
class Del: # needed to remove trash from pop_num later on
# http://stackoverflow.com/questions/1450897/python-removing-characters-except-digits-from-string
def __init__(self, keep=string.digits):
self.comp = dict((ord(c), c) for c in keep)
def __getitem__(self, k):
return self.comp.get(k)
DD = Del()
def main():
my_str = get_html()
goto_worlds = my_str.find("<tbody class='server-list__body'>")
go_through_worlds(goto_worlds)
iteration = 0
try:
while True: # main loop
get_pops(goto_worlds)
add_pops(iteration)
time.sleep(60)
iteration += 1
except KeyboardInterrupt:
pass
def go_through_worlds(last_position): # gets world numbers from the html sheet, makes them keys in worldDict
my_str = get_html()
while my_str.find("world=", last_position) != -1:
pos = my_str.find("world=", last_position)
world_num = int(my_str[pos + 6:pos + 9])
worldDict[world_num] = []
last_position = pos + 10
return worldDict
def get_pops(last_position): # gets population numbers from the html sheet, makes them values in the list of worldDict
my_str = get_html()
while my_str.find("world=", last_position) != -1:
for keys in sorted(worldDict.keys()):
pos = my_str.find("world=" + str(keys), last_position)
pop_num = my_str[pos + 140:pos + 145]
pop_num = pop_num.translate(DD)
try:
pop_num = int(pop_num)
except ValueError:
pop_num = "down"
worldDict[keys].append(pop_num)
last_position = pos + 8
return worldDict
def get_html(): # gets the html sheet from the url given, converts it to a string
my_url = urllib.request.urlopen("http://oldschool.runescape.com/slu?order=WmpLA")
my_bytes = my_url.read()
my_str = my_bytes.decode("utf8")
my_url.close()
return my_str
def add_pops(i): # subtracts last known pop from current pop, then prints this for every world to show changes
j = 0
if i == 0:
for keys in sorted(worldDict.keys()):
if j < 4:
if worldDict[keys][i] == 'down':
print(str(keys) + " : n/a", end=" || ")
j += 1
else:
print(str(keys) + " : " + str("{:4d}".format(worldDict[keys][i])), end=" || ")
j += 1
else:
if worldDict[keys][i] == 'down':
print(str(keys) + " : n/a", end="\n")
j = 0
else:
print(str(keys) + " : " + str("{:4d}".format(worldDict[keys][i])), end="\n")
j = 0
else:
for keys in sorted(worldDict.keys()):
if j < 4:
if worldDict[keys][i] == 'down':
print(str(keys) + " : n/a", end=" || ")
j += 1
else:
curr_change = worldDict[keys][i] - worldDict[keys][i - 1]
print(str(keys) + " : " + str('{:+3d}'.format(curr_change)), end=" || ")
j += 1
else:
if worldDict[keys][i] == 'down':
print(str(keys) + " : n/a", end="\n")
j = 0
else:
curr_change = worldDict[keys][i] - worldDict[keys][i - 1]
print(str(keys) + " : " + str('{:+3d}'.format(curr_change)), end="\n")
j = 0
main()