def get(cls): logger = HenriqueLogger.func_level2logger(cls.get, logging.DEBUG) sender_name = request.args.get(cls.Data.Field.SENDER_NAME) text_in = request.args.get(cls.Data.Field.TEXT) newline = request.args.get(cls.Data.Field.NEWLINE) logger.debug({"sender_name":sender_name, "text_in":text_in, "newline":newline}) if not HenriqueCommand.text2is_query(text_in): return None chatroom = ChatroomKakaotalk.chatroom() Chatroom.chatrooms2upsert([chatroom]) channel_user = ChannelUserKakaotalk.sender_name2channel_user(sender_name) ChannelUser.channel_users2upsert([channel_user]) packet = {KhalaPacket.Field.TEXT: text_in, KhalaPacket.Field.CHATROOM: Chatroom.chatroom2codename(chatroom), KhalaPacket.Field.CHANNEL_USER: ChannelUser.channel_user2codename(channel_user), KhalaPacket.Field.SENDER_NAME: sender_name, } logger.debug({"packet": packet,}) text_response = HenriquePacket.packet2response(packet) if not text_response: return None if cls.packet2skip_response(packet): # run packet but do not respond return None text_out = newline.join(text_response.splitlines()) if newline else text_response return text_out, 200
def codenames2docs(cls, codenames): collection = ChannelUserCollection.collection() query = {ChannelUser.Field.CODENAME: {"$in": codenames}} cursor = collection.find(query) h_codename2doc = merge_dicts( [{ ChannelUser.channel_user2codename(doc): doc } for doc in map(MongoDBTool.bson2json, cursor)], vwrite=vwrite_no_duplicate_key) doc_list = lmap(h_codename2doc.get, codenames) return doc_list
def tuple2pair(cls, t): logger = HenriqueLogger.func_level2logger(cls.tuple2pair, logging.DEBUG) def chatuser_uuid2channel_user(chatuser_uuid, alias=None): if not chatuser_uuid: return None row = ChatuserTable.uuid2row(chatuser_uuid) if not row: logger.debug({"chatuser_uuid":chatuser_uuid}) return None channel_user = ChatuserTable.row2channel_user(row, alias=alias) return channel_user j_postgres = t[PortTradegoodStateTable.index_json()] sender_name = j_postgres.get("sender_name") created_at = DatetimeTool.fromisoformat(j_postgres["created_at"]) port_name_en = PortTradegoodStateTable.tuple2port_name_en(t) port = cls.name_en2port(port_name_en) tradegood_name_en = PortTradegoodStateTable.tuple2tradegood_name_en(t) tradegood = cls.name_en2tradegood(tradegood_name_en) rate = PortTradegoodStateTable.tuple2rate(t) trend = PortTradegoodStateTable.tuple2trend(t) chatuser_uuid = PortTradegoodStateTable.tuple2chatuser_uuid(t) channel_user = chatuser_uuid2channel_user(chatuser_uuid, alias=sender_name) channel_user_code = ChannelUser.channel_user2codename(channel_user) if channel_user else None server_alias = str2lower(jdown(j_postgres, ["server", "name"])) # logger.debug({"server_alias":server_alias}) server = Server.alias_lang2server(server_alias, "ko") assert_true(server,server_alias) marketprice = {MarketpriceDoc.Field.CREATED_AT: created_at, MarketpriceDoc.Field.PORT: Port.port2codename(port), MarketpriceDoc.Field.TRADEGOOD: Tradegood.tradegood2codename(tradegood), MarketpriceDoc.Field.RATE: rate, MarketpriceDoc.Field.TREND: trend, MarketpriceDoc.Field.CHANNEL_USER: channel_user_code, MarketpriceDoc.Field.SERVER: Server.server2codename(server), } return marketprice, channel_user
def doc2pair(channel_user): cond = {ChannelUser.Field.CODENAME: ChannelUser.channel_user2codename(channel_user)} return cond, channel_user
def _docs2cache(cls, docs): logger = KhalaLogger.func_level2logger(cls._docs2cache, logging.DEBUG) for doc in docs: codename = ChannelUser.channel_user2codename(doc) CacheManager.add2cache(cls.codenames2docs, doc, args=[codename])