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:
148
example.py
Normal file
148
example.py
Normal file
@@ -0,0 +1,148 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
MDclient 使用範例 - Python 函式庫使用方式
|
||||
|
||||
這個範例展示了如何在 Python 程式碼中使用 MDclient 函式庫
|
||||
"""
|
||||
|
||||
from mdclient import MDClient, MDAuth
|
||||
import os
|
||||
|
||||
def example_basic_usage():
|
||||
"""基本使用範例"""
|
||||
print("=== MDclient 基本使用 ===\n")
|
||||
|
||||
# 建立客戶端(會自動讀取目前目錄的 key.conf)
|
||||
client = MDClient()
|
||||
|
||||
# 列出所有筆記
|
||||
print("1. 列出筆記:")
|
||||
notes = client.list_notes()
|
||||
if notes:
|
||||
for i, note in enumerate(notes[:5], 1):
|
||||
title = note.get('text', '無標題')
|
||||
note_id = note.get('id', '')
|
||||
pinned = " 📌" if note.get('pinned') else ""
|
||||
print(f" {i}. {title}{pinned}")
|
||||
print(f" ID: {note_id}")
|
||||
|
||||
def example_get_note():
|
||||
"""取得筆記內容範例"""
|
||||
print("\n2. 取得筆記內容:")
|
||||
|
||||
client = MDClient()
|
||||
note_id = "features" # 替換為實際的筆記 ID
|
||||
note = client.get_note(note_id)
|
||||
|
||||
if note:
|
||||
content = note.get('content', '')
|
||||
print(f" 內容預覽: {content[:100]}...")
|
||||
|
||||
def example_create_note():
|
||||
"""建立筆記範例"""
|
||||
print("\n3. 建立新筆記:")
|
||||
|
||||
client = MDClient()
|
||||
result = client.create_note(
|
||||
title="Python 測試筆記",
|
||||
content="# 歡迎使用 MDclient\n\n這是一個透過 Python API 建立的筆記。"
|
||||
)
|
||||
|
||||
if result:
|
||||
print(f" 筆記建立成功!")
|
||||
print(f" ID: {result.get('id')}")
|
||||
|
||||
def example_export_note():
|
||||
"""匯出筆記範例"""
|
||||
print("\n4. 匯出筆記:")
|
||||
|
||||
client = MDClient()
|
||||
note_id = "features"
|
||||
content = client.export_note(note_id, format="md")
|
||||
|
||||
if content:
|
||||
filename = f"{note_id}.md"
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
print(f" 筆記已匯出到: {filename}")
|
||||
|
||||
def example_with_env():
|
||||
"""使用 .env 檔案的範例"""
|
||||
print("\n=== 使用 .env 設定 ===\n")
|
||||
|
||||
try:
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
# 從環境變數讀取設定
|
||||
url = os.getenv('MDCLIENT_URL', 'https://codimd.lotimmy.com')
|
||||
email = os.getenv('MDCLIENT_EMAIL')
|
||||
password = os.getenv('MDCLIENT_PASSWORD')
|
||||
|
||||
if email and password:
|
||||
# 登入
|
||||
auth = MDAuth(base_url=url)
|
||||
auth.login(email, password)
|
||||
|
||||
# 使用客戶端
|
||||
client = MDClient(base_url=url)
|
||||
notes = client.list_notes()
|
||||
print(f"找到 {len(notes)} 筆筆記")
|
||||
else:
|
||||
print(" .env 檔案中未設定帳密")
|
||||
|
||||
except ImportError:
|
||||
print(" 需要安裝 python-dotenv: pip install python-dotenv")
|
||||
|
||||
def example_custom_server():
|
||||
"""連接到自訂伺服器的範例"""
|
||||
print("\n=== 連接到自訂伺服器 ===\n")
|
||||
|
||||
# 連接到不同的伺服器
|
||||
auth = MDAuth(
|
||||
base_url="https://your-hedgedoc-server.com",
|
||||
cookie_file="custom_server_cookies.conf"
|
||||
)
|
||||
|
||||
# 注意:需要先登入才能使用客戶端
|
||||
# auth.login("user@example.com", "password")
|
||||
|
||||
# client = MDClient(
|
||||
# base_url="https://your-hedgedoc-server.com",
|
||||
# cookie_file="custom_server_cookies.conf"
|
||||
# )
|
||||
|
||||
def example_search_notes():
|
||||
"""搜尋筆記範例"""
|
||||
print("\n5. 搜尋筆記:")
|
||||
|
||||
client = MDClient()
|
||||
results = client.search_notes("Python")
|
||||
|
||||
if results:
|
||||
print(f" 找到 {len(results)} 筆相關筆記")
|
||||
for note in results[:3]:
|
||||
print(f" - {note.get('text', '無標題')}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 確保已經登入
|
||||
if not os.path.exists("key.conf"):
|
||||
print("請先登入:")
|
||||
print(" 方式1:使用 .env 檔案")
|
||||
print(" 方式2:執行認證程式碼")
|
||||
print("\n範例:")
|
||||
print("""
|
||||
from mdclient import MDAuth
|
||||
auth = MDAuth()
|
||||
auth.login("your@email.com", "password")
|
||||
""")
|
||||
exit(1)
|
||||
|
||||
# 執行範例
|
||||
example_basic_usage()
|
||||
# example_get_note()
|
||||
# example_create_note()
|
||||
# example_export_note()
|
||||
# example_with_env()
|
||||
# example_search_notes()
|
||||
# example_custom_server()
|
||||
Reference in New Issue
Block a user