-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathosrs.py
205 lines (173 loc) · 6.19 KB
/
osrs.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
name = "OSRS_Hiscores"
import http.client
import math
from sys import exit
__status__ = "Closed (Unsupported)"
class Hiscores(object):
"""Hiscores class
The Hiscores class deals with collecting the required
information needed to fetch user information from in-game
API. After being supplied necessary information, Hiscores
class then sets self.stats dictionary with user information.
Args:
self,
username str: The username of the account that you
want to look up information on.
actype str: The account type of the account that
you want to lookup. If not supplied
this argument defaults to 'N' Normal.
Returns:
This object returns nothing. Instead it sets the
value of self.stats with a dictionary of values
keyed by the skill type. Example: self.stats['attack']
Example Invocation:
from OSRS-Hiscores import Hiscores
account = Hiscores('Zezima', 'N')
print(account.stats['attack']['level']) # displays attack level
"""
def __init__(self, username: str, actype='N'):
self.username = username
self.accountType = actype.upper()
self.getHTTPResponse()
def getHTTPResponse(self):
"""getHTTPResponse() method
The getHTTPResponse() method communicates with the OSRS Hiscores API
and supplies the required information from self.username and
self.actype to pull the given users stats and hiscore information.
Args:
self
Returns:
None
Triggers:
self.processResponse(): This method is always triggered, regardless
of whether or not the query to the API returned
successfully or not.
"""
conn = http.client.HTTPSConnection('secure.runescape.com')
if self.accountType == 'N':
conn.request("GET", "/m=hiscore_oldschool/index_lite.ws?player={}".format(self.username))
self.response = conn.getresponse()
self.status = self.response.status
elif self.accountType == 'IM':
conn.request("GET", "/m=hiscore_oldschool_ironman/index_lite.ws?player={}".format(self.username))
self.response = conn.getresponse()
self.status = self.response.status
elif self.accountType == "UIM":
conn.request("GET", "/m=hiscore_oldschool_ultimate/index_lite.ws?player={}".format(self.username))
self.response = conn.getresponse()
self.status = self.response.status
elif self.accountType == "HIM":
conn.request("GET", "/m=hiscore_oldschool_hardcore_ironman/index_lite.ws?player={}".format(self.username))
self.response = conn.getresponse()
self.status = self.response.status
elif self.accountType == "S":
conn.request("GET", "/m=hiscore_oldschool_seasonal/index_lite.ws?player={}".format(self.username))
self.response = conn.getresponse()
self.status = self.response.status
self.processResponse()
def processResponse(self):
"""processResponse() method
The processResponse() method processes the response received during the
getHTTPResponse() method. It handles potential 404 errors, 403 errors
and the like and sets self.errorMsg accordingly. On successful Response
data is stored in self.data and sent to the self.parseData() method.
Args:
self
Returns:
None
Triggers:
self.error(): This method is triggered whenever the self.status of
a request is not 200 (failed).
self.parseData(): This method is called when self.status is 200 (success)
"""
if self.status != 200:
self.errorMsg = "Player name given not found in account type provided. Valid account types are, 'N' (Normal), 'IM' (Iron Man), 'UIM' (Ultimate Iron Man), 'HIC' (Hardcore Iron Man)"
self.error()
else:
self.data = self.response.read().decode('ascii')
self.parseData()
def parseData(self):
"""parseData() method
The parseData() method parses the self.data processed in the processResponse()
method. Data parsed in placed in the self.stats dictionary.
Args:
self
Returns:
None
Triggers:
None
"""
self.data = self.data.replace('\n',',')
self.data = self.data.split(',')
subset = {}
# Totals
info = {}
info['rank'] = self.data[0]
info['level'] = self.data[1]
info['experience'] = self.data[2]
subset['total'] = info
skills = [
'attack',
'defense',
'strength',
'hitpoints',
'ranged',
'prayer',
'magic',
'cooking',
'woodcutting',
'fletching',
'fishing',
'firemaking',
'crafting',
'smithing',
'mining',
'herblore',
'agility',
'thieving',
'slayer',
'farming',
'runecrafting',
'hunter',
'construction'
]
counter = 0
for i in range(len(skills)):
info = {}
info['rank'] = int(self.data[counter+3])
info['level'] = int(self.data[counter+4])
info['experience'] = int(self.data[counter+5])
level = int(info['level']+1)
info['next_level_exp'] = math.floor(sum((math.floor(level + 300 * (2 ** (level / 7.0))) for level in range(1, level)))/4)
info['exp_to_next_level'] = int(info['next_level_exp'] - info['experience'])
subset[skills[i]] = info
counter += 3
# set stats dictionary
self.stats = subset
def skill(self, skill, stype: str = 'level'):
"""skill() method
The skill() method is a more dynamic, intuitive way to access stats
then the self.stats dictionary variable. It allows for a user to
provide the skill and stype (level, rank, experience) of the skill
they wish information on.
Args:
skill (str): The OSRS skill to get information on
stype (str): One of 'level', 'rank', or 'experience'
to receive information for. If not
supplied, stype is assumed to be
'level'
Returns:
self.stats[skill][stype] (int): The info you requested
"""
try:
if stype.lower() not in ['rank','level','experience','exp_to_next_level']:
raise "stype must be 'rank','level', or experience'"
exit(0)
else:
return self.stats[skill.lower()][stype.lower()]
except KeyError as KE:
print("ERROR: skill {} does not exist".format(KE))
exit(0)
def error(self):
print("Error occurred: {}".format(self.errorMsg))
exit(0)