def reindex() -> None: writer = WhooshWriter() cs = get_all_cards() for alias, name in fetcher.card_aliases(): for c in cs: if c.name == name: c.names.append(alias) writer.rewrite_index(cs)
def insert_card(p: Any, update_index: bool = True) -> Optional[int]: # Preprocess card partly for sanity but partly just to match what we used to get from mtgjson to make migration easier. sql = 'INSERT INTO card (' sql += ', '.join(name for name, prop in card.card_properties().items() if prop['scryfall']) sql += ') VALUES (' sql += ', '.join('%s' for name, prop in card.card_properties().items() if prop['scryfall']) sql += ')' values = [ p.get(database2json(name)) for name, prop in card.card_properties().items() if prop['scryfall'] ] db().execute(sql, values) card_id = db().last_insert_rowid() # 'meld' is in the list of normal cards here but is handled differently at a higher level. See above. if p['layout'] in [ 'augment', 'emblem', 'host', 'leveler', 'meld', 'normal', 'planar', 'saga', 'scheme', 'token', 'vanguard' ]: insert_face(p, card_id) elif p['layout'] in ['double_faced_token', 'flip', 'split', 'transform']: insert_card_faces(p, card_id) else: raise InvalidDataException(f"Unknown layout {p['layout']}") for color in p.get('colors', []): color_id = db().value('SELECT id FROM color WHERE symbol = %s', [color.capitalize()]) # INSERT IGNORE INTO because some cards have multiple faces with the same color. See DFCs and What // When // Where // Who // Why. db().execute( 'INSERT IGNORE INTO card_color (card_id, color_id) VALUES (%s, %s)', [card_id, color_id]) for symbol in p.get('color_identity', []): color_id = db().value('SELECT id FROM color WHERE symbol = %s', [symbol]) # INSERT IGNORE INTO because some cards have multiple faces with the same color identity. See DFCs and What // When // Where // Who // Why. db().execute( 'INSERT IGNORE INTO card_color_identity (card_id, color_id) VALUES (%s, %s)', [card_id, color_id]) for f, status in p.get('legalities', {}).items(): if status == 'not_legal': continue # Strictly speaking we could drop all this capitalizing and use what Scryfall sends us as the canonical name as it's just a holdover from mtgjson. name = f.capitalize() format_id = get_format_id(name, True) # INSERT IGNORE INTO because some cards have multiple faces with the same legality. See DFCs and What // When // Where // Who // Why. db().execute( 'INSERT IGNORE INTO card_legality (card_id, format_id, legality) VALUES (%s, %s, %s)', [card_id, format_id, status.capitalize()]) if update_index: p.id = card_id writer = WhooshWriter() writer.update_card(p) return card_id
def reindex() -> None: writer = WhooshWriter() writer.rewrite_index(get_all_cards())
def reindex_specific_cards(cs: List[Card]) -> None: writer = WhooshWriter() for c in cs: writer.update_card(c)
def insert_card(c, update_index: bool = True) -> None: name, card_id = try_find_card_id(c) if card_id is None: sql = 'INSERT INTO card (' sql += ', '.join(name for name, prop in card.card_properties().items() if prop['mtgjson']) sql += ') VALUES (' sql += ', '.join('%s' for name, prop in card.card_properties().items() if prop['mtgjson']) sql += ')' values = [ c.get(database2json(name)) for name, prop in card.card_properties().items() if prop['mtgjson'] ] db().execute(sql, values) card_id = db().last_insert_rowid() CARD_IDS[name] = card_id # mtgjson thinks the text of Jhessian Lookout is NULL not '' but that is clearly wrong. if c.get('text', None) is None and c['layout'] in playable_layouts(): c['text'] = '' c['nameAscii'] = card.unaccent(c.get('name')) c['searchText'] = re.sub(r'\([^\)]+\)', '', c['text']) c['cardId'] = card_id c['position'] = 1 if not c.get('names') else c.get( 'names', [c.get('name')]).index(c.get('name')) + 1 sql = 'INSERT INTO face (' sql += ', '.join(name for name, prop in card.face_properties().items() if not prop['primary_key']) sql += ') VALUES (' sql += ', '.join('%s' for name, prop in card.face_properties().items() if not prop['primary_key']) sql += ')' values = [ c.get(database2json(name)) for name, prop in card.face_properties().items() if not prop['primary_key'] ] try: db().execute(sql, values) except database.DatabaseException: print(c) raise for color in c.get('colors', []): color_id = db().value('SELECT id FROM color WHERE name = %s', [color]) # INSERT IGNORE INTO because some cards have multiple faces with the same color. See DFCs and What // When // Where // Who // Why. db().execute( 'INSERT IGNORE INTO card_color (card_id, color_id) VALUES (%s, %s)', [card_id, color_id]) for symbol in c.get('colorIdentity', []): color_id = db().value('SELECT id FROM color WHERE symbol = %s', [symbol]) # INSERT IGNORE INTO because some cards have multiple faces with the same color identity. See DFCs and What // When // Where // Who // Why. db().execute( 'INSERT IGNORE INTO card_color_identity (card_id, color_id) VALUES (%s, %s)', [card_id, color_id]) for supertype in c.get('supertypes', []): db().execute( 'INSERT INTO card_supertype (card_id, supertype) VALUES (%s, %s)', [card_id, supertype]) for subtype in c.get('subtypes', []): db().execute( 'INSERT INTO card_subtype (card_id, subtype) VALUES (%s, %s)', [card_id, subtype]) for info in c.get('legalities', []): format_id = get_format_id(info['format'], True) db().execute( 'INSERT INTO card_legality (card_id, format_id, legality) VALUES (%s, %s, %s)', [card_id, format_id, info['legality']]) if update_index: writer = WhooshWriter() c['id'] = c['cardId'] writer.update_card(c)