Feature/cookie login 修正程序输出,简化手动导入Cookies流程为直接粘贴#8
Conversation
|
Caution Review failedThe pull request is closed. Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. 📝 WalkthroughWalkthroughAuthentication shifts from file-based cookie handling to clipboard-driven and interactive SSO login; config adds TEST_LOGIN_MODE and SSO_INDEX_URL; main app flow and UX were restructured; cookie helper now reads pasted JSON from stdin; docs updated to remove file-based cookie instructions. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant User as User (CLI)
participant Main as main.py
participant Auth as automation/auth.py
participant Browser as Browser/Playwright
participant SSO as SSO Site
Note over User,Main: Startup
User->>Main: run program
Main->>Main: print_welcome()
Main->>Auth: check cookie file exists?
alt cookie file exists and valid
Auth->>Browser: load cookies
Browser-->>Auth: cookies loaded
Auth-->>Main: authenticated
else no valid cookies
Main->>User: prompt for login method (interactive / paste cookies)
alt interactive selected
Main->>Browser: launch browser (headful)
Browser-->>User: show SSO login UI
User->>SSO: completes SSO login manually
SSO-->>Browser: authenticated session
Browser->>Auth: page shows "砺儒云课堂" link
Auth->>Browser: click link, navigate to SSO index
Browser->>Auth: extract cookies
Auth->>Auth: save cookies to file
Auth-->>Main: authenticated
else paste cookies selected
User->>Main: paste clipboard JSON
Main->>Auth: parse & import cookies
Auth->>Browser: set cookies
Auth-->>Main: authenticated
end
end
Main->>Browser: proceed with scraping actions
Main->>Browser: close if connected
Note right of User: End
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (4)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @YewFence, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在显著提升SCNU砺儒云视频自动观看工具的用户登录体验。核心改进在于简化了Cookie的导入过程,允许用户直接粘贴浏览器导出的JSON格式Cookie,从而省去了文件操作的步骤。此外,登录流程变得更加智能和用户友好,程序会优先尝试现有凭证,并在必要时提供清晰的引导。此次更新还包括了新的测试配置选项、更简洁的控制台输出以及更健壮的错误处理机制,以提高整体的可用性和可维护性。 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
automation/auth.py
Outdated
| 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: |
There was a problem hiding this comment.
硬编码的 sso_index_url 默认值降低了代码的可维护性。建议将此 URL 移至 config.py 文件中,与其他 URL 配置放在一起。这样可以更方便地进行管理和修改,也符合将配置与逻辑分离的最佳实践。
建议修改如下:
config.py
# ...
LOGIN_URL = "https://sso.scnu.edu.cn/AccountService/user/login.html"
SSO_INDEX_URL = "https://sso.scnu.edu.cn/AccountService/user/index.html" # 新增
# ...automation/auth.py
async def interactive_login_and_save_cookies(self,
login_url: str,
base_url: str,
cookie_file: str = "cookies.json",
sso_index_url: str) -> bool: # 移除默认值
# ...main.py
# ...
login_success = await auth_manager.interactive_login_and_save_cookies(
config.LOGIN_URL,
config.BASE_URL,
config.COOKIE_FILE,
config.SSO_INDEX_URL # 新增
)
# ...
automation/auth.py
Outdated
| 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'): |
There was a problem hiding this comment.
cookie_fix.py
Outdated
| @@ -1,15 +1,18 @@ | |||
| # convert_cookies.py | |||
| import json | |||
| import sys | |||
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (5)
cookie_fix.py (2)
3-3: Remove unusedsysimport.The
sysmodule is imported but not used. Theinput()function is a Python builtin, not fromsys.🔎 Proposed fix
# convert_cookies.py import json -import sys
12-15: Consider more specific error handling for JSON parsing.The generic
Exceptionhandler at line 52 catches JSON parsing errors, but providing a more specific error message for malformed JSON would improve user experience.🔎 Proposed fix
if content == '': print("✗ 输入为空,请检查输入内容") return False - browser_cookies = json.loads(content) + try: + browser_cookies = json.loads(content) + except json.JSONDecodeError as e: + print(f"✗ JSON 格式错误: {e}") + return Falseautomation/auth.py (2)
132-136: Add type annotation forsso_index_urlparameter.For consistency with other parameters, add the
strtype annotation.🔎 Proposed fix
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: + sso_index_url: str = "https://sso.scnu.edu.cn/AccountService/user/index.html") -> bool:
180-181: Simplify retry condition check.After calling
.lower(), checking uppercase variants is redundant.🔎 Proposed fix
- if retry.lower() not in ('y', 'yes', 'Y', 'Yes', 'YES'): + if retry.lower() not in ('y', 'yes'): return Falsemain.py (1)
76-76: Redundant assignment.
login_successis alreadyFalseat this point (set on line 58 and not modified if we reach line 69).🔎 Proposed fix
print("登录凭证已失效或不存在") # 选择登录方式 print("\n🔐 请选择获取登录凭证(Cookies)的方式:") print(" 1. 交互式登录(推荐)- 自动打开登录页面,您手动登录后程序自动获取Cookies") print(" 2. 使用您手动获取的 Cookies 登录 - 在命令行中直接粘贴浏览器导出的 Cookies JSON") - login_success = False while True:
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
README.mdautomation/auth.pyconfig.pycookie_fix.pydocs/how_to_get_cookie.mdmain.py
🧰 Additional context used
🪛 Ruff (0.14.10)
main.py
59-59: Comment contains ambiguous , (FULLWIDTH COMMA). Did you mean , (COMMA)?
(RUF003)
62-62: Comment contains ambiguous , (FULLWIDTH COMMA). Did you mean , (COMMA)?
(RUF003)
64-64: String contains ambiguous , (FULLWIDTH COMMA). Did you mean , (COMMA)?
(RUF001)
72-72: String contains ambiguous ( (FULLWIDTH LEFT PARENTHESIS). Did you mean ( (LEFT PARENTHESIS)?
(RUF001)
72-72: String contains ambiguous ) (FULLWIDTH RIGHT PARENTHESIS). Did you mean ) (RIGHT PARENTHESIS)?
(RUF001)
73-73: String contains ambiguous ( (FULLWIDTH LEFT PARENTHESIS). Did you mean ( (LEFT PARENTHESIS)?
(RUF001)
73-73: String contains ambiguous ) (FULLWIDTH RIGHT PARENTHESIS). Did you mean ) (RIGHT PARENTHESIS)?
(RUF001)
73-73: String contains ambiguous , (FULLWIDTH COMMA). Did you mean , (COMMA)?
(RUF001)
80-80: String contains ambiguous , (FULLWIDTH COMMA). Did you mean , (COMMA)?
(RUF001)
100-100: String contains ambiguous , (FULLWIDTH COMMA). Did you mean , (COMMA)?
(RUF001)
100-100: String contains ambiguous , (FULLWIDTH COMMA). Did you mean , (COMMA)?
(RUF001)
103-103: String contains ambiguous , (FULLWIDTH COMMA). Did you mean , (COMMA)?
(RUF001)
133-133: Do not catch blind exception: Exception
(BLE001)
146-148: try-except-pass detected, consider logging the exception
(S110)
146-146: Do not catch blind exception: Exception
(BLE001)
147-147: Comment contains ambiguous , (FULLWIDTH COMMA). Did you mean , (COMMA)?
(RUF003)
156-156: String contains ambiguous , (FULLWIDTH COMMA). Did you mean , (COMMA)?
(RUF001)
156-156: String contains ambiguous : (FULLWIDTH COLON). Did you mean : (COLON)?
(RUF001)
automation/auth.py
169-169: Do not catch blind exception: Exception
(BLE001)
174-174: String contains ambiguous ! (FULLWIDTH EXCLAMATION MARK). Did you mean ! (EXCLAMATION MARK)?
(RUF001)
177-177: String contains ambiguous ! (FULLWIDTH EXCLAMATION MARK). Did you mean ! (EXCLAMATION MARK)?
(RUF001)
179-179: String contains ambiguous ? (FULLWIDTH QUESTION MARK). Did you mean ? (QUESTION MARK)?
(RUF001)
config.py
21-21: Comment contains ambiguous ( (FULLWIDTH LEFT PARENTHESIS). Did you mean ( (LEFT PARENTHESIS)?
(RUF003)
21-21: Comment contains ambiguous ) (FULLWIDTH RIGHT PARENTHESIS). Did you mean ) (RIGHT PARENTHESIS)?
(RUF003)
22-22: Comment contains ambiguous ( (FULLWIDTH LEFT PARENTHESIS). Did you mean ( (LEFT PARENTHESIS)?
(RUF003)
22-22: Comment contains ambiguous ) (FULLWIDTH RIGHT PARENTHESIS). Did you mean ) (RIGHT PARENTHESIS)?
(RUF003)
cookie_fix.py
13-13: String contains ambiguous , (FULLWIDTH COMMA). Did you mean , (COMMA)?
(RUF001)
🔇 Additional comments (7)
README.md (1)
113-124: Documentation updates look good.The instructions clearly reflect the new clipboard-based cookie workflow and are consistent with the code changes in
main.pyandcookie_fix.py.automation/auth.py (1)
150-170: SSO navigation flow looks reasonable.The flow navigates to SSO, finds the target link, handles popup, and waits for load. The broad exception catch at line 169 is acceptable here to prevent login flow from failing on transient UI issues.
main.py (4)
14-32: Welcome banner looks good.The ASCII art banner provides a clean user experience with helpful tips about usage and interruption.
59-68: Auto-login flow is well-structured.Good use of
TEST_LOGIN_MODEflag to conditionally skip cookie-based login for testing purposes. The path existence check prevents unnecessary login attempts.
139-148: Browser shutdown handling is appropriate.The guarded shutdown correctly checks
is_connected()before attempting close, and the silent exception handling is reasonable for cleanup code where the browser may already be closed.
150-156:suggestions()function provides helpful troubleshooting guidance.Consider adding a return type annotation for consistency.
🔎 Optional: Add return type annotation
-def suggestions(): +def suggestions() -> None: print("\n💡 故障排查建议:")docs/how_to_get_cookie.md (1)
16-16: Documentation correctly updated for clipboard workflow.The simplified instructions now accurately reflect that cookies are copied to clipboard after export, aligning with the new paste-based input in the program.
Summary by CodeRabbit
Documentation
New Features
Improvements
✏️ Tip: You can customize this high-level summary in your review settings.