diff --git a/data/third_party_json/spells.py b/data/third_party_json/spells.py index c9b9224..a88fd09 100644 --- a/data/third_party_json/spells.py +++ b/data/third_party_json/spells.py @@ -30,6 +30,15 @@ def main(): ## Get database connection conn = sqlite3.connect('../../pf2.db') + # load in ids for traits from traits table so we only call this once + # instead of every spell + stmt = "SELECT trait_id, short_name FROM traits" + c = conn.cursor() + c.execute(stmt) + traits = c.fetchall() + # print(traits) + + id = 0 for i in sorted_dicts: id += 1 @@ -37,8 +46,39 @@ def main(): do_basic_sql(i, id, conn) do_range_numbers(i,id,conn) do_sources_pages(i,id,conn) + do_spell_traits(i,id,conn,traits) # TODO do all the traits, FK stuff etc... +def do_spell_traits(i, id, conn, traits): + + # get list of traits from the json and capitalize first letter + traits_json = [] + for item in i['traits']: + traits_json.append(item.capitalize()) + + trait_ids =[] + for j in traits_json: + for k in traits: + if j == k[1]: + trait_ids.append(k[0]) + # print(trait_ids) + + inp = [] + for j in trait_ids: + inp.append((id,j)) + # print(inp) + + # insert into sql + stmt = "INSERT OR REPLACE INTO spells_traits (spells_id, traits_id) VALUES (?,?)" + try: + conn.executemany(stmt, inp) + except: + print("Error updating traits") + else: + conn.commit() + + + def do_sources_pages(i, id, conn): if 'source' not in i: return diff --git a/schema/spells.sql b/schema/spells.sql index 049ab51..da18251 100644 --- a/schema/spells.sql +++ b/schema/spells.sql @@ -28,8 +28,8 @@ CREATE TABLE spellschools ( -- UNIQUE as sanity requires :) CREATE TABLE spells ( spells_id INTEGER PRIMARY KEY, - sources_id INTEGER NOT NULL, -- manually entered right now - sources_pages TEXT, -- TODO convert to our format in spells.py + sources_id INTEGER NOT NULL, -- generated in spells.py from scraped data + sources_pages TEXT, -- generated in spells.py from scraped data name TEXT NOT NULL UNIQUE, -- scraped from github repo level INTEGER, -- scraped from github repo trigger TEXT, -- TODO in spells.py @@ -55,6 +55,7 @@ CREATE TABLE spells_traits ( id INTEGER PRIMARY KEY, spells_id INTEGER NOT NULL, traits_id INTEGER NOT NULL, + UNIQUE(spells_id, traits_id), FOREIGN KEY (spells_id) REFERENCES spells(spells_id), FOREIGN KEY (traits_id) REFERENCES traits(traits_id) );