mtgsqlive/json_to_sql.py

110 lines
5 KiB
Python
Raw Normal View History

2016-07-14 19:13:39 +00:00
#!/usr/bin/env python3
import json
import sqlite3
import time
import os
2016-07-16 06:35:04 +00:00
import sys
2016-07-14 19:13:39 +00:00
2016-07-18 05:11:33 +00:00
# Create the database
2016-07-14 20:12:49 +00:00
def create_db(database_connection):
c = database_connection.cursor()
2016-07-17 05:15:41 +00:00
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, mciNumber)')
2016-07-14 19:13:39 +00:00
c.execute('create table lastUpdated (datetime)')
2016-07-14 20:12:49 +00:00
database_connection.commit()
2016-07-14 19:13:39 +00:00
c.close()
2016-07-18 05:11:33 +00:00
def getVal(data, field):
value = data.get(field)
if value:
return json.dumps(value)
2016-07-14 20:12:49 +00:00
def json_to_db(json_file_opened, database_connection):
c = database_connection.cursor()
2016-07-18 05:11:33 +00:00
# Insert last updated time to database (so if you use the same people know when last updated)
2016-07-14 19:13:39 +00:00
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 = []
2016-07-14 20:12:49 +00:00
for i in json_file_opened.keys():
2016-07-14 19:13:39 +00:00
setNames.append(i)
# Iterate through each set, one at a time
for thisSet in setNames:
2016-07-14 20:12:49 +00:00
data = json_file_opened[thisSet] # All of the data for the set (I.e. SOI-x.json)
2016-07-14 19:13:39 +00:00
setName = data["name"]
setReleaseDate = data["releaseDate"]
setCards = data["cards"] # Now iterate through the setCards for each card in the set
for thisCard in setCards:
thisCard_id = getVal(thisCard, "id")
layout = getVal(thisCard, "layout")
name = getVal(thisCard, "name")
2016-07-18 05:11:33 +00:00
names = getVal(thisCard, "names")
2016-07-14 19:13:39 +00:00
manaCost = getVal(thisCard, "manaCost")
cmc = getVal(thisCard, "cmc")
2016-07-18 05:11:33 +00:00
colors = getVal(thisCard, "colors")
colorIdentity = getVal(thisCard, "colorIdentity")
2016-07-14 19:13:39 +00:00
thisCard_type = getVal(thisCard, "type")
2016-07-18 05:11:33 +00:00
supertypes = getVal(thisCard, "supertypes")
types = getVal(thisCard, "types")
subtypes = getVal(thisCard, "subtypes")
2016-07-14 19:13:39 +00:00
rarity = getVal(thisCard, "rarity")
text = getVal(thisCard, "text")
flavor = getVal(thisCard, "flavor")
artist = getVal(thisCard, "artist")
number = getVal(thisCard, "number")
power = getVal(thisCard, "power")
toughness = getVal(thisCard, "toughness")
loyalty = getVal(thisCard, "loyalty")
multiverseid = getVal(thisCard, "multiverseid")
variations = getVal(thisCard, "variations")
imageName = getVal(thisCard, "imageName")
watermark = getVal(thisCard, "watermark")
border = getVal(thisCard, "border")
timeshifted = getVal(thisCard, "timeshifted")
hand = getVal(thisCard, "hand")
life = getVal(thisCard, "life")
reserved = getVal(thisCard, "reserved")
releaseDate = getVal(thisCard, "releaseDate")
starter = getVal(thisCard, "starter")
2016-07-18 05:11:33 +00:00
rulings = getVal(thisCard, "rulings")
foreignNames = getVal(thisCard, "foreignNames")
printings = getVal(thisCard, "printings")
2016-07-14 19:13:39 +00:00
originalText = getVal(thisCard, "originalText")
originalType = getVal(thisCard, "originalType")
2016-07-18 05:11:33 +00:00
legalities = getVal(thisCard, "legalities")
2016-07-14 19:13:39 +00:00
source = getVal(thisCard, "source")
2016-07-17 05:15:41 +00:00
mciNumber = getVal(thisCard, "mciNumber")
2016-07-14 19:13:39 +00:00
2016-07-17 05:15:41 +00:00
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, mciNumber]
2016-07-14 19:13:39 +00:00
2016-07-18 05:11:33 +00:00
# Insert thisCard into the database
2016-07-17 05:15:41 +00:00
c.execute('insert into cards values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', thisCard_data)
2016-07-14 20:12:49 +00:00
database_connection.commit()
2016-07-14 19:13:39 +00:00
c.close()
def main():
2016-07-18 23:15:36 +00:00
if len(sys.argv) != 4:
print("Must provide 3 arguements: should_create, database_location, json_location")
os._exit(1)
2016-07-17 03:42:30 +00:00
i = sys.argv[1] # Should create new DB
2016-07-18 05:11:33 +00:00
db_path = os.path.expanduser(sys.argv[2]) # File location for database
2016-07-14 20:12:49 +00:00
2016-07-14 19:13:39 +00:00
if (i == '1'):
2016-07-18 05:11:33 +00:00
if os.path.isfile(db_path):
os.remove(db_path)
db_path = sqlite3.connect(db_path)
create_db(db_path)
2016-07-18 03:07:35 +00:00
else:
2016-07-18 05:11:33 +00:00
db_path = sqlite3.connect(db_path)
2016-07-14 19:13:39 +00:00
2016-07-18 05:11:33 +00:00
json_path = os.path.expanduser(sys.argv[3]) # File location for input file
json_path = json.load(open(json_path, 'r'))
2016-07-14 20:12:49 +00:00
2016-07-18 05:11:33 +00:00
json_to_db(json_path, db_path)
2016-07-14 19:13:39 +00:00
if __name__ == '__main__':
main()