def db_insert_data_card(self, cards_json): """Insert download from mtgjson""" c_rows = [] s_rows = [] for data in cards_json.values(): cards = [] for raw in data["cards"]: c = Card(raw) c.image_url = util.CARD_IMAGE_URL.format(c.multiverse_id) c.set = data["code"] c.set_name = data["name"] cards.append(c) for c in cards: c_rows.append(self.card_to_table_mapping(c)) set = Set(data) s_rows.append(self.set_to_table_mapping(set)) # Use separate connection to commit changes immediately con = sqlite3.connect(self.db_file) try: with con: sql_string = "INSERT INTO `cards` VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," \ "?,?,?,?,?,?,?,?,?,?,?)" con.executemany(sql_string, c_rows) sql_string = "INSERT INTO `sets` VALUES (?,?,?,?,?,?,?,?,?,?,?)" con.executemany(sql_string, s_rows) except sqlite3.OperationalError as err: util.log("Database Error", util.LogLevel.Error) util.log(str(err), util.LogLevel.Error) except sqlite3.IntegrityError as err: util.log("Database Error", util.LogLevel.Error) util.log(str(err), util.LogLevel.Error)
def table_to_card_mapping(row: sqlite3.Row): """Return card object representation of a table row""" card = Card() card.multiverse_id = row["multiverseid"] tmp = row["name"] card.name = tmp card.layout = row["layout"] card.mana_cost = row["manacost"] card.cmc = row["cmc"] card.colors = row["colors"] card.type = row["type"] card.rarity = row["rarity"] card.text = row["text"] card.flavor = row["flavor"] card.artist = row["artist"] card.number = row["number"] card.power = row["power"] card.toughness = row["toughness"] card.loyalty = row["loyalty"] card.watermark = row["watermark"] card.border = row["border"] card.hand = row["hand"] card.life = row["life"] card.release_date = row["releaseDate"] card.starter = row["starter"] card.original_text = row["originalText"] card.original_type = row["originalType"] card.source = row["source"] card.image_url = row["imageUrl"] card.set = row["set"] card.set_name = row["setName"] card.id = row["id"] # Bool attributes card.timeshifted = ast.literal_eval(row["timeshifted"]) # List attributes card.names = ast.literal_eval(row["names"]) card.supertypes = ast.literal_eval(row["supertypes"]) card.subtypes = ast.literal_eval(row["subtypes"]) card.types = ast.literal_eval(row["types"]) card.printings = ast.literal_eval(row["printings"]) card.variations = ast.literal_eval(row["variations"]) # Dict attributes card.legalities = ast.literal_eval(row["legalities"]) card.rulings = ast.literal_eval(row["rulings"]) card.foreign_names = ast.literal_eval(row["foreignNames"]) return card