-
Notifications
You must be signed in to change notification settings - Fork 28
/
items.py
131 lines (112 loc) · 3.63 KB
/
items.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
import sys
import traceback
import re
import mwparserfromhell as mw
import api
import util
from typing import *
import urllib.request
import json
"this isn't quite right, because 2h, but the format isn't smart enough for that"
slotIDs: Dict[str, int] = {
"weapon": 3,
"2h": 3,
"body": 4,
"head": 0,
"ammo": 13,
"legs": 7,
"feet": 10,
"hands": 9,
"cape": 1,
"neck": 2,
"ring": 12,
"shield": 5
}
def getLimits():
req = urllib.request.Request(
'https://oldschool.runescape.wiki/w/Module:GELimits/data.json?action=raw', headers=api.user_agent)
with urllib.request.urlopen(req) as response:
data = json.loads(response.read())
limits = {}
for itemname in data:
if itemname.startswith('%'):
continue
limits[itemname] = int(data[itemname])
return limits
def run():
limits = getLimits()
stats = {}
item_pages = api.query_category("Items")
for name, page in item_pages.items():
if name.startswith("Category:"):
continue
try:
code = mw.parse(page, skip_style_tags=True)
if util.has_template("Interface items", code) or util.has_template("Unobtainable items", code):
continue
equips = {}
for (vid, version) in util.each_version("Infobox Bonuses", code, include_base=True):
doc = {}
equips[vid] = doc
if "slot" in version:
slotID = str(version["slot"]).strip().lower()
if slotID in slotIDs:
doc["slot"] = slotIDs[slotID]
if slotID == "2h":
doc["is2h"] = True
elif slotID != "?":
print("Item {} has unknown slot {}".format(name, slotID))
for key in [
"astab", "aslash", "acrush", "amagic", "arange", "dstab", "dslash", "dcrush", "dmagic", "drange", "str",
"rstr", "prayer", ("speed", "aspeed")
]:
try:
util.copy(key, doc, version, lambda x: int(x))
except ValueError:
print("Item {} has an non integer {}".format(name, key))
try:
util.copy("mdmg", doc, version, lambda x: float(x))
except ValueError:
print("Item {} has non-float mdmg value {}".format(name, str(version["mdmg"]).strip()))
for (vid, version) in util.each_version("Infobox Item", code, mergable_keys=None if len(equips) <= 1 else []):
if "removal" in version and not str(version["removal"]).strip().lower() in ["", "no", "n/a"]:
continue
doc = util.get_doc_for_id_string(name + str(vid), version, stats)
if doc == None:
continue
util.copy("name", doc, version)
if not "name" in doc:
doc["name"] = name
equipable = "equipable" in version and "yes" in str(version["equipable"]).strip().lower()
if "weight" in version:
strval = str(version["weight"]).strip()
if strval.endswith("kg"):
strval = strval[:-2].strip()
if strval != "":
floatval = float(strval)
if floatval != 0:
doc["weight"] = floatval
equipVid = vid if vid in equips else -1 if -1 in equips else None
if equipVid != None:
if equipable or not "broken" in version["name"].lower():
if not equipable:
print("Item {} has Infobox Bonuses but not equipable".format(name))
doc["equipable"] = True
doc["equipment"] = equips[equipVid]
elif equipable:
print("Item {} has equipable but not Infobox Bonuses".format(name))
doc["equipable"] = True
doc["equipment"] = {}
itemName = name
if "gemwname" in version:
itemName = str(version["gemwname"]).strip()
elif "name" in version:
itemName = str(version["name"]).strip()
if itemName in limits:
doc['ge_limit'] = limits[itemName]
except (KeyboardInterrupt, SystemExit):
raise
except:
print("Item {} failed:".format(name))
traceback.print_exc()
util.write_json("stats.json", "stats.ids.min.json", stats)