From 1e3cb3b89e6138891ab7f2dd3f14877da7f96423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E7=BE=85=E7=8B=BC?= Date: Thu, 9 Nov 2023 13:12:48 +0800 Subject: [PATCH] update (#1012) --- config/config.toml.example | 3 ++- core/petal.py | 20 ++++++++++--------- modules/ask/__init__.py | 10 +++++----- modules/maimai/__init__.py | 17 +++++++--------- modules/maimai/libraries/maimaidx_api_data.py | 9 --------- modules/maimai/regex.py | 19 ++++++++---------- 6 files changed, 33 insertions(+), 45 deletions(-) diff --git a/config/config.toml.example b/config/config.toml.example index ff6e50c6..3e5f847e 100644 --- a/config/config.toml.example +++ b/config/config.toml.example @@ -72,5 +72,6 @@ dice_output_len = 200 dice_detail_cnt = 5 dice_count_limit = 10 api_port = 5000 +enable_langsmith = false langsmith_endpoint = "https://api.smith.langchain.com" -langsmith_project = \ No newline at end of file +langsmith_project = diff --git a/core/petal.py b/core/petal.py index 1bf8bd81..78758d3e 100644 --- a/core/petal.py +++ b/core/petal.py @@ -20,21 +20,23 @@ PRICE_PER_1K_TOKEN = BASE_COST_GPT_3_5 * THIRD_PARTY_MULTIPLIER * PROFIT_MULTIPL USD_TO_CNY = Decimal('7.3') # Assuming 1 USD = 7.3 CNY CNY_TO_PETAL = 100 # 100 petal = 1 CNY +import json +from datetime import datetime, timedelta + async def get_petal_exchange_rate(): api_key = Config('exchange_rate_api_key') api_url = f'https://v6.exchangerate-api.com/v6/{api_key}/pair/USD/CNY' - data = await get_url(api_url, 200, fmt='json') - if data['result'] == "success": - exchange_rate = data['conversion_rate'] - petal_value = exchange_rate * CNY_TO_PETAL - return {"exchange_rate": exchange_rate, "exchanged_petal": petal_value} - return None + try: + data = await get_url(api_url, 200, fmt='json') + if data['result'] == "success": + exchange_rate = data['conversion_rate'] + petal_value = exchange_rate * CNY_TO_PETAL + return {"exchange_rate": exchange_rate, "exchanged_petal": petal_value} + except: + return None -import json -from datetime import datetime, timedelta - async def load_or_refresh_cache(): cache_dir = Config('cache_path') file_path = os.path.join(cache_dir, 'petal_exchange_rate_cache.json') diff --git a/modules/ask/__init__.py b/modules/ask/__init__.py index c6ef5b4f..091fa608 100644 --- a/modules/ask/__init__.py +++ b/modules/ask/__init__.py @@ -11,11 +11,11 @@ from core.dirty_check import check_bool, rickroll from core.petal import count_petal from core.utils.cooldown import CoolDown -os.environ['LANGCHAIN_TRACING_V2'] = "true" -os.environ['LANGCHAIN_ENDPOINT'] = Config('langsmith_endpoint') if Config('langsmith_endpoint') is not None else os.environ.get('LANGCHAIN_ENDPOINT') -os.environ['LANGCHAIN_PROJECT'] = Config('langsmith_project') if Config('langsmith_project') is not None else os.environ.get('LANGCHAIN_PROJECT') -os.environ['LANGCHAIN_API_KEY'] = Config('langsmith_api_key') if Config('langsmith_api_key') is not None else os.environ.get('LANGCHAIN_API_KEY') - +os.environ['LANGCHAIN_TRACING_V2'] = str(Config('enable_langsmith')) +if Config('enable_langsmith'): + os.environ['LANGCHAIN_ENDPOINT'] = Config('langsmith_endpoint') + os.environ['LANGCHAIN_PROJECT'] = Config('langsmith_project') + os.environ['LANGCHAIN_API_KEY'] = Config('langsmith_api_key') from langchain.callbacks import get_openai_callback # noqa: E402 from .agent import agent_executor # noqa: E402 diff --git a/modules/maimai/__init__.py b/modules/maimai/__init__.py index ede9f3e3..7bcd4fd6 100644 --- a/modules/maimai/__init__.py +++ b/modules/maimai/__init__.py @@ -2,7 +2,7 @@ from core.utils.image import msgchain2image from modules.maimai.libraries.maimai_best_50 import generate -from modules.maimai.libraries.maimaidx_api_data import get_alias, get_cover, search_by_alias, update_assets +from modules.maimai.libraries.maimaidx_api_data import get_alias, search_by_alias, update_assets from modules.maimai.libraries.maimaidx_music import Music, TotalList from modules.maimai.libraries.maimaidx_project import get_level_process, get_plate_process, get_player_score, get_rank, \ get_score_list @@ -21,7 +21,7 @@ diff_label_zht = ['綠', '黃', '紅'] def song_txt(music: Music): return [Plain(f"{music.id}\u200B. {music.title}{' (DX)' if music['type'] == 'DX' else ''}\n"), - BImage(get_cover(music.id), ), + BImage(f"https://www.diving-fish.com/covers/{get_cover_len5_id(music.id)}.png"), Plain(f"\n{'/'.join(str(ds) for ds in music.ds)}")] @@ -199,10 +199,9 @@ async def _(msg: Bot.MessageSession, id_or_alias: str, username: str = None): output = await get_player_score(msg, payload, sid) - file = get_cover(music['id']) await msg.finish( [Plain(f"{music['id']}\u200B. {music['title']}{' (DX)' if music['type'] == 'DX' else ''}\n"), - BImage(f"{file}"), Plain(output)]) + BImage(f"https://www.diving-fish.com/covers/{get_cover_len5_id(music['id'])}.png"), Plain(output)]) @mai.handle('plate [] {{maimai.help.plate}}') @@ -256,8 +255,8 @@ async def _(msg: Bot.MessageSession, level: str, goal: str, username: str = None if level in level_list: level_num = int(level.split('+')[0]) - if level_num < 8: - await msg.finish(msg.locale.t("maimai.message.process.less_than_8")) +# if level_num < 8: +# await msg.finish(msg.locale.t("maimai.message.process.less_than_8")) else: await msg.finish(msg.locale.t("maimai.message.process.error.goal_invalid")) @@ -377,7 +376,6 @@ async def _(msg: Bot.MessageSession, id_or_alias: str, diff: str = None): chart = music['charts'][diff_index] ds = music['ds'][diff_index] level = music['level'][diff_index] - file = get_cover(music['id']) if len(chart['notes']) == 4: message = msg.locale.t( "maimai.message.song.sd", @@ -403,12 +401,11 @@ async def _(msg: Bot.MessageSession, id_or_alias: str, diff: str = None): charter=chart['charter']) await msg.finish( [Plain(f"{music['id']}\u200B. {music['title']}{' (DX)' if music['type'] == 'DX' else ''}\n"), - BImage(f"{file}"), Plain(message)]) + BImage(f"https://www.diving-fish.com/covers/{get_cover_len5_id(music['id'])}.png"), Plain(message)]) else: - file = get_cover(music['id']) await msg.finish( [Plain(f"{music['id']}\u200B. {music['title']}{' (DX)' if music['type'] == 'DX' else ''}\n"), - BImage(f"{file}"), + BImage(f"https://www.diving-fish.com/covers/{get_cover_len5_id(music['id'])}.png"), Plain(msg.locale.t("maimai.message.song", artist=music['basic_info']['artist'], genre=music['basic_info']['genre'], bpm=music['basic_info']['bpm'], version=music['basic_info']['from'], diff --git a/modules/maimai/libraries/maimaidx_api_data.py b/modules/maimai/libraries/maimaidx_api_data.py index 9327eea5..8f0a1520 100644 --- a/modules/maimai/libraries/maimaidx_api_data.py +++ b/modules/maimai/libraries/maimaidx_api_data.py @@ -136,12 +136,3 @@ async def get_plate(msg, payload): await msg.finish(msg.locale.t("maimai.message.forbidden")) return data - - -def get_cover(sid): - cover_url = f"https://www.diving-fish.com/covers/{get_cover_len5_id(sid)}.png" - cover_path = f"{cover_dir}/{get_cover_len5_id(sid)}.png" - if os.path.exists(os.path.abspath(cover_path)): - return os.path.abspath(cover_path) - else: - return cover_url diff --git a/modules/maimai/regex.py b/modules/maimai/regex.py index 4adce4d0..ef90b51d 100644 --- a/modules/maimai/regex.py +++ b/modules/maimai/regex.py @@ -4,7 +4,7 @@ from core.builtins import Bot, Plain, Image as BImage from core.component import module from core.logger import Logger from core.utils.image import msgchain2image -from modules.maimai.libraries.maimaidx_api_data import get_alias, get_cover, search_by_alias +from modules.maimai.libraries.maimaidx_api_data import get_alias, search_by_alias from modules.maimai.libraries.maimaidx_music import Music, TotalList from modules.maimai.libraries.maimaidx_project import get_level_process, get_plate_process, get_player_score @@ -21,7 +21,7 @@ diff_label_zht = ['綠', '黃', '紅'] def song_txt(music: Music): return [Plain(f"{music.id}\u200B. {music.title}{' (DX)' if music['type'] == 'DX' else ''}\n"), - BImage(get_cover(music.id), ), + BImage(f"https://www.diving-fish.com/covers/{get_cover_len5_id(music.id)}.png"), Plain(f"\n{'/'.join(str(ds) for ds in music.ds)}")] @@ -70,10 +70,9 @@ async def _(msg: Bot.MessageSession): if not music: await msg.finish(msg.locale.t("maimai.message.music_not_found")) - file = get_cover(music['id']) await msg.finish( [Plain(f"{music['id']}\u200B. {music['title']} {' (DX)' if music['type'] == 'DX' else ''}\n"), - BImage(f"{file}"), + BImage(f"https://www.diving-fish.com/covers/{get_cover_len5_id(music['id'])}.png"), Plain(msg.locale.t("maimai.message.song", artist=music['basic_info']['artist'], genre=music['basic_info']['genre'], bpm=music['basic_info']['bpm'], version=music['basic_info']['from'], @@ -111,11 +110,10 @@ async def _(msg: Bot.MessageSession): payload = {'username': username} output = await get_player_score(msg, payload, sid) - - file = get_cover(music['id']) + await msg.finish( [Plain(f"{music['id']}\u200B. {music['title']}{' (DX)' if music['type'] == 'DX' else ''}\n"), - BImage(f"{file}"), Plain(output)]) + BImage(f"https://www.diving-fish.com/covers/{get_cover_len5_id(music['id'])}.png"), Plain(output)]) @mai_regex.handle(re.compile(r"(?:id)?(\d+)\s?有什(么别|麼別)名", flags=re.I), desc='{maimai.help.maimai_regex.alias}') @@ -133,10 +131,9 @@ async def _(msg: Bot.MessageSession): result += "\n".join(alias) await msg.finish([Plain(result.strip())]) - file = get_cover(music['id']) await msg.finish( [Plain(f"{music['id']}\u200B. {music['title']} {' (DX)' if music['type'] == 'DX' else ''}\n"), - BImage(f"{file}"), + BImage(f"https://www.diving-fish.com/covers/{get_cover_len5_id(music['id'])}.png"), Plain(msg.locale.t("maimai.message.song", artist=music['basic_info']['artist'], genre=music['basic_info']['genre'], bpm=music['basic_info']['bpm'], version=music['basic_info']['from'], @@ -228,8 +225,8 @@ async def _(msg: Bot.MessageSession): if level in level_list: level_num = int(level.split('+')[0]) - if level_num < 8: - await msg.finish(msg.locale.t("maimai.message.process.less_than_8")) +# if level_num < 8: +# await msg.finish(msg.locale.t("maimai.message.process.less_than_8")) else: await msg.finish(msg.locale.t("maimai.message.process.error.goal_invalid"))