Skip to content

Instantly share code, notes, and snippets.

@TotallyGatsby
Created May 29, 2012 13:52
Show Gist options
  • Save TotallyGatsby/2828489 to your computer and use it in GitHub Desktop.
Save TotallyGatsby/2828489 to your computer and use it in GitHub Desktop.
Quick and Dirty Parser for D&D Weapons
import sqlite3
from bs4 import BeautifulSoup
from collections import defaultdict
conn = sqlite3.connect("C:\Users\Phill\Dropbox\code\python\compendium\items.db")
c = conn.cursor()
count = 0
for row in c.execute("SELECT * FROM weapondata LIMIT 5"):
soup = BeautifulSoup(row[2])
wpn = defaultdict(lambda: None)
wpn["id"] = row[0]
wpn["name"] = row[1]
isDamage = False
for detail in soup.div.contents:
sdetail = detail.encode("utf-8")
if sdetail.startswith("<h1"):
continue
if sdetail == "<br/>":
continue
if isDamage:
isDamage = False
wpn["damage"] = detail[2:]
continue
weaponSkill = ("Simple", "Military","Superior", "Improvised")
isSkills = reduce(lambda res, sk: res or sk, map(lambda x: str(detail.encode("utf-8")).startswith(x) and str(detail.encode("utf-8")).endswith("weapon"), weaponSkill))
if isSkills == True:
words = detail.split(" ")
wpn["skill"] = words[0]
wpn["numHands"] = words[1]
wpn["range"] = words[2]
continue;
if sdetail.endswith("gp"):
wpn["cost"] = float(sdetail.replace("Cost:", "").replace("\xe2\x80\x94", "0").replace("gp",""))
continue
if sdetail.startswith("<b>Damage</b>"):
isDamage = True
continue
if sdetail.startswith("Proficient:"):
wpn["proficient"] = int(sdetail.replace("Proficient:", ""))
continue
if sdetail.startswith("Weight:"):
wpn["weight"] = float(sdetail.replace("Weight:", "")
.replace("lb.","")
.replace("1\xe2\x80\x93","")
.replace("\xe2\x80\x94","0")
.replace("6\xe2\x80\x93",""))
continue
if sdetail.startswith("Range:"):
if sdetail.count("-") > 0:
continue
wpn["srange"] = int(sdetail.replace("Range:", "").split("/")[0])
wpn["lrange"] = int(sdetail.replace("Range:", "").split("/")[1])
continue
if sdetail.endswith("("):
print "PROPERTYGROUPFOUND!"
print detail
print wpn
conn.commit()
c.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment