- 修改代码适合打包

- 增加打包为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<EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>pyinstaller"
:: <20><>װ/<2F><><EFBFBD><EFBFBD>pyinstaller(ע<><EFBFBD><E2A3BA>ϣ<EFBFBD><CFA3><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>װ/<2F><><EFBFBD><EFBFBD>pyinstaller<65>ǵ<EFBFBD>ע<EFBFBD>͵<EFBFBD><CDB5><EFBFBD>
pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
title "AutoPyInstaller<EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD>Ŀ<EFBFBD>"
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
cls
:: <20><><EFBFBD>±<EFBFBD><C2B1><EFBFBD>
title "AutoPyInstaller<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>"
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һЩ<D2BB><D0A9>Ϣ
set "ProductName=Diary Assistant"
set "InternalName=yshtcn"
set "Comments=GitHub: https://github.com/yshtcn/DiaryAssistant_bot"
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>Ŀ¼
cd /d %~dp0
:: <20><><EFBFBD>±<EFBFBD><C2B1><EFBFBD>
title "AutoPyInstaller<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɰ汾<EFBFBD>ļ<EFBFBD>"
:: ʹ<><CAB9>WMIC<49><43>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
for /f "delims=" %%a in ('wmic os get localdatetime ^| find "."') do set datetime=%%a
:: <20>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
set "year=%datetime:~0,4%"
set "month=%datetime:~4,2%"
set "day=%datetime:~6,2%"
:: <20><>ȡ<EFBFBD><EFBFBD><E6B1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һλ
set /p "revision=<3D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>İ汾<C4B0><E6B1BE>:(%year%, %month%, %day%,[?]):"
:: <20><>ǰ<EFBFBD>汾Ŀ¼<C4BF><C2BC>δȥ<CEB4><C8A5><EFBFBD>ȵ<EFBFBD>0<EFBFBD><30>
set "versionFolder=%year%_%month%_%day%_%revision%"
:: ȥ<><C8A5><EFBFBD>º<EFBFBD><C2BA>յ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><E3A3A8><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>
set /a "month=1%month%-100"
set /a "day=1%day%-100"
:: <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD>
set "tempFile=temp.txt"
:: <20><><EFBFBD>ջ򴴽<D5BB><F2B4B4BD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD>
echo. > %tempFile%
:: <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD>
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%
:: <20><><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD> version_info.txt
move /Y %tempFile% version_info.txt
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
echo <20><EFBFBD><E6B1BE>Ϣ<EFBFBD>ѳɹ<D1B3><C9B9><EFBFBD><EFBFBD>ɡ<EFBFBD>
::<3A><><EFBFBD>±<EFBFBD><C2B1><EFBFBD>
title "AutoPyInstaller<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>
md build
:: ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC>ͬ<EFBFBD><EFBFBD>ŵ<EFBFBD><C5B5>ļ<EFBFBD><C4BC><EFBFBD>
rd /S /Q %~dp0\build\%versionFolder%
:: ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD>ļ<EFBFBD>
del /q %~dp0\build\heartbeat.spec
rd /S /Q %~dp0\build\build
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
cd /d %~dp0\build
pyinstaller --onefile --version-file %~dp0\version_info.txt "%~dp0\Diary Assistant.py"
::<3A><><EFBFBD>±<EFBFBD><C2B1><EFBFBD>
title "AutoPyInstaller<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һЩ<EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"
:: <20><><EFBFBD>ٴΣ<D9B4>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC>ͬ<EFBFBD><EFBFBD>ŵ<EFBFBD><C5B5>ļ<EFBFBD><C4BC><EFBFBD>
rd /S /Q %~dp0\build\%versionFolder%
:: <20><><EFBFBD>ٴΣ<D9B4>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD>ļ<EFBFBD>
del /q "%~dp0\build\Diary Assistant.spec"
rd /S /Q %~dp0\build\build
:: <20><><EFBFBD><EFBFBD><EFBFBD>ɴ<EFBFBD><C9B4><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF>԰汾<D4B0><E6B1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
rename dist %versionFolder%
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫһ<D2AA><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
copy %~dp0\config.Exsample.ini %~dp0\build\%versionFolder%\
copy %~dp0\README.md %~dp0\build\%versionFolder%\