Initial commit: Python library for Markdown note services
- 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>
This commit is contained in:
173
auth_example.py
Normal file
173
auth_example.py
Normal file
@@ -0,0 +1,173 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
MDclient 認證範例
|
||||
|
||||
展示如何進行認證和登入
|
||||
"""
|
||||
|
||||
from mdclient import MDAuth, MDClient
|
||||
import os
|
||||
|
||||
def basic_login_example():
|
||||
"""基本登入範例"""
|
||||
print("=== 基本登入範例 ===\n")
|
||||
|
||||
# 建立認證物件
|
||||
auth = MDAuth(
|
||||
base_url="https://codimd.lotimmy.com",
|
||||
cookie_file="key.conf"
|
||||
)
|
||||
|
||||
# 登入
|
||||
email = "your@email.com"
|
||||
password = "your_password"
|
||||
success = auth.login(email, password)
|
||||
|
||||
if success:
|
||||
print("✓ 登入成功!")
|
||||
|
||||
# 測試 Cookie
|
||||
is_valid = auth.test_cookie()
|
||||
print(f"Cookie 有效: {is_valid}")
|
||||
else:
|
||||
print("✗ 登入失敗")
|
||||
|
||||
def env_login_example():
|
||||
"""使用環境變數登入"""
|
||||
print("\n=== 使用環境變數登入 ===\n")
|
||||
|
||||
# 從環境變數讀取
|
||||
email = os.getenv('MDCLIENT_EMAIL')
|
||||
password = os.getenv('MDCLIENT_PASSWORD')
|
||||
url = os.getenv('MDCLIENT_URL', 'https://codimd.lotimmy.com')
|
||||
|
||||
if email and password:
|
||||
auth = MDAuth(base_url=url)
|
||||
success = auth.login(email, password)
|
||||
|
||||
if success:
|
||||
print("✓ 使用環境變數登入成功")
|
||||
|
||||
# 立即可用客戶端
|
||||
client = MDClient(base_url=url)
|
||||
notes = client.list_notes()
|
||||
print(f"找到 {len(notes)} 筆筆記")
|
||||
else:
|
||||
print("✗ 環境變數未設定")
|
||||
print("請在 .env 檔案中設定:")
|
||||
print(" MDCLIENT_EMAIL=your@email.com")
|
||||
print(" MDCLIENT_PASSWORD=your_password")
|
||||
|
||||
def load_existing_session():
|
||||
"""載入已儲存的會話"""
|
||||
print("\n=== 載入已儲存的會話 ===\n")
|
||||
|
||||
auth = MDAuth()
|
||||
|
||||
# 檢查是否已有 Cookie
|
||||
cookies = auth.load_cookie()
|
||||
if cookies:
|
||||
print("✓ 找到已儲存的 Cookie")
|
||||
|
||||
# 測試是否仍然有效
|
||||
is_valid = auth.test_cookie()
|
||||
if is_valid:
|
||||
print("✓ Cookie 仍然有效,可以直接使用")
|
||||
|
||||
# 建立客戶端
|
||||
client = MDClient()
|
||||
notes = client.list_notes()
|
||||
print(f"找到 {len(notes)} 筆筆記")
|
||||
else:
|
||||
print("✗ Cookie 已過期,需要重新登入")
|
||||
else:
|
||||
print("✗ 找不到已儲存的 Cookie,請先登入")
|
||||
|
||||
def custom_server_example():
|
||||
"""連接到自訂伺服器"""
|
||||
print("\n=== 連接到自訂伺服器 ===\n")
|
||||
|
||||
# 連接到不同的伺服器
|
||||
auth = MDAuth(
|
||||
base_url="https://demo.hedgedoc.org",
|
||||
cookie_file="hedgedoc_cookies.conf"
|
||||
)
|
||||
|
||||
print("提示:取消註解以下程式碼來登入")
|
||||
# auth.login("user@example.com", "password")
|
||||
|
||||
# client = MDClient(
|
||||
# base_url="https://demo.hedgedoc.org",
|
||||
# cookie_file="hedgedoc_cookies.conf"
|
||||
# )
|
||||
|
||||
def quick_start():
|
||||
"""快速開始指南"""
|
||||
print("=== 快速開始 ===\n")
|
||||
print("1. 基本使用:")
|
||||
print("""
|
||||
from mdclient import MDAuth, MDClient
|
||||
|
||||
# 登入
|
||||
auth = MDAuth()
|
||||
auth.login("your@email.com", "password")
|
||||
|
||||
# 使用客戶端
|
||||
client = MDClient()
|
||||
notes = client.list_notes()
|
||||
""")
|
||||
|
||||
print("\n2. 使用 .env 檔案:")
|
||||
print("""
|
||||
# 建立 .env 檔案
|
||||
echo "MDCLIENT_EMAIL=your@email.com" > .env
|
||||
echo "MDCLIENT_PASSWORD=your_password" >> .env
|
||||
|
||||
# 在程式碼中使用
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from mdclient import MDAuth, MDClient
|
||||
|
||||
load_dotenv()
|
||||
auth = MDAuth()
|
||||
auth.login(os.getenv('MDCLIENT_EMAIL'), os.getenv('MDCLIENT_PASSWORD'))
|
||||
|
||||
client = MDClient()
|
||||
notes = client.list_notes()
|
||||
""")
|
||||
|
||||
print("\n3. 連接到自訂伺服器:")
|
||||
print("""
|
||||
from mdclient import MDAuth, MDClient
|
||||
|
||||
auth = MDAuth(base_url="https://your-server.com")
|
||||
auth.login("user@example.com", "password")
|
||||
|
||||
client = MDClient(base_url="https://your-server.com")
|
||||
notes = client.list_notes()
|
||||
""")
|
||||
|
||||
def error_handling_example():
|
||||
"""錯誤處理範例"""
|
||||
print("\n=== 錯誤處理 ===\n")
|
||||
|
||||
auth = MDAuth()
|
||||
success = auth.login("wrong@email.com", "wrong_password")
|
||||
|
||||
if not success:
|
||||
print("✗ 登入失敗,請檢查帳密")
|
||||
|
||||
# 檢查 Cookie 是否存在
|
||||
cookies = auth.load_cookie()
|
||||
if not cookies:
|
||||
print("✗ 找不到已儲存的會話,請先登入")
|
||||
else:
|
||||
print("✓ 找到已儲存的會話")
|
||||
|
||||
if __name__ == "__main__":
|
||||
quick_start()
|
||||
# basic_login_example()
|
||||
# env_login_example()
|
||||
# load_existing_session()
|
||||
# custom_server_example()
|
||||
# error_handling_example()
|
||||
Reference in New Issue
Block a user