- 修改代码适合打包

- 增加打包为exe的脚本
This commit is contained in:
yshtcn 2023-10-12 21:13:36 +08:00
parent da64eff087
commit cda477e561
2 changed files with 149 additions and 13 deletions

View File

@ -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...")

View File

@ -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%\