From 38fed340c084d46df304e537f58ca9f32aea5129 Mon Sep 17 00:00:00 2001 From: Zach H Date: Sat, 16 Jul 2016 02:35:04 -0400 Subject: [PATCH] wip still --- json_to_sql.py | 11 ++++++----- sql_to_json.py | 36 ++++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/json_to_sql.py b/json_to_sql.py index d91018b..424693c 100755 --- a/json_to_sql.py +++ b/json_to_sql.py @@ -3,6 +3,7 @@ import json import sqlite3 import time import os +import sys def getVal(data, field): val = data.get(field) @@ -72,8 +73,8 @@ def json_to_db(json_file_opened, database_connection): reserved = getVal(thisCard, "reserved") releaseDate = getVal(thisCard, "releaseDate") starter = getVal(thisCard, "starter") - rulings = fixJson( getVal(thisCard, "rulings") ) - foreignNames = fixJson( getVal(thisCard, "foreignNames") ) + rulings = getVal(thisCard, "rulings") + foreignNames = getVal(thisCard, "foreignNames") printings = fixJson( getVal(thisCard, "printings") ) originalText = getVal(thisCard, "originalText") originalType = getVal(thisCard, "originalType") @@ -87,14 +88,14 @@ def json_to_db(json_file_opened, database_connection): c.close() def main(): - i = input("Create new database? 1 or 0: ") - d = os.path.join(os.path.expanduser(input("Location of database: ")), "Magic DB.db"); + i = sys.argv[1] #input("Create new database? 1 or 0: ") + d = os.path.join(os.path.expanduser(sys.argv[2]), "Magic DB.db"); #input("Location of database: ") d = sqlite3.connect(d) if (i == '1'): create_db(d) - xml = os.path.join(os.path.expanduser(input("Location of AllSets-x.json: ")), "AllSets-x.json") + xml = os.path.join(os.path.expanduser(sys.argv[3]), "AllSets-x.json") #input("Location of AllSets-x.json: ") xml = json.load(open(xml, 'r')) json_to_db(xml, d) diff --git a/sql_to_json.py b/sql_to_json.py index 98f10c8..29ff743 100755 --- a/sql_to_json.py +++ b/sql_to_json.py @@ -3,6 +3,7 @@ import json import sqlite3 import os import fileinput +import sys def dict_from_row(row): return dict(zip(row.keys(), row)) @@ -16,8 +17,7 @@ def remove_empty_keys(d): 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 10") + cursor.execute("SELECT DISTINCT setCode from cards LIMIT 15 OFFSET " + sys.argv[1]) mainDict = [] returnData = [] @@ -37,14 +37,12 @@ def db_to_json(database_connection): return mainDict def main(): - #d = os.path.join(os.path.expanduser(input("Location of database: ")), "Magic DB.db") - d = os.path.join(os.path.expanduser("~/Desktop"), "Magic DB.db") + d = os.path.join(os.path.expanduser(sys.argv[2]), "Magic DB.db") 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.tmp.json") - xml2 = os.path.join(os.path.expanduser("~/Desktop"), "Output.json") - + xml = os.path.join(os.path.expanduser(sys.argv[3]), "Output.tmp.json") + xml2 = os.path.join(os.path.expanduser(sys.argv[3]), "Output.json") + json_code = json.dumps(db_to_json(d), sort_keys=True, indent=2) writeFile = open(xml, 'w') @@ -55,17 +53,30 @@ def main(): with open(xml) as f: with open(xml2, 'w') as f2: for line in f.readlines(): + if '"rulings":' in line: continue + if '"foreignNames":' in line: continue + if '"printings":' in line: continue + if '"originalText":' in line: continue + if '"originalType":' in line: continue + if '"legalities":' in line: continue + if '"source":' in line: continue + + 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, "supertypes"): 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: + if '"variations":' in line: f2.write(",") + if '"watermark":' in line: + f2.write(",") + f2.write(line) f2.close() @@ -77,13 +88,18 @@ def replace_and_write_these_keys(file_opened, line, key_val): file_opened.write(retVal) return True +# Yes this is a mess, but it works for now 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:] + + line = line[:line_index] + line[line_index:].replace('\\"', '"').replace('”', '\\"').replace('“', '\\"').replace('’', "\\'")[1:] while line.strip()[-1:] != "]": line = line[:-1] + + try: line = line[:line_index] + json.dumps(json.loads(line[line_index:]), indent=2) + except: line = line if key_val != "types": line += ","