2019-08-08 16:37:08 -04:00
|
|
|
import json
|
2019-08-08 18:28:33 -04:00
|
|
|
import sqlite3
|
2019-08-08 16:37:08 -04:00
|
|
|
|
|
|
|
def main():
|
|
|
|
# load json into python
|
|
|
|
print("loading json")
|
|
|
|
## read file into memory
|
|
|
|
with open('spells.json') as f:
|
|
|
|
# raw_data = f.read()
|
|
|
|
data = json.load(f)
|
|
|
|
print("Imported {} spells.".format(len(data)))
|
|
|
|
# alphabetize spells into a list
|
|
|
|
sorted_names = []
|
|
|
|
for i in data:
|
|
|
|
sorted_names.append(i['name'])
|
|
|
|
sorted_names.sort()
|
|
|
|
# print(sorted_names)
|
|
|
|
|
|
|
|
# get list of dicts in order
|
|
|
|
sorted_dicts = [] # sorted by ['name'] alphabetically
|
|
|
|
for i in sorted_names:
|
|
|
|
for x in data:
|
|
|
|
# yes, I know this is double nested and not that efficient; don't
|
|
|
|
# care yet, it's not that many items
|
|
|
|
if x['name'] == i:
|
|
|
|
sorted_dicts.append(x)
|
|
|
|
|
|
|
|
# NOW we can go alphabetically spell by spell
|
2019-08-08 18:28:33 -04:00
|
|
|
|
|
|
|
## Get database connection
|
|
|
|
conn = sqlite3.connect('../../pf2.db')
|
2019-08-08 19:05:21 -04:00
|
|
|
|
|
|
|
id = 0
|
2019-08-08 16:37:08 -04:00
|
|
|
for i in sorted_dicts:
|
2019-08-08 19:05:21 -04:00
|
|
|
id += 1
|
2019-08-08 21:39:36 -04:00
|
|
|
# insert basics of a spell
|
|
|
|
do_basic_sql(i, id, conn)
|
|
|
|
do_range_numbers(i,id,conn)
|
2019-08-08 22:01:12 -04:00
|
|
|
do_sources_pages(i,id,conn)
|
2019-08-08 21:39:36 -04:00
|
|
|
# TODO do all the traits, FK stuff etc...
|
2019-08-08 16:37:08 -04:00
|
|
|
|
2019-08-08 22:01:12 -04:00
|
|
|
def do_sources_pages(i, id, conn):
|
|
|
|
if 'source' not in i:
|
|
|
|
return
|
|
|
|
print(i)
|
|
|
|
|
|
|
|
res = ''
|
|
|
|
source_id = 0
|
|
|
|
# Do Core Rulebook branch
|
|
|
|
if "Core Rulebook" in i['source']:
|
|
|
|
res = i['source'].replace('Core Rulebook pg.','').strip()
|
|
|
|
source_id = 1
|
|
|
|
|
|
|
|
stmt = "UPDATE spells SET sources_id=?, sources_pages=? WHERE spells_id=?"
|
|
|
|
inp = (source_id, res, id)
|
|
|
|
|
|
|
|
try:
|
|
|
|
conn.execute(stmt, inp)
|
|
|
|
except:
|
|
|
|
print("Error updating sources")
|
|
|
|
else:
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-08-08 21:39:36 -04:00
|
|
|
def do_range_numbers(i, id, conn):
|
|
|
|
# no need to do range
|
|
|
|
if 'range' not in i:
|
|
|
|
return
|
|
|
|
rg = -1
|
|
|
|
# convert range_text to an integer representation
|
|
|
|
if i['range'] == 'touch':
|
|
|
|
rg = 0
|
|
|
|
elif i['range'] == 'planetary':
|
|
|
|
rg = 999999999
|
|
|
|
# is the only one in CRB with emanation 40' from current scraping
|
|
|
|
elif i['name'] == 'Repulsion':
|
|
|
|
rg = 40
|
|
|
|
else:
|
|
|
|
# DO SPLITS
|
|
|
|
splits = i['range'].split(' ')
|
|
|
|
# print(splits)
|
|
|
|
rg = splits[0]
|
|
|
|
inp = (rg, id)
|
|
|
|
stmt = "UPDATE spells SET range_ft=? WHERE spells_id=?"
|
|
|
|
try:
|
|
|
|
conn.execute(stmt, inp)
|
|
|
|
except:
|
|
|
|
print("Error updating range_ft")
|
|
|
|
else:
|
|
|
|
conn.commit()
|
|
|
|
# print("Successfully updated range_ft")
|
|
|
|
|
|
|
|
|
|
|
|
def do_basic_sql(i, id, conn):
|
2019-08-08 19:05:21 -04:00
|
|
|
print("Doing spell id #{}: {}".format(id, i['name']))
|
|
|
|
stmt = """INSERT INTO spells (
|
|
|
|
spells_id,
|
|
|
|
sources_id,
|
|
|
|
sources_pages,
|
|
|
|
nethysurl,
|
|
|
|
name,
|
|
|
|
level,
|
|
|
|
descr,
|
|
|
|
range_text)
|
2019-08-08 21:39:36 -04:00
|
|
|
VALUES (?,?,?,?,?,?,?,?)"""
|
2019-08-08 19:05:21 -04:00
|
|
|
|
|
|
|
rge = None
|
|
|
|
if 'range' in i:
|
|
|
|
rge = i['range']
|
|
|
|
|
|
|
|
dscr = None
|
|
|
|
if 'description' in i:
|
|
|
|
dscr = i['description']
|
|
|
|
|
2019-08-08 21:39:36 -04:00
|
|
|
inp = (id, 1, i['source'], i['nethysUrl'], i['name'], i['level'], dscr, rge)
|
2019-08-08 19:05:21 -04:00
|
|
|
try:
|
|
|
|
conn.execute(stmt, inp)
|
|
|
|
except:
|
2019-08-08 21:39:36 -04:00
|
|
|
print("Error inserting row")
|
|
|
|
else:
|
|
|
|
conn.commit()
|
|
|
|
# print("Successfully inserted row")
|
2019-08-08 16:37:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|