diff --git a/json_to_sql.py b/json_to_sql.py index 4eb3580..73c37b2 100755 --- a/json_to_sql.py +++ b/json_to_sql.py @@ -86,6 +86,10 @@ def json_to_db(json_file_opened, database_connection): c.close() def main(): + if len(sys.argv) != 4: + print("Must provide 3 arguements: should_create, database_location, json_location") + os._exit(1) + i = sys.argv[1] # Should create new DB db_path = os.path.expanduser(sys.argv[2]) # File location for database diff --git a/sql_to_json.py b/sql_to_json.py index b5975f0..ad0d697 100755 --- a/sql_to_json.py +++ b/sql_to_json.py @@ -5,19 +5,23 @@ import os import fileinput import sys - def id(x): return x DECODERS = { 'setName': id, 'setCode': id, 'setReleaseDate': id } - + def dict_from_row(row): return {k: DECODERS.get(k, json.loads)(v) for k, v in zip(row.keys(), row) if v is not None} +def remove_set_info(dictionary): + dictionary.pop("setName", None) + dictionary.pop("setCode", None) + dictionary.pop("setReleaseDate", None) + return dictionary + def set_dictionary(row): return dict(zip(row.keys(), row)) - - + def db_to_json(database_connection): database_connection.row_factory = sqlite3.Row # Enable keys for the rows cursor = database_connection.cursor() @@ -35,11 +39,14 @@ def db_to_json(database_connection): setReleaseDate = None for row in card_rows: row = dict_from_row(row) - returnData.append(row) + if not setName or not setReleaseDate: setName = row["setName"] setReleaseDate = row["setReleaseDate"] + row = remove_set_info(row) + returnData.append(row) + mainDict[setCode["setCode"]] = dict(zip(["cards", "name", "releaseDate"], [returnData, setName, setReleaseDate])) setName = None setReleaseDate = None @@ -49,6 +56,10 @@ def db_to_json(database_connection): return mainDict def main(): + if len(sys.argv) != 3: + print("Must provide 2 arguements: database_location, json_output_location") + os._exit(1) + db_path = sqlite3.connect(os.path.expanduser(sys.argv[1])) # File location for database file_path = os.path.expanduser(sys.argv[2]) # File location for output