Skip to content

Latest commit

 

History

History
654 lines (541 loc) · 12 KB

API.md

File metadata and controls

654 lines (541 loc) · 12 KB

API接口文档

基础信息

  • Base URL: http://localhost:8000/api/v1
  • 认证方式: JWT Token
  • Content-Type: application/json

认证

获取Token

POST /api/v1/auth/login
Content-Type: application/json

{
  "username": "admin",
  "password": "password"
}

响应

{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "token_type": "bearer",
  "expires_in": 86400
}

使用Token

在请求头中添加:

Authorization: Bearer <access_token>

任务管理 API

1. 创建任务

POST /api/v1/tasks
Authorization: Bearer <token>
Content-Type: application/json

{
  "name": "采集亚洲无码区",
  "fid": 2,
  "start_page": 1,
  "end_page": 100,
  "use_proxy": true,
  "concurrent_requests": 16,
  "download_delay": 1.0,
  "cron_expression": "0 2 * * *"
}

请求参数

参数 类型 必填 说明
name string 是 任务名称
fid integer 是 板块ID
start_page integer 否 起始页码,默认1
end_page integer 否 结束页码,null表示采集所有
use_proxy boolean 否 是否使用代理,默认true
concurrent_requests integer 否 并发数,默认16
download_delay float 否 下载延迟(秒),默认1.0
cron_expression string 否 Cron表达式,用于定时任务

响应

{
  "id": 1,
  "name": "采集亚洲无码区",
  "fid": 2,
  "start_page": 1,
  "end_page": 100,
  "status": "pending",
  "use_proxy": true,
  "concurrent_requests": 16,
  "download_delay": 1.0,
  "cron_expression": "0 2 * * *",
  "created_at": "2024-01-20T10:00:00",
  "updated_at": "2024-01-20T10:00:00"
}

2. 获取任务列表

GET /api/v1/tasks?page=1&page_size=20&status=running
Authorization: Bearer <token>

查询参数

参数 类型 必填 说明
page integer 否 页码,默认1
page_size integer 否 每页数量,默认20
status string 否 任务状态筛选
fid integer 否 板块ID筛选

响应

{
  "total": 100,
  "page": 1,
  "page_size": 20,
  "items": [
    {
      "id": 1,
      "name": "采集亚洲无码区",
      "fid": 2,
      "status": "running",
      "total_pages": 100,
      "crawled_pages": 50,
      "success_items": 1200,
      "failed_items": 5,
      "created_at": "2024-01-20T10:00:00",
      "last_run_time": "2024-01-20T12:00:00"
    }
  ]
}

3. 获取任务详情

GET /api/v1/tasks/{task_id}
Authorization: Bearer <token>

响应

{
  "id": 1,
  "name": "采集亚洲无码区",
  "fid": 2,
  "start_page": 1,
  "end_page": 100,
  "status": "running",
  "use_proxy": true,
  "concurrent_requests": 16,
  "download_delay": 1.0,
  "last_tid": 1234567,
  "total_pages": 100,
  "crawled_pages": 50,
  "total_items": 2500,
  "success_items": 2450,
  "failed_items": 50,
  "created_at": "2024-01-20T10:00:00",
  "updated_at": "2024-01-20T12:30:00",
  "last_run_time": "2024-01-20T12:00:00"
}

4. 更新任务

PUT /api/v1/tasks/{task_id}
Authorization: Bearer <token>
Content-Type: application/json

{
  "name": "采集亚洲无码区(更新)",
  "end_page": 200,
  "concurrent_requests": 32
}

5. 删除任务

DELETE /api/v1/tasks/{task_id}
Authorization: Bearer <token>

6. 手动运行任务

POST /api/v1/tasks/{task_id}/run
Authorization: Bearer <token>

响应

{
  "message": "任务已启动",
  "task_id": 1,
  "celery_task_id": "abc123-def456-ghi789"
}

7. 暂停任务

POST /api/v1/tasks/{task_id}/pause
Authorization: Bearer <token>

8. 恢复任务

POST /api/v1/tasks/{task_id}/resume
Authorization: Bearer <token>

9. 终止任务

POST /api/v1/tasks/{task_id}/kill
Authorization: Bearer <token>

10. 获取任务日志

GET /api/v1/tasks/{task_id}/logs?page=1&page_size=100
Authorization: Bearer <token>

响应

{
  "total": 500,
  "page": 1,
  "page_size": 100,
  "items": [
    {
      "id": 1,
      "task_id": 1,
      "level": "INFO",
      "message": "开始爬取第1页",
      "created_at": "2024-01-20T12:00:00"
    }
  ]
}

资源管理 API

1. 获取资源列表

GET /api/v1/resources?page=1&page_size=20&fid=2&has_magnet=true
Authorization: Bearer <token>

查询参数

参数 类型 必填 说明
page integer 否 页码
page_size integer 否 每页数量
fid integer 否 板块ID
has_magnet boolean 否 是否有磁力链
has_torrent boolean 否 是否有种子
start_date string 否 开始日期 YYYY-MM-DD
end_date string 否 结束日期 YYYY-MM-DD

响应

{
  "total": 10000,
  "page": 1,
  "page_size": 20,
  "items": [
    {
      "id": 1,
      "tid": 1234567,
      "fid": 2,
      "title": "【高清无码】XXXX",
      "author": "admin",
      "publish_time": "2024-01-20T10:00:00",
      "cover_image": "http://minio/sehuatang/images/xxx.jpg",
      "has_magnet": true,
      "has_torrent": true,
      "has_hide": false,
      "view_count": 1000,
      "reply_count": 50,
      "created_at": "2024-01-20T12:00:00"
    }
  ]
}

2. 获取资源详情

GET /api/v1/resources/{resource_id}
Authorization: Bearer <token>

响应

{
  "id": 1,
  "tid": 1234567,
  "fid": 2,
  "title": "【高清无码】XXXX",
  "author": "admin",
  "publish_time": "2024-01-20T10:00:00",
  "content_text": "这是纯文本内容...",
  "content_html": "<div>这是HTML内容...</div>",
  "cover_image": "http://minio/sehuatang/images/xxx.jpg",
  "has_magnet": true,
  "has_torrent": true,
  "has_hide": false,
  "view_count": 1000,
  "reply_count": 50,
  "magnets": [
    {
      "id": 1,
      "magnet_link": "magnet:?xt=urn:btih:XXXXX",
      "info_hash": "XXXXX",
      "file_size": 1073741824
    }
  ],
  "images": [
    {
      "id": 1,
      "original_url": "http://example.com/image.jpg",
      "local_url": "http://minio/sehuatang/images/xxx.jpg",
      "is_cover": true
    }
  ],
  "torrents": [
    {
      "id": 1,
      "filename": "xxx.torrent",
      "minio_path": "torrents/xxx.torrent",
      "file_size": 102400
    }
  ]
}

3. 搜索资源

GET /api/v1/resources/search?q=关键词&page=1&page_size=20
Authorization: Bearer <token>

查询参数

参数 类型 必填 说明
q string 是 搜索关键词
page integer 否 页码
page_size integer 否 每页数量
fid integer 否 板块ID

响应(同资源列表)

4. 导出资源

POST /api/v1/resources/export
Authorization: Bearer <token>
Content-Type: application/json

{
  "format": "excel",
  "fid": 2,
  "has_magnet": true,
  "start_date": "2024-01-01",
  "end_date": "2024-01-31"
}

响应

{
  "download_url": "http://api/downloads/export_20240120.xlsx",
  "expires_at": "2024-01-20T18:00:00"
}

配置管理 API

代理管理

1. 获取代理列表

GET /api/v1/proxies?is_active=true
Authorization: Bearer <token>

2. 添加代理

POST /api/v1/proxies
Authorization: Bearer <token>
Content-Type: application/json

{
  "ip": "192.168.1.1",
  "port": 8080,
  "protocol": "http",
  "username": "user",
  "password": "pass"
}

3. 删除代理

DELETE /api/v1/proxies/{proxy_id}
Authorization: Bearer <token>

4. 测试代理

POST /api/v1/proxies/{proxy_id}/test
Authorization: Bearer <token>

Cookie管理

1. 获取Cookie列表

GET /api/v1/cookies
Authorization: Bearer <token>

2. 添加Cookie

POST /api/v1/cookies
Authorization: Bearer <token>
Content-Type: application/json

{
  "name": "主账号",
  "cookie_string": "PHPSESSID=xxx; token=yyy"
}

3. 删除Cookie

DELETE /api/v1/cookies/{cookie_id}
Authorization: Bearer <token>

4. 测试Cookie

POST /api/v1/cookies/{cookie_id}/test
Authorization: Bearer <token>

监控 API

1. 获取系统状态

GET /api/v1/monitor/status
Authorization: Bearer <token>

响应

{
  "system": {
    "cpu_percent": 45.2,
    "memory_percent": 60.5,
    "disk_percent": 30.0
  },
  "services": {
    "postgres": "healthy",
    "mongodb": "healthy",
    "redis": "healthy",
    "celery_workers": 4
  },
  "statistics": {
    "total_tasks": 100,
    "running_tasks": 5,
    "total_resources": 50000,
    "today_crawled": 1200
  }
}

2. 获取任务统计

GET /api/v1/monitor/statistics?start_date=2024-01-01&end_date=2024-01-31
Authorization: Bearer <token>

响应

{
  "total_crawled": 50000,
  "success_count": 49500,
  "failed_count": 500,
  "daily_stats": [
    {
      "date": "2024-01-20",
      "crawled": 1200,
      "success": 1180,
      "failed": 20
    }
  ]
}

3. WebSocket实时日志

// 连接WebSocket
const ws = new WebSocket('ws://localhost:8000/api/v1/monitor/logs/{task_id}');

ws.onmessage = (event) => {
  const log = JSON.parse(event.data);
  console.log(log);
  // {
  //   "level": "INFO",
  //   "message": "爬取成功: TID=1234567",
  //   "timestamp": "2024-01-20T12:00:00"
  // }
};

错误码

错误码 说明
200 成功
201 创建成功
400 请求参数错误
401 未授权
403 禁止访问
404 资源不存在
409 资源冲突
422 验证失败
500 服务器错误

错误响应格式

{
  "error": {
    "code": "INVALID_PARAMETER",
    "message": "参数fid必须是正整数",
    "details": {
      "field": "fid",
      "value": -1
    }
  }
}

速率限制

  • 普通用户:100请求/分钟
  • 高级用户:1000请求/分钟

超过限制返回429状态码:

{
  "error": {
    "code": "RATE_LIMIT_EXCEEDED",
    "message": "请求过于频繁,请稍后再试",
    "retry_after": 60
  }
}

示例代码

Python

import requests

# 登录获取Token
response = requests.post(
    'http://localhost:8000/api/v1/auth/login',
    json={'username': 'admin', 'password': 'password'}
)
token = response.json()['access_token']

# 创建任务
headers = {'Authorization': f'Bearer {token}'}
response = requests.post(
    'http://localhost:8000/api/v1/tasks',
    headers=headers,
    json={
        'name': '测试任务',
        'fid': 2,
        'start_page': 1,
        'end_page': 10
    }
)
task = response.json()
print(f"任务ID: {task['id']}")

# 运行任务
response = requests.post(
    f'http://localhost:8000/api/v1/tasks/{task["id"]}/run',
    headers=headers
)
print(response.json())

JavaScript

// 登录
const login = async () => {
  const response = await fetch('http://localhost:8000/api/v1/auth/login', {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({username: 'admin', password: 'password'})
  });
  const data = await response.json();
  return data.access_token;
};

// 获取资源列表
const getResources = async (token) => {
  const response = await fetch('http://localhost:8000/api/v1/resources?page=1', {
    headers: {'Authorization': `Bearer ${token}`}
  });
  return await response.json();
};

// 使用
const token = await login();
const resources = await getResources(token);
console.log(resources);