From 8a0e745641bd75a1cc3c1f88a06a23482afe0762 Mon Sep 17 00:00:00 2001 From: Zach H Date: Fri, 15 Jul 2016 23:56:19 -0400 Subject: [PATCH] wip 2 --- json_to_sql.py | 23 ++++++++++++--------- sql_to_json.py | 54 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/json_to_sql.py b/json_to_sql.py index 4ebde54..d91018b 100755 --- a/json_to_sql.py +++ b/json_to_sql.py @@ -9,6 +9,11 @@ def getVal(data, field): if val: return str(val) return val + +def fixJson(data): + if data: + return data.replace("'", '"') + return data def create_db(database_connection): c = database_connection.cursor() @@ -42,12 +47,12 @@ def json_to_db(json_file_opened, database_connection): names = getVal(thisCard, "names") manaCost = getVal(thisCard, "manaCost") cmc = getVal(thisCard, "cmc") - colors = getVal(thisCard, "colors") - colorIdentity = getVal(thisCard, "colorIdentity") + colors = fixJson( getVal(thisCard, "colors") ) + colorIdentity = fixJson( getVal(thisCard, "colorIdentity") ) thisCard_type = getVal(thisCard, "type") - supertypes = getVal(thisCard, "supertypes") - types = getVal(thisCard, "types") - subtypes = getVal(thisCard, "subtypes") + supertypes = fixJson( getVal(thisCard, "supertypes") ) + types = fixJson( getVal(thisCard, "types") ) + subtypes = fixJson( getVal(thisCard, "subtypes") ) rarity = getVal(thisCard, "rarity") text = getVal(thisCard, "text") flavor = getVal(thisCard, "flavor") @@ -67,12 +72,12 @@ def json_to_db(json_file_opened, database_connection): reserved = getVal(thisCard, "reserved") releaseDate = getVal(thisCard, "releaseDate") starter = getVal(thisCard, "starter") - rulings = getVal(thisCard, "rulings") - foreignNames = getVal(thisCard, "foreignNames") - printings = getVal(thisCard, "printings") + rulings = fixJson( getVal(thisCard, "rulings") ) + foreignNames = fixJson( getVal(thisCard, "foreignNames") ) + printings = fixJson( getVal(thisCard, "printings") ) originalText = getVal(thisCard, "originalText") originalType = getVal(thisCard, "originalType") - legalities = getVal(thisCard, "legalities") + legalities = fixJson( getVal(thisCard, "legalities") ) source = getVal(thisCard, "source") thisCard_data = [thisCard_id, layout, name, names, manaCost, cmc, colors, colorIdentity, thisCard_type, supertypes, types, subtypes, rarity, text, flavor, artist, number, power, toughness, loyalty, multiverseid, variations, imageName, watermark, border, timeshifted, hand, life, reserved, releaseDate, starter, rulings, foreignNames, printings, originalText, originalType, legalities, source, setName, thisSet, setReleaseDate] diff --git a/sql_to_json.py b/sql_to_json.py index 2f49013..de77ab9 100755 --- a/sql_to_json.py +++ b/sql_to_json.py @@ -2,6 +2,12 @@ import json import sqlite3 import os +import fileinput + +def get_next(some_iterable, window=1): + items, nexts = tee(some_iterable, 2) + nexts = islice(nexts, window, None) + return izip_longest(items, nexts) def dict_from_row(row): return dict(zip(row.keys(), row)) @@ -16,19 +22,18 @@ def db_to_json(database_connection): database_connection.row_factory = sqlite3.Row # Enable keys for the rows cursor = database_connection.cursor() - cursor.execute("SELECT DISTINCT setCode from cards LIMIT 3,5") + cursor.execute("SELECT DISTINCT setCode from cards LIMIT 10") mainDict = [] returnData = [] rows = cursor.fetchall() for setCode in rows: setCode = remove_empty_keys(dict_from_row(setCode)) - cursor.execute("SELECT * FROM cards WHERE setCode = '%s' LIMIT 5" % setCode["setCode"]) + cursor.execute("SELECT * FROM cards WHERE setCode = '%s'" % setCode["setCode"]) card_rows = cursor.fetchall() for row in card_rows: row = remove_empty_keys(dict_from_row(row)) - returnData.append(row) mainDict.append([setCode, returnData]) @@ -42,13 +47,52 @@ def main(): d = sqlite3.connect(d) #xml = os.path.join(os.path.expanduser(input("Location of save file: ")), "Output.json") - xml = os.path.join(os.path.expanduser("~/Desktop"), "Output.json") + xml = os.path.join(os.path.expanduser("~/Desktop"), "Output.tmp.json") + xml2 = os.path.join(os.path.expanduser("~/Desktop"), "Output.json") - json_code = json.dumps(db_to_json(d), indent=4, sort_keys=True) + json_code = json.dumps(db_to_json(d), sort_keys=True, indent=2) writeFile = open(xml, 'w') writeFile.write(json_code) writeFile.close() + + with open(xml) as f: + with open(xml2, 'w') as f2: + for line in f.readlines(): + if replace_and_write_these_keys(f2, line, "colorIdentity"): continue + if replace_and_write_these_keys(f2, line, "colors"): continue + if replace_and_write_these_keys(f2, line, "printings"): continue + if replace_and_write_these_keys(f2, line, "subtypes"): continue + if replace_and_write_these_keys(f2, line, "legalities"): continue + if replace_and_write_these_keys(f2, line, "types"): continue + #if replace_and_write_these_keys(f2, line, "rulings"): continue + #if replace_and_write_these_keys(f2, line, "foreignNames"): continue + + if '"' + "variations" + '":' in line: + f2.write(",") + + f2.write(line) + f2.close() + os.remove(xml) + +def replace_and_write_these_keys(file_opened, line, key_val): + retVal = str_to_json(line, key_val) + if retVal: + file_opened.write(retVal) + return True + +def str_to_json(line, key_val): + if '"' + key_val + '":' in line: + line_index = line.index('"[') + line = line[:line_index] + line[line_index:].replace('\\"', '"')[1:] + + while line.strip()[-1:] != "]": + line = line[:-1] + + if key_val != "types": + line += "," + line += "\n" + return line if __name__ == '__main__': main() \ No newline at end of file