From cda477e561df270d29e961f6066287a86d0f9589 Mon Sep 17 00:00:00 2001 From: yshtcn Date: Thu, 12 Oct 2023 21:13:36 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E9=80=82=E5=90=88=E6=89=93=E5=8C=85=20-=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=89=93=E5=8C=85=E4=B8=BAexe=E7=9A=84=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Diary Assistant.py | 39 ++++++---- Diary Assistant_pyinstaller.bat | 123 ++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 13 deletions(-) create mode 100644 Diary Assistant_pyinstaller.bat diff --git a/Diary Assistant.py b/Diary Assistant.py index eec4dad..ccbfc2c 100644 --- a/Diary Assistant.py +++ b/Diary Assistant.py @@ -10,18 +10,23 @@ proxies = None TOKEN = None -# 定义配置文件名 -config_filename = "bot_config.json" + # 定义当前脚本所在目录(如果是frozen的使用exe所在的目录) current_dir = os.path.dirname(os.path.abspath(__file__)) if getattr(sys, 'frozen', False): current_dir = os.path.dirname(sys.executable) +# 定义配置文件名 +config_filename = "bot_config.json" +blacklist_filename="blacklist.json" +message_queue_filename="message_queue.json" +user_data_filename="user_data.json" + + # 检查配置文件是否存在 config_path = os.path.join(current_dir, config_filename) - if os.path.exists(config_path): # 读取配置文件 with open(config_path, 'r') as f: @@ -32,6 +37,12 @@ if os.path.exists(config_path): # 检查Token是否已设置 if not TOKEN: print("Please set your bot token in the config file.") + input("Press any key to exit...") + exit(1) + #或者检查TOKEN是否等于”Your_Token_Here“ + elif TOKEN == "Your_Token_Here": + print("Please set your bot token in the config file.") + input("Press any key to exit...") exit(1) else: # 如果配置文件不存在,则创建一个新的配置文件并写入示范Token和示范代理 @@ -48,6 +59,8 @@ else: with open(config_path, 'w') as f: f.write(config_str) print(f"Config file created at {config_path}. Please set your bot token.") + # 等待用户按任意键退出 + input("Press any key to exit...") exit(1) # 设置代理 @@ -91,7 +104,7 @@ def set_bot_commands(): # 尝试从文件中加载黑名单 try: - with open(os.path.join(current_dir, 'blacklist.json'), 'r') as f: + with open(os.path.join(current_dir, blacklist_filename), 'r') as f: blacklist = json.load(f) except FileNotFoundError: blacklist = [] @@ -113,7 +126,7 @@ def send_message(chat_id, text): try: # 尝试从文件中加载消息队列 try: - with open(os.path.join(current_dir, 'message_queue.json'), 'r') as f: + with open(os.path.join(current_dir, message_queue_filename), 'r') as f: message_queue = json.load(f) except FileNotFoundError: message_queue = [] @@ -122,7 +135,7 @@ def send_message(chat_id, text): message_queue.append({'chat_id': chat_id, 'text': text}) # 将更新后的消息队列保存回文件 - with open(os.path.join(current_dir, 'message_queue.json'), 'w') as f: + with open(os.path.join(current_dir, message_queue_filename), 'w') as f: json.dump(message_queue, f) except Exception as e: print(f"Error queuing message: {e}") @@ -135,7 +148,7 @@ def funcion_send_message(chat_id, text, reply_markup=None): def process_message_queue(): # 尝试从文件中加载消息队列 try: - with open(os.path.join(current_dir, 'message_queue.json'), 'r') as f: + with open(os.path.join(current_dir, message_queue_filename), 'r') as f: message_queue = json.load(f) except FileNotFoundError: message_queue = [] @@ -164,7 +177,7 @@ def process_message_queue(): time.sleep(10) # 将更新后(或未成功发送的)消息队列保存回文件 - with open(os.path.join(current_dir, 'message_queue.json'), 'w') as f: + with open(os.path.join(current_dir, message_queue_filename), 'w') as f: json.dump(remaining_messages, f) @@ -246,16 +259,16 @@ def main(): # 保存数据到文件 - with open(os.path.join(current_dir, 'user_data.json'), 'w') as f: + with open(os.path.join(current_dir, user_data_filename), 'w') as f: json.dump(user_data, f) - with open(os.path.join(current_dir, 'blacklist.json'), 'w') as f: + with open(os.path.join(current_dir, blacklist_filename), 'w') as f: json.dump(blacklist, f) # 保存数据到文件 - with open(os.path.join(current_dir, 'user_data.json'), 'w') as f: + with open(os.path.join(current_dir, user_data_filename), 'w') as f: json.dump(user_data, f) - with open(os.path.join(current_dir, 'blacklist.json'), 'w') as f: + with open(os.path.join(current_dir, blacklist_filename), 'w') as f: json.dump(blacklist, f) @@ -263,7 +276,7 @@ def main(): else: print(f"{URL} Received updates: {updates}") print("Error or no updates; retrying in 10 seconds...") - time.sleep(10) # 等待5秒再重试 + time.sleep(10) print("Process message queue...") diff --git a/Diary Assistant_pyinstaller.bat b/Diary Assistant_pyinstaller.bat new file mode 100644 index 0000000..3cd28b1 --- /dev/null +++ b/Diary Assistant_pyinstaller.bat @@ -0,0 +1,123 @@ +@echo off + + +title "AutoPyInstallerװpyinstaller" +:: װ/pyinstaller(ע⣺ϣԶװ/pyinstallerǵע͵ +pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple + +title "AutoPyInstallerװõĿ" +pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple +cls + +:: ± +title "AutoPyInstallerʼ" + +:: һЩϢ +set "ProductName=Diary Assistant" +set "InternalName=yshtcn" +set "Comments=GitHub: https://github.com/yshtcn/DiaryAssistant_bot" + + +:: ڵĿ¼ +cd /d %~dp0 + + + +:: ± +title "AutoPyInstallerɰ汾ļ" + + + +:: ʹWMICȡǰ +for /f "delims=" %%a in ('wmic os get localdatetime ^| find "."') do set datetime=%%a + + +:: ֽʱַ +set "year=%datetime:~0,4%" +set "month=%datetime:~4,2%" +set "day=%datetime:~6,2%" + +:: ȡ汾һλ +set /p "revision=İ汾:(%year%, %month%, %day%,[?]):" + +:: ǰ汾Ŀ¼δȥȵ0 +set "versionFolder=%year%_%month%_%day%_%revision%" + +:: ȥºյǰ㣨У +set /a "month=1%month%-100" +set /a "day=1%day%-100" + +:: ʼʱļ +set "tempFile=temp.txt" + +:: ջ򴴽ʱļ +echo. > %tempFile% + +:: дʱļ +echo # version_info.txt >> %tempFile% +echo VSVersionInfo( >> %tempFile% +echo ffi=FixedFileInfo( >> %tempFile% +echo filevers=(%year%, %month%, %day%, %revision%), >> %tempFile% +echo prodvers=(%year%, %month%, %day%, %revision%), >> %tempFile% +echo mask=0x3f, >> %tempFile% +echo flags=0x0, >> %tempFile% +echo OS=0x4, >> %tempFile% +echo fileType=0x1, >> %tempFile% +echo subtype=0x0, >> %tempFile% +echo date=(0, 0) >> %tempFile% +echo ), >> %tempFile% +echo kids=[ >> %tempFile% +echo StringFileInfo( >> %tempFile% +echo [ >> %tempFile% +echo StringTable( >> %tempFile% +echo '040904B0', >> %tempFile% +echo [StringStruct('ProductName', '%ProductName%'), >> %tempFile% +echo StringStruct('ProductVersion', '%year%, %month%, %day%, %revision%'), >> %tempFile% +echo StringStruct('InternalName', '%InternalName%'), >> %tempFile% +echo StringStruct('CompanyName', 'ysht.me - %Comments%'), >> %tempFile% +echo StringStruct('Comments', '%Comments%'), >> %tempFile% +echo StringStruct('LegalCopyright', 'Apache-2.0 license - %Comments%'), >> %tempFile% +echo ] >> %tempFile% +echo ), >> %tempFile% +echo ] >> %tempFile% +echo ), >> %tempFile% +echo VarFileInfo([VarStruct('Translation', [0x804, 1200])]) >> %tempFile% +echo ] >> %tempFile% +echo ) >> %tempFile% + +:: ʱļƶյ version_info.txt +move /Y %tempFile% version_info.txt + +:: Ϣ +echo 汾Ϣѳɹɡ + +::± +title "AutoPyInstallerʼ" + +:: Ŀ¼ڣ +md build +:: ɾĿ¼ͬ汾ŵļ +rd /S /Q %~dp0\build\%versionFolder% + +:: ɾĿ¼Ĺļ +del /q %~dp0\build\heartbeat.spec +rd /S /Q %~dp0\build\build + +:: Ŀ¼ʼ +cd /d %~dp0\build +pyinstaller --onefile --version-file %~dp0\version_info.txt "%~dp0\Diary Assistant.py" + +::± +title "AutoPyInstallerϣһЩβ" +:: ٴΣɾĿ¼ͬ汾ŵļ +rd /S /Q %~dp0\build\%versionFolder% +:: ٴΣɾĿ¼Ĺļ +del /q "%~dp0\build\Diary Assistant.spec" +rd /S /Q %~dp0\build\build + +:: ɴĿ¼԰汾 +rename dist %versionFolder% + +:: Ҫһļ +copy %~dp0\config.Exsample.ini %~dp0\build\%versionFolder%\ +copy %~dp0\README.md %~dp0\build\%versionFolder%\ \ No newline at end of file