def addMessage(chat_id): new_idMessage = max(coll.distinct("idMessage")) + 1 idUser = int(request.forms.get("idUser")) message = str(request.forms.get("text")) fields = list( coll.find({"idUser": idUser}, { "userName": 1, "idUser": 1, "_id": 0, "idChat": 1 })) print(fields) name = fields[0]["userName"] for f in fields: if f["userName"] == name: new_idUser = f["idUser"] else: name = name new_message = { "idUser": idUser, "userName": name, "idChat": int(chat_id), "idMessage": new_idMessage, "text": message } print(new_message) new_user = {"idUser": new_idUser, "userName": name} print(new_user) coll.insert_one(new_message)
def getSentiment(chat_id): query = list( coll.find({'idChat': int(chat_id)}, { "userName": 1, "idUser": 1, "text": 1, "_id": 0 })) sid = SentimentIntensityAnalyzer() total_info = [] for text in query: info = {} info["userName"] = text["userName"] info["idChat"] = int(chat_id) info["text"] = text["text"] info["sentiment"] = sid.polarity_scores(text["text"]) total_info.append(info) print(total_info) return dumps(total_info) """comp_sent = [s['sentiment']['compound'] for s in total_info]
def conver(tipo): return dumps(coll.find({'userName': tipo}))
def index(): return dumps(coll.find())
def get_user(user): return dumps(coll.find({"userName": str(user)}, {"text": 0}))
def get_chat(chat_id): return dumps(coll.find({"idChat": int(chat_id)}, {"_id": 0}))