+class SkillGemListParser(WikiParser):
+ def __init__(self):
+ super(SkillGemListParser, self).__init__()
+
+ self.in_subheading = False
+ self.in_list = False
+ self.links = []
+
+ def handle_starttag(self, tag, attrs):
+ super(SkillGemListParser, self).handle_starttag(tag, attrs)
+
+ if tag=="h2":
+ self.in_subheading = True
+ elif tag=="a":
+ if self.in_list and not self.ignore:
+ for n, v in attrs:
+ if n=="href":
+ self.links.append(v)
+
+ def handle_endtag(self, tag):
+ super(SkillGemListParser, self).handle_endtag(tag)
+
+ if tag=="h2":
+ self.in_subheading = False
+
+ def handle_data(self, data):
+ data = data.strip()
+
+ if self.in_subheading:
+ self.in_list = (data=="List")
+
+class SkillGemParser(WikiParser):
+ prices = (("alchemy", "Orb of Alchemy"),
+ ("chance", "Orb of Chance"),
+ ("alteration", "Orb of Alteration"),
+ ("transmute", "Orb of Transmutation"),
+ ("wisdom", "Scroll of Wisdom"))
+
+ def __init__(self):
+ super(SkillGemParser, self).__init__()
+
+ self.item = None
+ self.in_heading = False
+ self.in_subheading = False
+ self.in_subheading = False
+ self.in_infobox = 0
+ self.in_box_heading = False
+ self.in_purchase = False
+ self.in_progression = False
+ self.in_acquisition = False
+ self.have_acquisition_data = False
+ self.row = 0
+ self.column = 0
+ self.attribute_reqs = []
+
+ def handle_starttag(self, tag, attrs):
+ super(SkillGemParser, self).handle_starttag(tag, attrs)
+
+ if tag=="h1":
+ self.in_heading = True
+ elif tag=="h2":
+ self.in_subheading = True
+ elif tag=="span":
+ if self.in_infobox:
+ self.in_infobox += 1
+ else:
+ for n, v in attrs:
+ if n=="class" and "item-box" in v:
+ self.in_infobox = 1
+ elif tag=="em":
+ if self.in_infobox:
+ for n, v in attrs:
+ if n=="class" and "header" in v:
+ self.in_purchase = False
+ self.in_box_heading = True
+ elif tag=="table":
+ self.row = 0
+ elif tag=="tr":
+ self.row += 1
+ self.column = 0
+ elif tag=="td" or tag=="th":
+ self.column += 1
+ elif tag=="img":
+ if self.in_progression and self.column>=3:
+ for n, v in attrs:
+ if n=="alt":
+ self.attribute_reqs.append([v, 0])
+ elif tag=="div":
+ for n, v in attrs:
+ if n=="id" and v=="footer":
+ if not self.have_acquisition_data:
+ self.item.drop_only = True
+
+ def handle_endtag(self, tag):
+ super(SkillGemParser, self).handle_endtag(tag)
+
+ if tag=="h1":
+ self.in_heading = False
+ elif tag=="h2":
+ self.in_subheading = False
+ elif tag=="span":
+ if self.in_infobox:
+ self.in_infobox -= 1
+ elif tag=="em":
+ self.in_box_heading = False
+ elif tag=="tr":
+ if self.in_progression and self.row==2 and self.attribute_reqs:
+ self.attribute_reqs.sort(key=lambda r: r[1])
+ self.item.primary_attribute = self.attribute_reqs[0][0]
+ if len(self.attribute_reqs)>1:
+ self.item.secondary_attribute = self.attribute_reqs[1][0]
+
+ def handle_data(self, data):
+ data = data.strip()
+ if not data:
+ return
+
+ if self.in_heading:
+ name = data
+ paren = name.find('(')
+ if paren>=0:
+ name = name[:paren].strip()
+ self.item = SkillGem(name)
+ if name.startswith("Vaal"):
+ self.item.vaal = True
+ elif self.in_subheading:
+ self.in_progression = ("progression" in data)
+ self.in_acquisition = ("acquisition" in data)
+ elif self.in_box_heading:
+ if "Purchase" in data:
+ self.in_purchase = True
+ elif self.in_purchase:
+ if self.column==2:
+ for p, n in SkillGemParser.prices:
+ if n==data:
+ self.item.price = p
+ elif self.in_progression:
+ if self.row==2 and self.column>=3 and self.column<3+len(self.attribute_reqs):
+ self.attribute_reqs[self.column-3][1] = int(data)
+ elif self.in_acquisition:
+ self.have_acquisition_data = True
+