- Add MDAuth class for authentication and session management - Add MDClient class for note operations (CRUD + export/search) - Add CLI tool mdclient_cli.py for quick operations - Support for CodiMD, HedgeDoc, and HackMD services - Multi-endpoint API compatibility Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
147 lines
5.2 KiB
Python
147 lines
5.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
MDclient CLI 工具
|
|
|
|
使用 MDclient Python 函式庫的命令列介面
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# 從 mdclient 套件匯入
|
|
from mdclient import MDClient, MDAuth
|
|
|
|
|
|
def load_env():
|
|
"""載入 .env 檔案"""
|
|
env_file = Path(".env")
|
|
if env_file.exists():
|
|
with open(env_file, 'r') as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if line and not line.startswith('#') and '=' in line:
|
|
key, value = line.split('=', 1)
|
|
os.environ[key.strip()] = value.strip()
|
|
|
|
|
|
def main():
|
|
"""主函數"""
|
|
import argparse
|
|
|
|
# 載入 .env 檔案
|
|
load_env()
|
|
|
|
parser = argparse.ArgumentParser(description="MDclient - Markdown 筆記服務命令列工具")
|
|
parser.add_argument('--url', default=os.getenv('MDCLIENT_URL', 'https://codimd.lotimmy.com'), help='伺服器位址')
|
|
parser.add_argument('--cookie-file', default=os.getenv('MDCLIENT_COOKIE_FILE', 'key.conf'), help='Cookie 檔案路徑')
|
|
|
|
subparsers = parser.add_subparsers(dest='command', help='可用指令')
|
|
|
|
# 登入指令
|
|
login_parser = subparsers.add_parser('login', help='登入並儲存 Cookie')
|
|
login_parser.add_argument('email', nargs='?', help='登入信箱(可從 .env 讀取)')
|
|
login_parser.add_argument('password', nargs='?', help='登入密碼(可從 .env 讀取)')
|
|
|
|
# 測試指令
|
|
subparsers.add_parser('test', help='測試 Cookie 是否有效')
|
|
|
|
# 使用者資訊指令
|
|
subparsers.add_parser('info', help='取得使用者資訊')
|
|
|
|
# 列出筆記指令
|
|
subparsers.add_parser('list', help='列出所有筆記')
|
|
|
|
# 取得筆記指令
|
|
get_parser = subparsers.add_parser('get', help='取得筆記內容')
|
|
get_parser.add_argument('note_id', help='筆記 ID')
|
|
|
|
# 建立筆記指令
|
|
create_parser = subparsers.add_parser('create', help='建立新筆記')
|
|
create_parser.add_argument('title', help='筆記標題')
|
|
create_parser.add_argument('--content', '-c', default='', help='筆記內容')
|
|
|
|
# 匯出筆記指令
|
|
export_parser = subparsers.add_parser('export', help='匯出筆記')
|
|
export_parser.add_argument('note_id', help='筆記 ID')
|
|
export_parser.add_argument('--format', '-f', default='md', choices=['md', 'pdf', 'html', 'slides'], help='匯出格式')
|
|
export_parser.add_argument('--output', '-o', help='輸出檔案路徑')
|
|
|
|
args = parser.parse_args()
|
|
|
|
if not args.command:
|
|
parser.print_help()
|
|
sys.exit(1)
|
|
|
|
# 執行指令
|
|
if args.command == 'login':
|
|
# 從參數或環境變數取得帳密
|
|
email = args.email or os.getenv('MDCLIENT_EMAIL')
|
|
password = args.password or os.getenv('MDCLIENT_PASSWORD')
|
|
|
|
if not email or not password:
|
|
print("✗ 請提供帳密,或設定 .env 檔案中的 MDCLIENT_EMAIL 和 MDCLIENT_PASSWORD")
|
|
sys.exit(1)
|
|
|
|
auth = MDAuth(base_url=args.url, cookie_file=args.cookie_file)
|
|
success = auth.login(email, password)
|
|
if success:
|
|
print("\n✓ 登入成功!")
|
|
auth.test_cookie()
|
|
else:
|
|
print("\n✗ 登入失敗")
|
|
sys.exit(1)
|
|
|
|
elif args.command == 'test':
|
|
auth = MDAuth(base_url=args.url, cookie_file=args.cookie_file)
|
|
auth.test_cookie()
|
|
|
|
elif args.command == 'info':
|
|
client = MDClient(base_url=args.url, cookie_file=args.cookie_file)
|
|
info = client.get_user_info()
|
|
if info:
|
|
print("\n使用者資訊:")
|
|
print(f" 使用者名稱: {info.get('username', 'N/A')}")
|
|
print(f" 電子信箱: {info.get('email', 'N/A')}")
|
|
|
|
elif args.command == 'list':
|
|
client = MDClient(base_url=args.url, cookie_file=args.cookie_file)
|
|
notes = client.list_notes()
|
|
if notes:
|
|
for i, note in enumerate(notes, 1):
|
|
title = note.get('title') or note.get('text') or note.get('alias') or note.get('id', '無標題')
|
|
note_id = note.get('id', '')
|
|
pinned = " 📌" if note.get('pinned') else ""
|
|
print(f"{i}. {title}{pinned} (ID: {note_id})")
|
|
|
|
elif args.command == 'get':
|
|
client = MDClient(base_url=args.url, cookie_file=args.cookie_file)
|
|
note = client.get_note(args.note_id)
|
|
if note:
|
|
content = note.get('content', '')
|
|
print(content)
|
|
|
|
elif args.command == 'create':
|
|
client = MDClient(base_url=args.url, cookie_file=args.cookie_file)
|
|
result = client.create_note(args.title, args.content or "")
|
|
if result:
|
|
note_id = result.get('id') or result.get('alias', '')
|
|
url = result.get('url', f"{args.url}/{note_id}")
|
|
print(f"✓ 筆記建立成功!")
|
|
print(f" ID: {note_id}")
|
|
print(f" 網址: {url}")
|
|
|
|
elif args.command == 'export':
|
|
client = MDClient(base_url=args.url, cookie_file=args.cookie_file)
|
|
content = client.export_note(args.note_id, args.format)
|
|
if content and args.output:
|
|
with open(args.output, 'w', encoding='utf-8') as f:
|
|
f.write(content)
|
|
print(f"✓ 已匯出到: {args.output}")
|
|
elif content:
|
|
print(content)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|