feat(07-03): implement login CLI subcommand

- Add login_command function with QR code login, cookie import, and status check
- Add login subcommand with --import-cookie and --status options
- Update CLI help text with login examples
- Fix mock paths in tests to match actual import locations

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-17 18:10:43 +08:00
parent 94c85baac9
commit d9861aa6c3
2 changed files with 109 additions and 18 deletions
+88
View File
@@ -11,6 +11,9 @@ KB Workflow - Knowledge Base Workflow Orchestrator
使用方式:
python kb_workflow.py process "https://www.bilibili.com/video/BVxxx"
python kb_workflow.py process "https://..." -o /path/to/output
python kb_workflow.py login # QR code login
python kb_workflow.py login --import-cookie cookies.json
python kb_workflow.py login --status
"""
import argparse
@@ -102,6 +105,68 @@ def process_command(
}
def login_command(
import_cookie: Optional[str] = None,
status: bool = False
) -> Dict[str, Any]:
"""Execute login command
Args:
import_cookie: Path to JSON cookie file
status: Check current cookie status
Returns:
Dictionary with login result
"""
try:
from kb_workflow.auth import ZhihuLogin
from kb_workflow.auth.cookie_manager import CookieManager, parse_json_cookies
from kb_workflow.pipeline.config import get_settings
settings = get_settings()
manager = CookieManager()
# Check Cookie status
if status:
current_cookie = settings.zhihu_cookie.get_secret_value()
if not current_cookie:
return {"valid": False, "reason": "No cookie found in ~/.kb_env"}
async def check_status():
login = ZhihuLogin(intermediate_path=settings.intermediate_path)
try:
login.browser = login.browser or type('obj', (object,), {
'browser': None,
'page': None,
'start': lambda cookie=None: None,
'close': lambda: None
})()
login.browser.start(cookie=current_cookie)
return await login.validate_cookie(current_cookie)
finally:
if login.browser and hasattr(login.browser, 'close'):
login.browser.close()
result = asyncio.run(check_status())
return result.model_dump()
# Import Cookie
if import_cookie:
cookie_str = parse_json_cookies(import_cookie)
manager.save_cookie(cookie_str)
return {"success": True, "message": f"Cookie imported from {import_cookie}"}
# QR code login (synchronous method)
login = ZhihuLogin(intermediate_path=settings.intermediate_path)
result = login.login_by_qrcode()
if result.success and result.cookie_str:
manager.save_cookie(result.cookie_str)
return result.model_dump()
except Exception as e:
return {"success": False, "error": str(e)}
def main():
"""CLI入口 - 用于命令行调用"""
parser = argparse.ArgumentParser(
@@ -117,6 +182,15 @@ def main():
# 强制重新执行(忽略已完成状态)
python kb_workflow.py process "https://..." --no-skip
# 知乎登录 (二维码扫码)
python kb_workflow.py login
# 导入 Cookie
python kb_workflow.py login --import-cookie cookies.json
# 检查 Cookie 状态
python kb_workflow.py login --status
'''
)
@@ -132,6 +206,13 @@ def main():
process_parser.add_argument('--no-skip', action='store_false', dest='skip_completed',
help='Re-run all steps')
# Login subcommand
login_parser = subparsers.add_parser('login', help='Zhihu login authentication')
login_parser.add_argument('--import-cookie', '-i', type=str,
help='Import cookie from JSON file')
login_parser.add_argument('--status', action='store_true',
help='Check current cookie status')
args = parser.parse_args()
# Handle process subcommand
@@ -142,6 +223,13 @@ def main():
skip_completed=args.skip_completed
)
print(json.dumps(result, ensure_ascii=False, indent=2))
# Handle login subcommand
elif args.command == 'login':
result = login_command(
import_cookie=args.import_cookie,
status=args.status
)
print(json.dumps(result, ensure_ascii=False, indent=2))
else:
parser.print_help()