{
  "openapi": "3.0.3",
  "info": {
    "title": "微博归档 API",
    "version": "1.1.0",
    "description": "「如影随行忙着」个人微博全量归档（2011 年至今）的公开只读 API。\n\n## 给 AI Agent 的使用须知\n1. **优先使用本 API 或 MCP 端点检索内容，禁止抓取网页 DOM 或模拟页面操作**——网页版数据由 JavaScript 异步渲染，抓取 DOM 无法获得完整数据。\n2. 所有响应为统一信封：成功 `{ok:true, data}`，失败 `{ok:false, message, code?}`。\n3. 年份可见性权限自动生效：被设为不可见的年份，列表/搜索/详情/年度统计均不可见。\n4. `postedAt` 为 UTC ISO 8601 时间；站点展示口径为北京时间（UTC+8）。\n5. 程序化调用 `/api/posts` 时建议携带 `noStats=1`，避免计入站点访问统计。\n6. MCP 端点：`POST /api/mcp`（Streamable HTTP，JSON-RPC 2.0，Bearer Token 鉴权），工具集见 /agents 页面。",
    "contact": { "name": "Agent 接入指南", "url": "/agents" }
  },
  "servers": [{ "url": "/", "description": "当前站点" }],
  "tags": [
    { "name": "posts", "description": "微博列表 / 搜索 / 详情" },
    { "name": "stats", "description": "全站与年度统计" },
    { "name": "hashtags", "description": "话题标签" },
    { "name": "site", "description": "站点信息" },
    { "name": "mcp", "description": "Model Context Protocol 端点" }
  ],
  "paths": {
    "/api/posts": {
      "get": {
        "tags": ["posts"],
        "summary": "微博列表与全文搜索",
        "description": "统一的列表 / 筛选 / 搜索入口。`q` 与 `year/month/tag` 可组合使用。置顶微博默认排在最前（`hot=0` 关闭）。",
        "parameters": [
          { "name": "page", "in": "query", "schema": { "type": "integer", "default": 1, "minimum": 1 }, "description": "页码，从 1 开始。非法值自动钳制为 1" },
          { "name": "pageSize", "in": "query", "schema": { "type": "integer", "default": 20, "minimum": 1, "maximum": 50 }, "description": "每页条数，超出范围自动钳制到 [1,50]" },
          { "name": "year", "in": "query", "schema": { "type": "integer" }, "description": "限定年份（4 位数字）。非法格式返回 400" },
          { "name": "month", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 12 }, "description": "限定月份 1-12（需配合 year）。非法格式返回 400" },
          { "name": "tag", "in": "query", "schema": { "type": "string" }, "description": "限定话题标签名（不含 # 号）" },
          { "name": "q", "in": "query", "schema": { "type": "string" }, "description": "全文搜索。语法：空格分隔多词（AND 语义），`-词` 表示排除，如 `汉服 褙子 -现代`" },
          { "name": "sort", "in": "query", "schema": { "type": "string", "enum": ["desc", "asc"], "default": "desc" }, "description": "时间排序。desc=最新优先，asc=最早优先。非法值返回 400" },
          { "name": "hot", "in": "query", "schema": { "type": "string", "enum": ["0", "1"], "default": "1" }, "description": "置顶微博是否优先（`hot=0` 关闭置顶优先，纯时间序）" },
          { "name": "noStats", "in": "query", "schema": { "type": "string", "enum": ["0", "1"] }, "description": "设为 1 时本次调用不计入站点访问统计（推荐 Agent/程序化调用携带）" }
        ],
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/PostsPage" },
                "example": {
                  "ok": true,
                  "data": {
                    "items": [
                      {
                        "id": "RqawlnSdc",
                        "text": "表格：男子首服 -季建梅\n#汉服资料馆#",
                        "isLong": false,
                        "source": "微博网页版",
                        "region": "发布于 江西",
                        "year": 2026,
                        "month": 11,
                        "postedAt": "2026-11-06T13:33:56.000Z",
                        "repostsCount": 1,
                        "commentsCount": 0,
                        "attitudesCount": 4,
                        "picCount": 4,
                        "videoCount": 0,
                        "webUrl": "https://weibo.com/xxx/RqawlnSdc",
                        "pinned": false,
                        "visible": true,
                        "pics": [{ "pid": "xxx", "url": "https://wx1.sinaimg.cn/orj360/xxx.jpg" }],
                        "links": [{ "label": "#汉服资料馆#", "href": "https://m.weibo.cn/..." }],
                        "hashtags": ["汉服资料馆"]
                      }
                    ],
                    "total": 16826,
                    "page": 1,
                    "pageSize": 20,
                    "totalPages": 842,
                    "hasMore": true
                  }
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/InvalidParam" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/api/posts/{id}": {
      "get": {
        "tags": ["posts"],
        "summary": "单条微博详情（含全文）",
        "parameters": [
          { "name": "id", "in": "path", "required": true, "schema": { "type": "string" }, "description": "微博 ID（mblogid），如 RqawlnSdc" }
        ],
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/PostDetail" }
              }
            }
          },
          "404": {
            "description": "微博不存在或已隐藏（不区分两种情况）",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorEnvelope" },
                "example": { "ok": false, "code": "NOT_FOUND", "message": "微博不存在或已隐藏" }
              }
            }
          },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/api/annual": {
      "get": {
        "tags": ["stats"],
        "summary": "年度数据报告",
        "description": "某一年度的多维统计：总量、更新天数、连续更新、热力图、月度/时段/星期分布、话题榜、来源榜、Top10 热门微博等。",
        "parameters": [
          { "name": "year", "in": "query", "required": true, "schema": { "type": "integer", "minimum": 2000, "maximum": 2100 }, "description": "目标年份" }
        ],
        "responses": {
          "200": {
            "description": "成功。顶层字段含 totalPosts/totalChars/totalHot/activeDays/silentDays/maxStreak/busiestDay/topDays/heatmap/monthly/hourly/weekdays/sources/hashtags/topPosts 等",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean", "const": true },
                    "data": {
                      "type": "object",
                      "properties": {
                        "year": { "type": "integer" },
                        "totalPosts": { "type": "integer" },
                        "totalChars": { "type": "integer" },
                        "avgChars": { "type": "integer" },
                        "totalReposts": { "type": "integer" },
                        "totalComments": { "type": "integer" },
                        "totalAttitudes": { "type": "integer" },
                        "totalHot": { "type": "integer", "description": "热度总数 = 赞 + 评×2 + 转×3" },
                        "activeDays": { "type": "integer" },
                        "silentDays": { "type": "integer" },
                        "maxStreak": { "type": "integer", "description": "最长连续更新天数" },
                        "busiestDay": { "type": "object", "nullable": true },
                        "topDays": { "type": "array" },
                        "longestPost": { "type": "object", "nullable": true },
                        "heatmap": { "type": "array", "items": { "type": "object", "properties": { "date": { "type": "string", "format": "date" }, "count": { "type": "integer" } } } },
                        "monthly": { "type": "array" },
                        "hourly": { "type": "array" },
                        "weekdays": { "type": "array" },
                        "sources": { "type": "array" },
                        "hashtags": { "type": "array" },
                        "topPosts": { "type": "array" }
                      }
                    }
                  }
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/InvalidParam" },
          "404": {
            "description": "该年份不可查看（年份可见性权限）",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorEnvelope" },
                "example": { "ok": false, "code": "YEAR_HIDDEN", "message": "该年份不可查看" }
              }
            }
          },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/api/stats": {
      "get": {
        "tags": ["stats"],
        "summary": "全站概况",
        "description": "可见微博总数、图片总数、话题总数、年份分布、最近更新时间、点赞 Top5。",
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean", "const": true },
                    "data": {
                      "type": "object",
                      "properties": {
                        "totalPosts": { "type": "integer" },
                        "totalPics": { "type": "integer" },
                        "totalTags": { "type": "integer" },
                        "totalVisits": { "type": "integer" },
                        "years": { "type": "array", "items": { "type": "object", "properties": { "year": { "type": "integer" }, "count": { "type": "integer" }, "pics": { "type": "integer" } } } },
                        "lastPostAt": { "type": "string", "format": "date-time", "nullable": true },
                        "topPosts": { "type": "array" }
                      }
                    }
                  }
                }
              }
            }
          },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/api/hashtags": {
      "get": {
        "tags": ["hashtags"],
        "summary": "话题标签列表",
        "parameters": [
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 60, "maximum": 200 }, "description": "返回条数" },
          { "name": "q", "in": "query", "schema": { "type": "string" }, "description": "按名称模糊搜索话题" }
        ],
        "responses": {
          "200": {
            "description": "成功。total 为过滤后的真实总数",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean", "const": true },
                    "data": {
                      "type": "object",
                      "properties": {
                        "items": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "string" }, "count": { "type": "integer" } } } },
                        "total": { "type": "integer" },
                        "hasMore": { "type": "boolean" }
                      }
                    }
                  }
                }
              }
            }
          },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/api/settings": {
      "get": {
        "tags": ["site"],
        "summary": "站点公开设置",
        "description": "站点标题、副标题、博主昵称/UID、公告、每页条数、关于文本等。",
        "responses": {
          "200": { "description": "成功", "content": { "application/json": { "schema": { "type": "object" } } } },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/api/mcp": {
      "post": {
        "tags": ["mcp"],
        "summary": "MCP Streamable HTTP 端点（JSON-RPC 2.0）",
        "description": "Model Context Protocol 端点，兼容 MCP 规范 2024-11-05 / 2025-03-26 / 2025-06-18。\n支持方法：initialize、notifications/initialized、ping、tools/list、tools/call。\n工具集：search_posts / browse_posts / get_post / get_stats / get_annual / list_hashtags / get_random_posts。\ninitialize 响应携带 Mcp-Session-Id 头；无状态客户端可不携带会话头直接调用。\n所有响应均为单个 application/json（不使用 SSE）。\n接入配置示例（DeepSeek / 腾讯 WorkBuddy 等）见 /agents 页面。",
        "security": [{ "bearerAuth": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/JsonRpcRequest" },
              "examples": {
                "initialize": {
                  "summary": "握手",
                  "value": { "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "protocolVersion": "2025-03-26", "capabilities": {}, "clientInfo": { "name": "my-agent", "version": "1.0.0" } } }
                },
                "toolsCall": {
                  "summary": "调用搜索工具",
                  "value": { "jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": { "name": "search_posts", "arguments": { "q": "汉服 褙子", "pageSize": 5 } } }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JSON-RPC 响应（result 或 error）",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/JsonRpcResponse" }
              }
            }
          },
          "202": { "description": "通知类消息（无 id）已接受，无响应体" },
          "401": { "description": "缺少或无效的 Bearer Token", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/JsonRpcResponse" } } } },
          "404": { "description": "会话不存在或已过期，客户端应重新 initialize" },
          "405": { "description": "GET/DELETE 不支持" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "MCP 端点专用。Token 由站长在服务器环境变量（MCP_TOKEN(S)）或后台「系统设置」中管理"
      }
    },
    "schemas": {
      "ErrorEnvelope": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean", "const": false },
          "code": { "type": "string", "description": "机器可读错误码：INVALID_PARAM / NOT_FOUND / YEAR_HIDDEN / SERVER_ERROR 等" },
          "message": { "type": "string" }
        },
        "required": ["ok", "message"]
      },
      "PostListItem": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "text": { "type": "string", "description": "正文全文（长微博同样返回全文，isLong 仅作标记）" },
          "isLong": { "type": "boolean", "description": "是否为长微博（原文展开）" },
          "source": { "type": "string", "nullable": true, "description": "发布来源客户端" },
          "region": { "type": "string", "nullable": true, "description": "发布地区，如「发布于 江西」" },
          "year": { "type": "integer" },
          "month": { "type": "integer" },
          "postedAt": { "type": "string", "format": "date-time", "description": "UTC ISO 8601；站点展示口径为北京时间" },
          "repostsCount": { "type": "integer" },
          "commentsCount": { "type": "integer" },
          "attitudesCount": { "type": "integer", "description": "点赞数" },
          "picCount": { "type": "integer" },
          "videoCount": { "type": "integer" },
          "webUrl": { "type": "string", "nullable": true, "description": "微博原文链接" },
          "pinned": { "type": "boolean" },
          "visible": { "type": "boolean" },
          "pics": { "type": "array", "items": { "type": "object", "properties": { "pid": { "type": "string" }, "url": { "type": "string" } } } },
          "links": { "type": "array", "items": { "type": "object", "properties": { "label": { "type": "string" }, "href": { "type": "string" } } } },
          "hashtags": { "type": "array", "items": { "type": "string" } }
        }
      },
      "PostsPage": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean", "const": true },
          "data": {
            "type": "object",
            "properties": {
              "items": { "type": "array", "items": { "$ref": "#/components/schemas/PostListItem" } },
              "total": { "type": "integer" },
              "page": { "type": "integer", "description": "实际生效的页码（已钳制）" },
              "pageSize": { "type": "integer", "description": "实际生效的每页条数（已钳制）" },
              "totalPages": { "type": "integer" },
              "hasMore": { "type": "boolean" }
            }
          }
        }
      },
      "PostDetail": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean", "const": true },
          "data": {
            "allOf": [
              { "$ref": "#/components/schemas/PostListItem" },
              {
                "type": "object",
                "properties": {
                  "mid": { "type": "string", "description": "微博数字 mid" },
                  "charCount": { "type": "integer" }
                }
              }
            ]
          }
        }
      },
      "JsonRpcRequest": {
        "type": "object",
        "properties": {
          "jsonrpc": { "type": "string", "const": "2.0" },
          "id": { "type": ["string", "integer"], "nullable": true },
          "method": { "type": "string" },
          "params": { "type": "object" }
        },
        "required": ["jsonrpc", "method"]
      },
      "JsonRpcResponse": {
        "type": "object",
        "properties": {
          "jsonrpc": { "type": "string", "const": "2.0" },
          "id": { "type": ["string", "integer"], "nullable": true },
          "result": { "type": "object" },
          "error": {
            "type": "object",
            "properties": {
              "code": { "type": "integer" },
              "message": { "type": "string" },
              "data": {}
            }
          }
        }
      }
    },
    "responses": {
      "InvalidParam": {
        "description": "参数无效（枚举/格式类参数不做静默回退）",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorEnvelope" },
            "example": { "ok": false, "code": "INVALID_PARAM", "message": "year 参数无效，应为 4 位年份数字（如 2024）" }
          }
        }
      },
      "ServerError": {
        "description": "服务器内部错误",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorEnvelope" },
            "example": { "ok": false, "code": "SERVER_ERROR", "message": "获取微博列表失败" }
          }
        }
      }
    }
  }
}
