def importWeaponsByJob(job: str, amount: int): r = Reader(job) results = r.getArms(amount) msg: List[Dict] = [] if len(results) >= 1: db = DB(Base) session = db.newSession() for i in results: try: sqlRes = session.query(Weapon).filter(Weapon.name == i.name).one() msg.append({ "msg": f"Item was already present.", "item": i.name }) except Exception as e: w = Weapon() w.convertToSqlObject(i) w.stats = Stats() w.stats.convertToSqlObject(i.stats) w.repair = Repair() w.repair.convertToSqlObject(i.repair) w.materia = Materia() w.materia.convertToSqlObject(i.materia) session.add(w) session.commit() msg.append({ "msg": "Item was added to the database", "item": i.name }) session.close() return dumps(msg)
class DbInit(): def __init__(self) -> None: self.db = DB(Base) self.cd = os.getcwd() self.cd = f"{self.cd}/xivdb/static/images/itemIcons/" pass def runJob(self) -> None: self.downloadExternalData() pass def downloadExternalData(self) -> None: r = Reader(job='gnb') gnbList: List[Weapon] = r.getArms(recordLimit=10) for i in gnbList: self.checkSql(i) def checkSql(self, item: Weapon) -> None: session = self.db.newSession() try: returnedItem: Weapon = session.query(Weapon).filter(Weapon.name == item.name).one() session.close() except Exception as e: w: Weapon = Weapon() w.convertToSqlObject(item) s: Stats = Stats() s.convertToSqlObject(item.stats) r: Repair = Repair() r.convertToSqlObject(item.repair) m: Materia = Materia() m.convertToSqlObject(item.materia) w.stats = s w.repair = r w.materia = m self.downloadPicturesToStatic(w.pictureUrl, w.name) session.add(w) session.commit() session.close() def downloadPicturesToStatic(self, pictureUrl: str, itemName: str) -> None: png = f"{self.cd}{itemName}.png" if os.path.exists(png) == False: self.downloadPicture(png, pictureUrl) if os.path.exists(png) == False: print(f"failed to download icon for {itemName}") def downloadPicture(self, pngPath: str, pictureUrl: str) -> None: try: requestRes = requests.get(pictureUrl, stream=True) requestRes.raw.decode_content = True with open(pngPath, 'wb') as imgFile: shutil.copyfileobj(requestRes.raw, imgFile) except Exception as e: print(e)
from flask import render_template, Blueprint, send_file from xivdb.sql import DB, Base, Weapon, Repair, Materia, Stats, PictureIcon from typing import List from sqlalchemy.orm import sessionmaker, Session, Query import os import io d = DB(Base) session: Session = d.newSession() weapon_bp = Blueprint('weapons', __name__, template_folder="templates") @weapon_bp.route('/') def index(): return render_template('weapons/index.html') @weapon_bp.route('/list') def weaponsList(): items: List[dict] = [] session = d.newSession() try: for names in session.query(Weapon.name,Weapon.level ,Weapon.itemLevel ,Weapon.id ): items.append({ 'name': names[0] ,'level': names[1] ,'itemLevel': names[2] ,'id': names[3] }) except Exception as e: print(e)
from xivdb.sql import DB, Base, Weapon, Repair, Materia, Stats from xivdb.importCsv import importCsv from typing import List from sqlalchemy.orm import sessionmaker from XivDbReader import Reader import sqlalchemy.orm d = DB(Base) session: sessionmaker = d.newSession() w = d.newWeapon() read: Reader = Reader(job='whm') whm = read.getArms(recordLimit=1) for i in whm: try: res: Weapon = session.query(Weapon).filter(Weapon.name == i.name).one() except Exception as e: #print(f"{i.name} was not found in the DB.") ic = importCsv() counter: int = 1 weapons: List[Weapon] = ic.getAllWeapons() stats: List[Stats] = ic.getAllStats() repairs: List[Repair] = ic.getAllRepairs() materias: List[Materia] = ic.getAllMateria() counter: int = 0 for i in weapons: