# Momentry Core API 示範手冊 | 項目 | 內容 | |------|------| | 版本 | V1.0 | | 日期 | 2026-03-25 | | 狀態 | 完成 | --- ## 快速開始 ### Demo API Key ``` API Key: muser_68600856036340bcafc01930eb4bd839_1774418104_97221b69 Key ID: muser_68600856036340bcafc01930eb4bd839 過期日: 2027-03-25 ``` ### 測試連線 ```bash curl http://localhost:3002/health ``` ```json {"status":"ok","version":"0.1.0","uptime_ms":456464} ``` ### 測試認證 ```bash curl -H "X-API-Key: muser_68600856036340bcafc01930eb4bd839_1774418104_97221b69" \ http://localhost:3002/api/v1/videos | jq '.videos | length' ``` ```json 13 ``` --- ## 環境 URL | 環境 | URL | 用途 | |------|-----|------| | **本地開發** | `http://localhost:3002` | 本機開發測試 | | **外部訪問** | `https://api.momentry.ddns.net` | n8n/WordPress/curl 生產環境 | --- ## 端點總覽 | 方法 | 端點 | 說明 | 認證 | |------|------|------|------| | GET | `/health` | 健康檢查 | 公開 | | GET | `/health/detailed` | 詳細健康檢查 | 公開 | | POST | `/api/v1/register` | 註冊影片 | 需要 | | POST | `/api/v1/probe` | 探測影片資訊 | 需要 | | POST | `/api/v1/search` | 語意搜尋 | 需要 | | POST | `/api/v1/n8n/search` | n8n 格式搜尋 | 需要 | | POST | `/api/v1/search/hybrid` | 混合搜尋 | 需要 | | GET | `/api/v1/videos` | 列出所有影片 | 需要 | | GET | `/api/v1/lookup` | 查詢影片 UUID | 需要 | | GET | `/api/v1/progress/:uuid` | 處理進度 | 需要 | | GET | `/api/v1/jobs` | 任務列表 | 需要 | | GET | `/api/v1/jobs/:uuid` | 任務詳情 | 需要 | --- ## 1. curl 範例 ### 基本格式 ```bash curl -H "X-API-Key: YOUR_API_KEY" \ -H "Content-Type: application/json" \ URL ``` ### 1.1 健康檢查(公開) ```bash # 基本健康檢查 curl http://localhost:3002/health # 詳細健康檢查(含服務狀態) curl http://localhost:3002/health/detailed ``` ### 1.2 列出影片 ```bash curl -H "X-API-Key: muser_68600856036340bcafc01930eb4bd839_1774418104_97221b69" \ http://localhost:3002/api/v1/videos | jq '.' ``` ```json { "videos": [ { "uuid": "952f5854b9febad1", "file_name": "ExaSAN PCIe series - Director Ou Yu-Zhi Shares His Experience.mp4", "duration": 159.637188, "width": 640, "height": 360 }, ... ] } ``` ### 1.3 搜尋影片 ```bash curl -X POST \ -H "X-API-Key: muser_68600856036340bcafc01930eb4bd839_1774418104_97221b69" \ -H "Content-Type: application/json" \ -d '{"query": "ExaSAN", "limit": 5}' \ http://localhost:3002/api/v1/search | jq '.' ``` ```json { "results": [ { "uuid": "952f5854b9febad1", "chunk_id": "...", "text": "...", "score": 0.85, "start_time": 0.0, "end_time": 5.0 } ], "total": 1, "query": "ExaSAN", "took_ms": 123 } ``` ### 1.4 查詢進度 ```bash curl -H "X-API-Key: muser_68600856036340bcafc01930eb4bd839_1774418104_97221b69" \ http://localhost:3002/api/v1/progress/952f5854b9febad1 | jq '.' ``` ```json { "uuid": "952f5854b9febad1", "overall_progress": 67, "current_processor": "yolo", "processors": [ {"name": "asr", "status": "completed"}, {"name": "cut", "status": "completed"}, {"name": "yolo", "status": "running"} ] } ``` --- ## 2. n8n 範例 ### 2.1 HTTP Request 節點設定 ``` Method: POST URL: https://api.momentry.ddns.net/api/v1/search Authentication: None (使用 Header) Headers: ┌─────────────────────┬──────────────────────────────────────────────────┐ │ Name │ Value │ ├─────────────────────┼──────────────────────────────────────────────────┤ │ X-API-Key │ muser_68600856036340bcafc01930eb4bd839_... │ │ Content-Type │ application/json │ └─────────────────────┴──────────────────────────────────────────────────┘ Body Content (JSON): { "query": "{{ $json.search_term }}", "limit": 5 } ``` ### 2.2 n8n 搜尋 Workflow ```json { "nodes": [ { "name": "Manual Trigger", "type": "n8n-nodes-base.manualTrigger", "position": [250, 300] }, { "name": "Set Search Term", "type": "n8n-nodes-base.set", "parameters": { "values": { "json": { "search_term": "ExaSAN" } } }, "position": [450, 300] }, { "name": "Search Videos", "type": "n8n-nodes-base.httpRequest", "parameters": { "method": "POST", "url": "https://api.momentry.ddns.net/api/v1/search", "authentication": "genericCredentialType", "genericAuthType": "httpHeaderAuth", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "X-API-Key", "value": "muser_68600856036340bcafc01930eb4bd839_1774418104_97221b69" } ] }, "sendBody": true, "bodyContentType": "json", "specifyBody": "json", "jsonBody": "={{ { \"query\": $json.search_term, \"limit\": 5 } }}" }, "position": [650, 300] }, { "name": "Process Results", "type": "n8n-nodes-base.code", "parameters": { "jsCode": "// Extract video results\nconst results = $input.first().json.results;\nreturn results.map(r => ({\n uuid: r.uuid,\n text: r.text,\n score: r.score,\n time: `${r.start_time}s - ${r.end_time}s`\n}));" }, "position": [850, 300] } ], "connections": { "Manual Trigger": { "main": [[{"node": "Set Search Term"}]] }, "Set Search Term": { "main": [[{"node": "Search Videos"}]] }, "Search Videos": { "main": [[{"node": "Process Results"}]] } } } ``` ### 2.3 n8n 列出影片 Workflow ```json { "nodes": [ { "name": "Get Videos", "type": "n8n-nodes-base.httpRequest", "parameters": { "method": "GET", "url": "https://api.momentry.ddns.net/api/v1/videos", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "X-API-Key", "value": "muser_68600856036340bcafc01930eb4bd839_1774418104_97221b69" } ] } }, "position": [450, 300] }, { "name": "Extract Video List", "type": "n8n-nodes-base.code", "parameters": { "jsCode": "const videos = $input.first().json.videos;\nreturn videos.map(v => ({\n json: {\n uuid: v.uuid,\n name: v.file_name,\n duration: Math.round(v.duration) + 's',\n resolution: `${v.width}x${v.height}`\n }\n}));" }, "position": [650, 300] }, { "name": "Slack Notification", "type": "n8n-nodes-base.slack", "parameters": { "channel": "#momentry", "text": "=Found {{ $json.length }} videos:\n{{ $json.map(v => `• ${v.name} (${v.duration})`).join(`\n`) }}" }, "position": [850, 300] } ] } ``` ### 2.4 n8n 定時同步 Workflow ```json { "nodes": [ { "name": "Schedule Trigger", "type": "n8n-nodes-base.scheduleTrigger", "parameters": { "rule": { "interval": [{"field": "hours", "hours": 1}] } }, "position": [250, 300] }, { "name": "Get Pending Videos", "type": "n8n-nodes-base.httpRequest", "parameters": { "method": "GET", "url": "https://api.momentry.ddns.net/api/v1/videos" }, "position": [450, 300] }, { "name": "Filter Processing", "type": "n8n-nodes-base.filter", "parameters": { "conditions": { "options": {"caseSensitive": true}, "conditions": [ {"id": "status", "leftValue": "{{ $json.status }}", "rightValue": "processing"} ] } }, "position": [650, 300] } ] } ``` --- ## 3. WordPress 範例 ### 3.1 PHP 函數庫 ```php [ 'X-API-Key' => self::API_KEY, 'Content-Type' => 'application/json', ], 'timeout' => 30, ]; if ($method === 'POST') { $args['method'] = 'POST'; $args['body'] = json_encode($data); } $response = wp_remote_request($url, $args); if (is_wp_error($response)) { throw new Exception($response->get_error_message()); } return json_decode(wp_remote_retrieve_body($response), true); } /** * 列出所有影片 */ public function list_videos(): array { return $this->request('/api/v1/videos'); } /** * 搜尋影片內容 */ public function search(string $query, int $limit = 10): array { return $this->request('/api/v1/search', [ 'query' => $query, 'limit' => $limit, ], 'POST'); } /** * 取得影片進度 */ public function get_progress(string $uuid): array { return $this->request("/api/v1/progress/{$uuid}"); } /** * 檢查健康狀態 */ public function health_check(): array { return $this->request('/health'); } } ``` ### 3.2 短代碼 (Shortcode) ```php 10, ], $atts); $api = new Momentry_API(); try { $result = $api->list_videos(); $videos = array_slice($result['videos'], 0, $atts['limit']); ob_start(); ?>
請提供搜尋關鍵字
'; } $api = new Momentry_API(); try { $result = $api->search($query); ob_start(); ?>沒有找到相關結果