diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..d505248 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +DOMAIN='xxxxx.me' +TEMP_MAIL='xxxxxx' \ No newline at end of file diff --git a/.gitignore b/.gitignore index 180f04b..c70b215 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ __pycache__/ venv/ node_modules/ + +.env diff --git a/browser_utils.py b/browser_utils.py index 90d42c2..1aaa04d 100644 --- a/browser_utils.py +++ b/browser_utils.py @@ -29,7 +29,7 @@ class BrowserManager: co.set_pref("credentials_enable_service", False) co.set_argument("--hide-crash-restore-bubble") co.auto_port() - co.headless(True) # 生产环境使用无头模式 + # co.headless(True) # 生产环境使用无头模式 # Mac 系统特殊处理 if sys.platform == "darwin": diff --git a/build.py b/build.py index aa3b273..bee967e 100644 --- a/build.py +++ b/build.py @@ -162,6 +162,14 @@ def build(): else: subprocess.run(["cp", "config.ini.example", f"{output_dir}/config.ini"]) + # Copy .env.example file + if os.path.exists(".env.example"): + simulate_progress("Copying environment file...", 0.5) + if system == "windows": + subprocess.run(["copy", ".env.example", f"{output_dir}\\.env"], shell=True) + else: + subprocess.run(["cp", ".env.example", f"{output_dir}/.env"]) + print( f"\n\033[92mBuild completed successfully! Output directory: {output_dir}\033[0m" ) diff --git a/config.py b/config.py new file mode 100644 index 0000000..f1201f0 --- /dev/null +++ b/config.py @@ -0,0 +1,37 @@ +from dotenv import load_dotenv +import os +import sys + + +class Config: + def __init__(self): + # 获取应用程序的根目录路径 + if getattr(sys, "frozen", False): + # 如果是打包后的可执行文件 + application_path = os.path.dirname(sys.executable) + else: + # 如果是开发环境 + application_path = os.path.dirname(os.path.abspath(__file__)) + + # 指定 .env 文件的路径 + dotenv_path = os.path.join(application_path, ".env") + + # 加载 .env 文件 + load_dotenv(dotenv_path) + + def get_temp_mail(self): + return os.getenv("TEMP_MAIL") + + def get_domain(self): + return os.getenv("DOMAIN") + + +# 使用示例 +if __name__ == "__main__": + try: + config = Config() + print("环境变量加载成功!") + print(f"临时邮箱: {config.get_temp_mail()}") + print(f"域名: {config.get_domain()}") + except ValueError as e: + print(f"错误: {e}") diff --git a/cursor_pro_keep_alive.py b/cursor_pro_keep_alive.py index fddec56..6d4c108 100644 --- a/cursor_pro_keep_alive.py +++ b/cursor_pro_keep_alive.py @@ -14,7 +14,7 @@ from logger import logging from browser_utils import BrowserManager from get_email_code import EmailVerificationHandler from logo import print_logo -import psutil +from config import Config def handle_turnstile(tab): @@ -191,7 +191,6 @@ def sign_up_account(browser, tab): class EmailGenerator: def __init__( self, - domain="mailto.plus", password="".join( random.choices( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*", @@ -201,7 +200,7 @@ class EmailGenerator: first_name="yuyan", last_name="peng", ): - self.domain = domain + self.domain = Config().get_domain() self.default_password = password self.default_first_name = first_name self.default_last_name = last_name diff --git a/get_email_code.py b/get_email_code.py index b019510..673b492 100644 --- a/get_email_code.py +++ b/get_email_code.py @@ -1,15 +1,16 @@ from DrissionPage.common import Keys import time import re +from config import Config class EmailVerificationHandler: def __init__(self, browser, mail_url="https://tempmail.plus"): self.browser = browser self.mail_url = mail_url + self.username = Config().get_temp_mail() - def get_verification_code(self, email): - username = email.split("@")[0] + def get_verification_code(self): code = None try: @@ -19,7 +20,7 @@ class EmailVerificationHandler: self.browser.activate_tab(tab_mail) # 输入用户名 - self._input_username(tab_mail, username) + self._input_username(tab_mail) # 等待并获取最新邮件 code = self._get_latest_mail_code(tab_mail) @@ -35,14 +36,14 @@ class EmailVerificationHandler: return code - def _input_username(self, tab, username): + def _input_username(self, tab): while True: if tab.ele("@id=pre_button"): tab.actions.click("@id=pre_button") time.sleep(0.5) tab.run_js('document.getElementById("pre_button").value = ""') time.sleep(0.5) - tab.actions.input(username).key_down(Keys.ENTER).key_up(Keys.ENTER) + tab.actions.input(self.username).key_down(Keys.ENTER).key_up(Keys.ENTER) break time.sleep(1) diff --git a/requirements.txt b/requirements.txt index 2c3a154..fd438e6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ DrissionPage==4.1.0.9 colorama==0.4.6 +python-dotenv==1.0.0