from fastapi import FastAPI, HTTPException import uvicorn from database.database import Database app = FastAPI() db = Database() @app.get("/artnet/metadata/art") async def art(id: int = None, hash: str = None, tag_id: int = None): if id is None and hash is None and tag_id is None: raise HTTPException(status_code=406, detail="requires id or hash param") result = None if id is not None: result = db.get_art_by_id(id) elif hash is not None: result = db.get_art_by_hash(hash) elif tag_id is not None: result = db.get_tag_art(tag_id) if result is not None: return result raise HTTPException(status_code=404, detail="Art was not found!") @app.get("/artnet/metadata/presence") async def presence(name: str = None, domain: str = None, artist_id: int = None, art_id: int = None, art_md5: str = None): if name is None and domain is None and artist_id is None and art_id is None and art_md5 is None: raise HTTPException(status_code=406, detail="You must query with at least one parameter!") result = None if artist_id is not None and name is None and domain is None: result = db.get_artist_presences(artist_id) elif art_id is not None and name is None and domain is None: result = db.get_art_presences_by_id(art_id) elif art_md5 is not None and name is None and domain is None: result = db.get_art_presences_by_hash(art_md5) elif name is not None and domain is not None: result = db.get_presence(name, domain) if result is not None: return result raise HTTPException(status_code=404, detail="Presence was not found!") @app.get("/artnet/metadata/artist") async def artist(id: int = None, topic_id: int = None): if id is None and topic_id is None: raise HTTPException(status_code=406) result = None if id is not None: result = db.get_artist(id) elif topic_id is not None: result = db.get_topic_artists(topic_id) if result is not None: return result raise HTTPException(status_code=404, detail="Artist was not found!") @app.get("/artnet/metadata/topic") async def topic(name: str = None, id: int = None, artist_id: int = None): if name is None and id is None and artist_id is None: raise HTTPException(status_code=406) result = None if name is not None or id is not None: if id is not None: result = db.get_topic_by_id(id) elif name is not None: result = db.get_topic_by_name(name) elif artist_id is not None: result = db.get_artist_topics(artist_id) if result is not None: return result raise HTTPException(status_code=404, detail="Topic was not found!") @app.get("/artnet/metadata/tag") async def tag(id: int = None, name: str = None, fuzzy: bool = False, category: int = None): if id is None and name is None and category is None: raise HTTPException(status_code=406) result = None if fuzzy: if name is not None: result = db.search_tag_by_name_fuzzy(name) if result is not None: return result raise HTTPException(status_code=404, detail="No matching tag found!") else: if id is not None: result = db.get_tag_by_id(id) elif name is not None: result = db.get_tag_by_name(name) elif category is not None: result = db.get_category_tags(category) if result is not None: return result raise HTTPException(status_code=404, detail="Tag was not found!") @app.get("/artnet/metadata/category") async def category(name: str = None, id: int = None): if name is None and id is None: raise HTTPException(status_code=406) result = None if id is not None: result = db.get_category_by_id(id) elif name is not None: result = db.get_category_by_name(name) if result is not None: return result raise HTTPException(status_code=404, detail="Category not found!") if __name__ == "__main__": uvicorn.run(app, host="127.0.0.1", port=8000)