forked from runelite/runelite-wiki-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.py
130 lines (109 loc) · 3.46 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
import sys
import traceback
import re
import mwparserfromhell as mw
import api
import util
from typing import *
import urllib.request
"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
}
WEIGHT_REDUCTION_EXTRACTOR = re.compile(
r"(?i)'''(?:In )?inventory:?''':? ([0-9.-]+) kg<br ?\/?> *'''Equipped:?''':? ([0-9.-]+)")
def getLimits():
req = urllib.request.Request(
'https://oldschool.runescape.wiki/w/Module:GELimits/data?action=raw', headers=api.user_agent)
with urllib.request.urlopen(req) as response:
data = response.read()
limits = {}
for line in data.splitlines():
match = re.search(r"\[\"(.*)\"\] = (\d+),?", str(line))
if match:
name = match.group(1).replace('\\', '')
limit = match.group(2)
limits[name] = int(limit)
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)
equips = {}
for (vid, version) in util.each_version("Infobox Bonuses", code):
doc = {}
equips[vid] = doc
slotID = str(version["slot"]).strip().lower()
doc["slot"] = slotIDs[slotID]
if slotID == "2h":
doc["is2h"] = True
for key in [
"astab", "aslash", "acrush", "amagic", "arange", "dstab", "dslash", "dcrush", "dmagic", "drange", "str",
"rstr", "mdmg", "prayer", ("speed", "aspeed")
]:
try:
util.copy(key, doc, version, lambda x: int(x))
except ValueError:
print("Item {} has an non integer {}".format(name, key))
for (vid, version) in util.each_version("Infobox Item", code):
if "removal" in version and str(version["removal"]).strip():
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
util.copy("quest", doc, version, lambda x: x.lower() != "no")
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 != "":
red = WEIGHT_REDUCTION_EXTRACTOR.match(strval)
if red:
strval = red.group(2)
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)