new scripts

This commit is contained in:
Zach H 2016-07-14 15:13:39 -04:00
parent cebf62c573
commit 766d948154
5 changed files with 143 additions and 154 deletions

Binary file not shown.

View file

@ -1,87 +0,0 @@
#!/usr/bin/env python3
import json
import sqlite3
import time
import os
JSON_FILE = os.path.expanduser(input("Where is the \"AllSets-x.json\" file you'd like to import?\n> "))
DB_FILE = os.path.join(os.path.dirname(JSON_FILE), "Magic Database Complete.db")
# Get the value of the card, return None if NULL
def getVal(data, field):
val = data.get(field)
if val:
return str(val)
return val
traffic = json.load(open(JSON_FILE))
conn = sqlite3.connect(DB_FILE)
c = conn.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)')
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():
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)
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")
names = getVal(thisCard, "names")
manaCost = getVal(thisCard, "manaCost")
cmc = getVal(thisCard, "cmc")
colors = getVal(thisCard, "colors")
colorIdentity = getVal(thisCard, "colorIdentity")
thisCard_type = getVal(thisCard, "type")
supertypes = getVal(thisCard, "supertypes")
types = getVal(thisCard, "types")
subtypes = getVal(thisCard, "subtypes")
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")
rulings = getVal(thisCard, "rulings")
foreignNames = getVal(thisCard, "foreignNames")
printings = getVal(thisCard, "printings")
originalText = getVal(thisCard, "originalText")
originalType = getVal(thisCard, "originalType")
legalities = 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]
c.execute('insert into cards values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', thisCard_data)
print("Database file can be found at " + os.path.abspath(DB_FILE))
conn.commit()
c.close()

View file

@ -1,67 +0,0 @@
#!/usr/bin/env python3
import json
import sqlite3
import datetime
import os
JSON_FILE = input("Where is the single set JSON file you'd like to import?\n> ") #Input filename via command line
DB_FILE = "Magic Database Single Set " + str(datetime.datetime.now()) + ".db"
traffic = json.load(open(JSON_FILE))
conn = sqlite3.connect(DB_FILE)
c = conn.cursor()
c.execute('create table table_name (name, manaCost, cmc, colors, type_, supertypes, types, subtypes, rarity, text, flavor, artist, number, power, toughness, layout, multiverseid, imageName, id_, names, colorIdentity, loyalty, variations, watermark, border, timeshifted, hand, life, reserved, releaseDate, starter, rulings, foreignNames, printings, originalType, legalities, source)')
def getVal(data, field):
try: retVal = str(data[field])
except KeyError: retVal = None
return retVal
cards = traffic["cards"]
for i in range(0, len(cards)):
name = getVal(cards[i], "name")
manaCost = getVal(cards[i], "manaCost")
cmc = getVal(cards[i], "cmc")
colors = getVal(cards[i], "colors")
type_ = getVal(cards[i], "type")
supertypes = getVal(cards[i], "supertypes")
types = getVal(cards[i], "types")
subtypes = getVal(cards[i], "subtypes")
rarity = getVal(cards[i], "rarity")
text = getVal(cards[i], "text")
flavor = getVal(cards[i], "flavor")
artist = getVal(cards[i], "artist")
number = getVal(cards[i], "number")
power = getVal(cards[i], "power")
toughness = getVal(cards[i], "toughness")
layout = getVal(cards[i], "layout")
multiverseid = getVal(cards[i], "multiverseid")
imageName = getVal(cards[i], "imageName")
id_ = getVal(cards[i], "id")
names = getVal(cards[i], "names")
colorIdentity = getVal(cards[i], "colorIdentity")
loyalty = getVal(cards[i], "loyalty")
variations = getVal(cards[i], "variations")
watermark = getVal(cards[i], "watermark")
border = getVal(cards[i], "border")
timeshifted = getVal(cards[i], "timeshifted")
hand = getVal(cards[i], "hand")
life = getVal(cards[i], "life")
reserved = getVal(cards[i], "reserved")
releaseDate = getVal(cards[i], "releaseDate")
starter = getVal(cards[i], "starter")
rulings = getVal(cards[i], "rulings")
foreignNames = getVal(cards[i], "foreignNames")
printings = getVal(cards[i], "printings")
originalType = getVal(cards[i], "originalType")
legalities = getVal(cards[i], "legalities")
source = getVal(cards[i], "source")
data = [name, manaCost, cmc, colors, type_, supertypes, types, subtypes, rarity, text, flavor, artist, number, power, toughness, layout, multiverseid, imageName, id_, names, colorIdentity, loyalty, variations, watermark, border, timeshifted, hand, life, reserved, releaseDate, starter, rulings, foreignNames, printings, originalType, legalities, source]
c.execute('insert into table_name values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', data)
print("Database file can be found at " + os.path.abspath(DB_FILE))
conn.commit()
c.close()

98
json_to_sql.py Executable file
View file

@ -0,0 +1,98 @@
#!/usr/bin/env python3
import json
import sqlite3
import time
import os
def getVal(data, field):
val = data.get(field)
if val:
return str(val)
return val
def create_db(database_file):
conn = sqlite3.connect(database_file)
c = conn.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()
c.close()
def json_to_db(json_file, database_file):
traffic = json.load(open(json_file))
conn = sqlite3.connect(database_file)
c = conn.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():
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)
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")
names = getVal(thisCard, "names")
manaCost = getVal(thisCard, "manaCost")
cmc = getVal(thisCard, "cmc")
colors = getVal(thisCard, "colors")
colorIdentity = getVal(thisCard, "colorIdentity")
thisCard_type = getVal(thisCard, "type")
supertypes = getVal(thisCard, "supertypes")
types = getVal(thisCard, "types")
subtypes = getVal(thisCard, "subtypes")
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")
rulings = getVal(thisCard, "rulings")
foreignNames = getVal(thisCard, "foreignNames")
printings = getVal(thisCard, "printings")
originalText = getVal(thisCard, "originalText")
originalType = getVal(thisCard, "originalType")
legalities = 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]
c.execute('insert into cards values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', thisCard_data)
conn.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");
if (i == '1'):
create_db(d)
xml = os.path.join(os.path.expanduser(input("Location of AllSets-x.json XML: ")), "AllSets-x.json")
json_to_db(xml, d)
if __name__ == '__main__':
main()

45
sql_to_json.py Executable file
View file

@ -0,0 +1,45 @@
#!/usr/bin/env python3
import json
import sqlite3
import os
def dict_from_row(row):
return dict(zip(row.keys(), row))
def remove_empty_keys(d):
for k in list(d):
if not d[k]:
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()
cursor.execute(""" SELECT * from cards """)
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)
conn.close()
printFile.close()
def main():
d = os.path.join(os.path.expanduser(input("Location of database: ")), "Magic DB.db")
xml = os.path.join(os.path.expanduser(input("Location of save file: ")), "Output.json")
db_to_json(d, xml)
if __name__ == '__main__':
main()