file changes

This commit is contained in:
Zach H 2016-07-14 16:12:49 -04:00
parent 766d948154
commit 70e0361a94
2 changed files with 29 additions and 30 deletions

View file

@ -10,29 +10,25 @@ def getVal(data, field):
return str(val)
return val
def create_db(database_file):
conn = sqlite3.connect(database_file)
c = conn.cursor()
def create_db(database_connection):
c = database_connection.cursor()
c.execute('create table cards (id, layout, name, names, manaCost, cmc, colors, colorIdentity, 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, setCode, setReleaseDate)')
c.execute('create table lastUpdated (datetime)')
conn.commit()
database_connection.commit()
c.close()
def json_to_db(json_file, database_file):
traffic = json.load(open(json_file))
conn = sqlite3.connect(database_file)
c = conn.cursor()
def json_to_db(json_file_opened, database_connection):
c = database_connection.cursor()
c.execute('insert into lastUpdated values (?)', [str(time.strftime("%Y-%m-%d %H:%M:%S"))])
# Get the setnames in the AllSets file and put them into a dictionary for later use
setNames = []
for i in traffic.keys():
for i in json_file_opened.keys():
setNames.append(i)
# Iterate through each set, one at a time
for thisSet in setNames:
data = traffic[thisSet] # All of the data for the set (I.e. SOI-x.json)
data = json_file_opened[thisSet] # All of the data for the set (I.e. SOI-x.json)
setName = data["name"]
setReleaseDate = data["releaseDate"]
@ -82,16 +78,20 @@ def json_to_db(json_file, database_file):
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]
c.execute('insert into cards values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', thisCard_data)
conn.commit()
database_connection.commit()
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");
d = sqlite3.connect(d)
if (i == '1'):
create_db(d)
xml = os.path.join(os.path.expanduser(input("Location of AllSets-x.json XML: ")), "AllSets-x.json")
xml = os.path.join(os.path.expanduser(input("Location of AllSets-x.json: ")), "AllSets-x.json")
xml = json.load(open(xml, 'r'))
json_to_db(xml, d)
if __name__ == '__main__':

View file

@ -12,34 +12,33 @@ def remove_empty_keys(d):
del d[k]
return d
def db_to_json(database, fileName="", writeToFile=True):
if (writeToFile):
printFile = open(fileName, 'w')
conn = sqlite3.connect(database)
conn.row_factory = sqlite3.Row # Enable keys for the rows
cursor = conn.cursor()
def db_to_json(database_connection):
database_connection.row_factory = sqlite3.Row # Enable keys for the rows
cursor = database_connection.cursor()
cursor.execute(""" SELECT * from cards """)
returnData = ""
rows = cursor.fetchall()
for row in rows:
row = remove_empty_keys(dict_from_row(row))
dump = json.dumps(row, sort_keys=True)
if (writeToFile):
printFile.write(dump)
else:
print(dump)
returnData += dump
conn.close()
printFile.close()
database_connection.close()
return returnData
def main():
d = os.path.join(os.path.expanduser(input("Location of database: ")), "Magic DB.db")
d = sqlite3.connect(d)
xml = os.path.join(os.path.expanduser(input("Location of save file: ")), "Output.json")
db_to_json(d, xml)
json_code = db_to_json(d)
writeFile = open(xml, 'w')
writeFile.write(json_code)
writeFile.close()
if __name__ == '__main__':
main()