-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/cookie login 修正程序输出,简化手动导入Cookies流程为直接粘贴 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b91da95
chore: 美观化程序输出
YewFence 125398e
feat: 更改手动获取的 Cookies 的读取方式为直接粘贴至程序内
YewFence dff0e2d
docs: 说明新版本下如何导入Cookies
YewFence dc28646
feat: 增加尝试直接使用已有Cookies登录的功能
YewFence 0956ed4
test: 增加登录测试模式
YewFence d195854
fix: 修复无法结束粘贴输入的问题
YewFence e627aec
feat: 解耦SSO主页URL配置
YewFence 87dc6ee
refactor: 删除不必要的逻辑
YewFence File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,7 @@ async def refresh_cookies(self, cookie_file: str = "cookies.json"): | |
| await refresh_button.click() | ||
| await asyncio.sleep(1) # 等待cookie更新 | ||
| await self.save_cookies(cookie_file) | ||
| await self.load_cookies(cookie_file) | ||
|
|
||
| async def check_cookie_validity(self) -> bool: | ||
| """ | ||
|
|
@@ -77,8 +78,7 @@ async def check_cookie_validity(self) -> bool: | |
| try: | ||
| page_content = await self.page.content() | ||
| if "访客不能访问此课程" in page_content: | ||
| print("❌ 检测到Cookie已失效!页面显示: 访客不能访问此课程") | ||
| print("💡 请重新导出browser_cookies.json并运行脚本") | ||
| print("❌ 检测到Cookie已失效") | ||
| return False | ||
| return True | ||
| except Exception as e: | ||
|
|
@@ -97,12 +97,6 @@ async def login_with_cookies(self, base_url: str, cookie_file: str = "cookies.js | |
| # 加载Cookie | ||
| if not await self.load_cookies(cookie_file): | ||
| print("\n❌ Cookie加载失败!") | ||
| print("💡 请按以下步骤手动获取Cookie:") | ||
| print(" 1. 在浏览器中登录网站") | ||
| print(" 2. 按F12打开开发者工具 -> Application -> Cookies") | ||
| print(" 3. 复制所有Cookie并保存为 cookies.json") | ||
| print(" 4. 或使用浏览器扩展导出Cookie(推荐)") | ||
| print("\n详细说明请查看: how_to_get_cookie.md") | ||
| return False | ||
| # 检查登录状态 | ||
| return await self.check_login_status(base_url) | ||
|
|
@@ -135,7 +129,11 @@ async def check_login_status(self, base_url: str) -> bool: | |
| print(f"✓ Cookie登录成功,当前页面: {self.page.url}") | ||
| return True | ||
|
|
||
| async def interactive_login_and_save_cookies(self, login_url: str, base_url: str, cookie_file: str = "cookies.json") -> bool: | ||
| async def interactive_login_and_save_cookies(self, | ||
| login_url: str, | ||
| base_url: str, | ||
| cookie_file: str = "cookies.json", | ||
| sso_index_url = "https://sso.scnu.edu.cn/AccountService/user/index.html") -> bool: | ||
| """ | ||
| 交互式登录:打开登录页面,等待用户手动登录,然后保存Cookie | ||
| :param login_url: 登录页面URL | ||
|
|
@@ -149,6 +147,8 @@ async def interactive_login_and_save_cookies(self, login_url: str, base_url: str | |
| print(f"✅ 登录页面已打开: {login_url}") | ||
| print("📝 请在浏览器中完成登录操作") | ||
| await asyncio.get_running_loop().run_in_executor(None, input, "🔑 登录完成后,请按回车键继续...") | ||
| # 先前往 SSO 主页 | ||
| await self.page.goto(sso_index_url) | ||
| print("🔍 尝试获取cookie...") | ||
| try: | ||
| # 查找文本为"砺儒云课堂"的a标签 | ||
|
|
@@ -165,20 +165,19 @@ async def interactive_login_and_save_cookies(self, login_url: str, base_url: str | |
| await moodle_page.wait_for_load_state() | ||
| print("✅ 成功跳转到目标页面") | ||
| else: | ||
| print("⚠️ 未找到'lry课堂'链接,继续执行后续操作") | ||
| print("⚠️ 未找到'砺儒云课堂'链接") | ||
| except Exception as e: | ||
| print(f"⚠️ 点击'lry课堂'链接时出错: {e}") | ||
| print("继续执行后续操作...") | ||
| print(f"⚠️ 点击'砺儒云课堂'链接时出错: {e}") | ||
| # 验证Cookie是否有效 | ||
| print("🔍 验证登录状态...") | ||
| if await self.check_login_status(base_url): | ||
| print("✅ 登录验证成功!") | ||
| else: | ||
| while not await self.check_login_status(base_url): | ||
| print("❌ 登录验证失败!请确认您已完成登录") | ||
| print("❌ 登录验证失败!") | ||
| loop = asyncio.get_running_loop() | ||
| retry = await loop.run_in_executor(None, input, "是否重试?(y/n): ") | ||
| if retry.lower() not in ('y', 'yes'): | ||
| if retry.lower() not in ('y', 'yes', 'Y', 'Yes', 'YES'): | ||
|
||
| return False | ||
| print("✅ 登录验证成功!") | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,18 @@ | ||
| # convert_cookies.py | ||
| import json | ||
| import sys | ||
|
||
|
|
||
| def cookie_fix(file_name: str = 'browser_cookies.json'): | ||
| def cookie_fix(): | ||
| try: | ||
| # 读取浏览器导出的Cookie | ||
| with open(file_name, 'r', encoding='utf-8') as f: | ||
| content = f.read().strip() | ||
| if content == '': | ||
| print("✗ Cookie文件为空,请检查文件内容") | ||
| return False | ||
| browser_cookies = json.loads(content) | ||
| # 从CLI读取浏览器导出的Cookie | ||
| print("请粘贴浏览器导出的Cookie JSON (连续敲击两次回车(Enter)结束输入):") | ||
| lines = list(iter(input, '')) | ||
| content = '\n'.join(lines) | ||
|
|
||
| if content == '': | ||
| print("✗ 输入为空,请检查输入内容") | ||
| return False | ||
| browser_cookies = json.loads(content) | ||
|
|
||
| # 转换为Playwright格式 | ||
| playwright_cookies = [] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
硬编码的
sso_index_url默认值降低了代码的可维护性。建议将此 URL 移至config.py文件中,与其他 URL 配置放在一起。这样可以更方便地进行管理和修改,也符合将配置与逻辑分离的最佳实践。建议修改如下:
config.pyautomation/auth.pymain.py