From 0392f4e52fc3ce017d7fcc51c561efeac2452360 Mon Sep 17 00:00:00 2001 From: mohab Date: Tue, 9 Jan 2024 17:18:46 +0200 Subject: [PATCH] feat(BREAKING CHANGE): support mid toot mention refactor getReplyFromDB --- db.py | 57 ++++++++++++++++++++++++++++++++++------------------ test_db.py | 14 +++++++++++++ test_util.py | 7 ++++--- util.py | 6 +++++- 4 files changed, 60 insertions(+), 24 deletions(-) diff --git a/db.py b/db.py index e7aee01..014752f 100644 --- a/db.py +++ b/db.py @@ -5,8 +5,7 @@ from pymongo.mongo_client import MongoClient from pymongo.server_api import ServerApi -def getReplyFromDB(charName: str, flags: [str]): - +def get_DB_URI(): # Load environment variables. try: load_dotenv() @@ -14,8 +13,12 @@ def getReplyFromDB(charName: str, flags: [str]): except Exception as e: print(e) + return db_uri + + +def getMongoClinet(): # Create a new MongoDB client and connect to the server. - client = MongoClient(db_uri, server_api=ServerApi("1")) + client = MongoClient(get_DB_URI(), server_api=ServerApi("1")) # Send a ping to confirm a successful connection. try: @@ -26,53 +29,67 @@ def getReplyFromDB(charName: str, flags: [str]): except Exception as e: print(e) + return client + + +def getCollection(): # Get database. - db = client.fightlike + db = getMongoClinet().fightlike # Get collection. collection = db["character"] + return collection + + +def getArchetype(charName: str, flags: [str]): # Formulate archetype query. archetypeQuery = { "name": charName } # Check for and add game flag, if present. - title = '' + title = "" for flag in flags: if (flag.startswith("!")): title = flag[1:] archetypeQuery["game.title"] = flag[1:] - flags.remove(flag) # Get character document. - charObj = collection.find_one(archetypeQuery) + charObj = getCollection().find_one(archetypeQuery) # Assume charName is slug if name is not in database. if charObj is None: del archetypeQuery["name"] archetypeQuery["slug"] = charName.casefold() - charObj = collection.find_one(archetypeQuery) + charObj = getCollection().find_one(archetypeQuery) - # Assume game.title is the game's slug. + # Assume game.title is game's slug. if charObj is None and title: del archetypeQuery["game.title"] archetypeQuery["game.slug"] = title - charObj = collection.find_one(archetypeQuery) + charObj = getCollection().find_one(archetypeQuery) # Assume charName is name again. if charObj is None: del archetypeQuery["slug"] archetypeQuery["name"] = charName - charObj = collection.find_one(archetypeQuery) + charObj = getCollection().find_one(archetypeQuery) + + try: + return charObj["archetype"][0] + except Exception: + return "" - # Assume character is not in database if slug is not in database. - if charObj is None: - return "Unfortunately, your main is not in Fightlike's database yet. You can fill this form to add them: https://fightlike.mohab.xyz/submit" - # Pull archetype from charObj. - archetype: str = charObj["archetype"][0] +def getReplyFromDB(charName: str, flags: [str]): + + archetype: str = getArchetype(charName, flags) + + # If archetype is empty, assume character is not in database. + if (archetype == ""): + return "Unfortunately, your main is not in Fightlike's database yet. You can fill this form to add them: https://fightlike.mohab.xyz/submit" # Formulate query. query = { @@ -81,17 +98,17 @@ def getReplyFromDB(charName: str, flags: [str]): # Add flags to query. for flag in flags: - if (flag == "rollback" or flag == "delay"): + if (flag.startswith("!")): + continue + elif (flag == "rollback" or flag == "delay"): query["game.netcode"] = flag else: query["game.franchise"] = flag - print(query) - # Get similar characters and compose reply. reply = f'{archetype.capitalize()} character(s) to try:\n\n' - for entry in collection.find(query): + for entry in getCollection().find(query): if (charName == entry["name"]): continue elif (entry["game"]["netcode"] != "rollback"): diff --git a/test_db.py b/test_db.py index d9e590c..0f8f377 100644 --- a/test_db.py +++ b/test_db.py @@ -1,6 +1,20 @@ import db +def test_getArchetype(): + + reply = db.getArchetype("bridget", ["!ggst", "rollback", "blazblue"]) + + assert reply == "all-rounder" + + +def test_getArchetypeIfCharDoesNotExist(): + + reply = db.getArchetype("mohab", []) + + assert reply == "" + + # Test for 'Nine the Phantom' substring in MongoDB response. def test_getReplyFromDBWithGameFlag(): diff --git a/test_util.py b/test_util.py index 5d9b15d..5523b0c 100644 --- a/test_util.py +++ b/test_util.py @@ -5,7 +5,7 @@ import util def test_getTootText(): tootText = util.getTootText( - '

@fightlike eddie

' + '

Find characters like Eddie: @fightlike eddie

' ) assert tootText == "@fightlike eddie" @@ -27,6 +27,7 @@ def test_getFullCharName(): def test_getFlags(): - flags = util.getFlags("@fightlike eddie !ggxxacpr !blazblue !rollback") - assert flags[0] == "ggxxacpr" and flags[1] == "blazblue" and flags[2] == "rollback" + flags = util.getFlags("@fightlike eddie !!ggxxacpr !blazblue !rollback") + + assert flags[0] == "!ggxxacpr" and flags[1] == "blazblue" and flags[2] == "rollback" diff --git a/util.py b/util.py index f4b2036..e5d3fc4 100644 --- a/util.py +++ b/util.py @@ -8,7 +8,11 @@ def getTootText(tootContent: str): tootContent, "html.parser" ) - return parsedTootContent.get_text().strip(' \t\n\r') + tootText: str = parsedTootContent.get_text().strip(' \t\n\r') + + beginIndex: int = tootText.find("@fightlike") + + return tootText[beginIndex:] # Pull character name from text. -- 2.45.2