Skip to content

Commit

Permalink
feat: 打包成单个文件时候的持久化 #424
Browse files Browse the repository at this point in the history
  • Loading branch information
mikumifa committed Sep 17, 2024
1 parent 08b063f commit 24862f1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 27 deletions.
21 changes: 13 additions & 8 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import os
import sys
import time

import loguru
import ntplib

from util.BiliRequest import BiliRequest
from util.KVDatabase import KVDatabase
Expand All @@ -15,24 +13,31 @@
# 获取图标文件的路径
def get_application_path():
if getattr(sys, "frozen", False):
application_path = sys._MEIPASS
application_path = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
else:
application_path = os.path.dirname(os.path.abspath(__file__))

return application_path


APP_PATH = get_application_path()


def get_application_tmp_path():
os.makedirs(os.path.join(get_application_path(), "tmp"), exist_ok=True)
return os.path.join(get_application_path(), "tmp")
os.makedirs(os.path.join(APP_PATH, "tmp"), exist_ok=True)
return os.path.join(APP_PATH, "tmp")


TEMP_PATH = get_application_tmp_path()
BASE_DIR = os.path.dirname(os.path.realpath(sys.executable))

configDB = KVDatabase(os.path.join(get_application_tmp_path(), "config.json"))
loguru.logger.info(f"设置路径, APP_PATH={APP_PATH} TEMP_PATH={TEMP_PATH} BASE_DIR={BASE_DIR}")
configDB = KVDatabase(os.path.join(BASE_DIR, "config.json"))
if not configDB.contains("cookie_path"):
configDB.insert("cookie_path", os.path.join(get_application_tmp_path(), "cookies.json"))
configDB.insert("cookie_path", os.path.join(BASE_DIR, "cookies.json"))
main_request = BiliRequest(cookies_config_path=configDB.get("cookie_path"))
global_cookieManager = main_request.cookieManager

## 时间
time_service = TimeService()
time_service.set_timeoffset(time_service.compute_timeoffset())
time_service.set_timeoffset(time_service.compute_timeoffset())
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import gradio as gr
from loguru import logger

from config import get_application_path
from config import get_application_path, BASE_DIR
from tab.go import go_tab
from tab.login import login_tab
from tab.problems import problems_tab
Expand Down Expand Up @@ -35,7 +35,7 @@
parser.add_argument("--port", type=int, default=7860, help="server port")
parser.add_argument("--share", type=bool, default=False, help="create a public link")
args = parser.parse_args()
log_file = os.path.join(get_application_path(), "app.log")
log_file = os.path.join(BASE_DIR, "app.log")
logger.add(log_file)
with gr.Blocks(head=short_js, css=custom_css) as demo:
gr.Markdown(header)
Expand Down
4 changes: 2 additions & 2 deletions tab/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from gradio_calendar import Calendar
from loguru import logger

from config import main_request, get_application_tmp_path
from config import main_request, TEMP_PATH

buyer_value = []
addr_value = []
Expand Down Expand Up @@ -208,7 +208,7 @@ def on_submit_all(ticket_id, ticket_info, people_indices, people_buyer_index, ad
+ address_cur["addr"],
},
}
filename = os.path.join(get_application_tmp_path(), filename_filter(detail) + ".json")
filename = os.path.join(TEMP_PATH, filename_filter(detail) + ".json")
with open(filename, 'w', encoding='utf-8') as f:
json.dump(config_dir, f, ensure_ascii=False, indent=4)
return [gr.update(), gr.update(value=config_dir, visible=True), gr.update(value=filename, visible=True)]
Expand Down
1 change: 0 additions & 1 deletion util/CookieManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def _login_and_save_cookies(
browser.close()
logger.info("登录成功, 浏览器退出.")
return self.db.get("cookie")

except Exception as e:
logger.error(f"登录失败: {e}")
raise
Expand Down
11 changes: 4 additions & 7 deletions util/PushPlusUtil.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import json
import os

import loguru
import requests
import playsound
import os
import requests

from config import get_application_tmp_path
from config import TEMP_PATH


def send_message(token, content, title):
Expand All @@ -25,8 +25,5 @@ def send_message(token, content, title):
loguru.logger.info("PushPlus消息发送失败")





if __name__ == '__main__':
playsound.playsound(os.path.join(get_application_tmp_path(), "default.mp3"))
playsound.playsound(os.path.join(TEMP_PATH, "default.mp3"))
11 changes: 4 additions & 7 deletions util/ServerChanUtil.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import json
import os

import loguru
import requests
import playsound
import os
import requests

from config import get_application_tmp_path
from config import TEMP_PATH


def send_message(token, desp, title):
Expand All @@ -24,8 +24,5 @@ def send_message(token, desp, title):
loguru.logger.info("ServerChan消息发送失败")





if __name__ == '__main__':
playsound.playsound(os.path.join(get_application_tmp_path(), "default.mp3"))
playsound.playsound(os.path.join(TEMP_PATH, "default.mp3"))

0 comments on commit 24862f1

Please sign in to comment.