def scrape_all(): from mtgoracle.dbengine import dbPath, get_engine from mtgoracle.model import metadata dbbak = dbPath + '.bak' if os.path.exists(dbbak): os.remove(dbbak) if os.path.exists(dbPath): os.rename(dbPath, dbbak) engine = get_engine(path=dbPath, echo=False) metadata.create_all(bind=engine) # @UndefinedVariable @IgnorePep8 DBSession.configure(bind=engine) setdicts = scrape_setdicts() for setdict in setdicts: cset = CardSet(**setdict) DBSession.add(cset) print cset cardtuples = cardandprints_from_setcode(cset.code) for carddict, printdict in cardtuples: card = DBSession.query(Card).\ filter_by(name=carddict['name']).first() if card is None: typestrs = carddict.pop('types') stypestrs = carddict.pop('subtypes') card = Card(**carddict) DBSession.add(card) print card for typestr in typestrs: ctype = DBSession.query(CardType).\ filter_by(name=typestr).first() if ctype is None: ctype = CardType(name=typestr) DBSession.add(ctype) print ctype card.types.append(ctype) for stypestr in stypestrs: cstype = DBSession.query(CardSubtype).\ filter_by(name=stypestr).first() if cstype is None: cstype = CardSubtype(name=stypestr) DBSession.add(cstype) print cstype card.subtypes.append(cstype) printing = CardPrinting(**printdict) printing.setcode = cset.code printing.cardname = card.name DBSession.add(printing) print printing DBSession.commit()
def with_type(typestr): return DBSession.query(Card).\ filter(Card.types.any(CardType.name == typestr)).all()
def with_subtype(stypestr): return DBSession.query(Card).\ filter(Card.subtypes.any(CardSubtype.name == stypestr)).all()
def by_name(namestr): return DBSession.query(Card).filter_by(name=namestr).first()