async def add_character(self, context: commands.Context, url: str) -> Investigator: result = await self.request(url) author_id = str(context.author.id) author_name = str(context.author.name) # もし画像を見つけたらイメージ画像として採用する。(複数ある場合は最後のもの) image_url = "" for attachment in context.message.attachments: print(attachment) temp_image_url = str(attachment.url) if await self.is_image_url(temp_image_url): image_url = temp_image_url df = self.__sheet_controller.find_character_by_site_info( author_id, result.site_id1, result.site_id2, result.site_url) if len(df) == 0: # 新規 # activeの設定初期値の決定 author_df = self.__sheet_controller.find_characters_by_author_id( author_id) # 初めての登録キャラの場合はTrueとする。 value_active = False if len(author_df) >= 1 else True result.unique_id = self.__sheet_controller.assign_unique_id( result.site_id1, result.site_id2, result.site_url) result.author_id = author_id result.author_name = author_name result.active = value_active result.lost = False result.image_url = image_url else: # 登録済み result.unique_id = str(df["unique_id"].values[0]) result.author_id = author_id result.author_name = author_name result.active = strtobool(df["active"].values[0]) result.lost = strtobool(df["lost"].values[0]) if len(image_url) >= 1: result.image_url = image_url else: result.image_url = str(df["character_image_url"].values[0]) record = LakshmiCharactersSheetRecord() record.set_values_by_investigator(result) self.__sheet_controller.merge_character_by_unique_id(record) # NOTE: self.__sheet_controllerのfind等からsaveまでの間にawait処理が入らないように注意。 await self.register_investigator_in_cache(result) await self.background_save() return result
def insert_character(self, record: LakshmiCharactersSheetRecord): row = self.pandasheet.createRowSeries() row["unique_id"] = record.unique_id row["site_id1"] = record.site_id1 row["site_id2"] = record.site_id2 row["site_url"] = record.site_url row["character_name"] = record.character_name row["character_image_url"] = record.character_image_url row["author_id"] = record.author_id row["author_name"] = record.author_name row["active"] = record.get_active_to_string() row["lost"] = record.get_lost_to_string() self.pandasheet.appendRow(row) return self
def update_character_by_index(self, index, record: LakshmiCharactersSheetRecord): row = pd.Series(self.pandasheet.df.iloc[index]) row["unique_id"] = record.unique_id row["site_id1"] = record.site_id1 row["site_id2"] = record.site_id2 row["site_url"] = record.site_url row["character_name"] = record.character_name row["character_image_url"] = record.character_image_url row["author_id"] = record.author_id row["author_name"] = record.author_name row["active"] = record.get_active_to_string() row["lost"] = record.get_lost_to_string() self.pandasheet.df.iloc[index] = row return self
async def set_character_lost( self, context: commands.Context, unique_id: str) -> LakshmiCharactersSheetRecord: result = LakshmiCharactersSheetRecord() author_id = str(context.author.id) author_name = str(context.author.name) df = self.__sheet_controller.find_character_by_unique_id( author_id, unique_id) if len(df) == 0: # 登録がない raise LakshmiErrors.CharacterNotFoundException() result.set_values_by_dataframe(df) result.author_id = author_id result.author_name = author_name result.lost = True index = int(df.index[0]) self.__sheet_controller.update_character_by_index(index, result) # NOTE: self.__sheet_controllerのfind等からsaveまでの間にawait処理が入らないように注意。 await self.background_save() return result
async def get_character_information(self, context: commands.Context, unique_id: str = "") -> Investigator: author_id = str(context.author.id) author_name = str(context.author.name) if len(unique_id) >= 1: df = self.__sheet_controller.find_character_by_unique_id( author_id, unique_id) else: df = self.__sheet_controller.find_active_character_by_author_id( author_id) if len(df) == 0: # 登録がない raise LakshmiErrors.CharacterNotFoundException() # 登録済み record = LakshmiCharactersSheetRecord() record.set_values_by_dataframe(df) record.author_id = author_id record.author_name = author_name #NOTE: find_character_by_unique_idの後だが、background_saveしないのでOKとする。 result = await self.request(record.site_url) self.__sheet_controller.set_investigator_by_record(result, record) await self.register_investigator_in_cache(result) return result
async def get_character_list( self, context: commands.Context) -> List[LakshmiCharactersSheetRecord]: result = [] author_id = str(context.author.id) author_name = str(context.author.name) df = self.__sheet_controller.find_characters_by_author_id(author_id) if len(df) >= 1: # Sort df = df.sort_values('unique_id', ascending=True) # 1件以上あり for index, row in df.iterrows(): record = LakshmiCharactersSheetRecord() record.set_values_by_series(row) record.author_id = author_id record.author_name = author_name result.append(record) return result