{
  "openapi": "3.1.0",
  "info": {
    "title": "恰研搜索 API",
    "version": "1.0.0",
    "description": "搜索微信公众号文章，并读取可公开访问的正文。"
  },
  "servers": [
    {
      "url": "https://search.qiayanai.com"
    }
  ],
  "security": [
    {
      "apiKeyBearer": []
    }
  ],
  "paths": {
    "/v1/search": {
      "get": {
        "operationId": "searchWechatArticles",
        "summary": "搜索公众号文章",
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 100
            },
            "description": "文章关键词"
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 20,
              "default": 10
            },
            "description": "最多返回条数，实际条数取决于上游"
          }
        ],
        "responses": {
          "200": {
            "description": "搜索结果。取得微信链接后，仍需单独读取正文。",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SearchResponse"
                }
              }
            }
          },
          "400": {
            "description": "参数错误",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "API Key 缺失或无效",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "429": {
            "description": "超过每 Key 调用频率，Retry-After 为建议等待秒数",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "内部错误",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "502": {
            "description": "上游页面不可用、格式变化或响应超限",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "503": {
            "description": "服务繁忙，或上游限制访问、要求访问验证",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "504": {
            "description": "等待上游响应超时",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/v1/read": {
      "get": {
        "operationId": "readWechatArticle",
        "summary": "读取公开文章的 Markdown 正文",
        "parameters": [
          {
            "name": "url",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uri",
              "maxLength": 4096
            },
            "description": "微信 mp.weixin.qq.com/s 文章或搜狗 weixin.sogou.com/link 跳转链接"
          }
        ],
        "responses": {
          "200": {
            "description": "正文和元数据",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Article"
                }
              }
            }
          },
          "404": {
            "description": "链接无法解析或文章公开正文不可用",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "400": {
            "description": "参数错误",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "API Key 缺失或无效",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "429": {
            "description": "超过每 Key 调用频率，Retry-After 为建议等待秒数",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "内部错误",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "502": {
            "description": "上游页面不可用、格式变化或响应超限",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "503": {
            "description": "服务繁忙，或上游限制访问、要求访问验证",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "504": {
            "description": "等待上游响应超时",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiKeyBearer": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "API Key"
      }
    },
    "schemas": {
      "SearchResponse": {
        "type": "object",
        "required": [
          "query",
          "limit",
          "count",
          "results"
        ],
        "properties": {
          "query": {
            "type": "string"
          },
          "limit": {
            "type": "integer"
          },
          "count": {
            "type": "integer"
          },
          "results": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/SearchResult"
            }
          },
          "warning": {
            "$ref": "#/components/schemas/Error"
          }
        }
      },
      "SearchResult": {
        "type": "object",
        "required": [
          "title",
          "link_status"
        ],
        "properties": {
          "title": {
            "type": "string"
          },
          "account": {
            "type": "string"
          },
          "published_at": {
            "type": "string",
            "format": "date-time"
          },
          "summary": {
            "type": "string"
          },
          "wechat_url": {
            "type": "string",
            "format": "uri"
          },
          "source_url": {
            "type": "string",
            "description": "搜狗跳转链接，可能过期",
            "format": "uri"
          },
          "link_status": {
            "type": "string",
            "description": "是否解析出微信链接",
            "enum": [
              "resolved",
              "unresolved"
            ]
          },
          "link_error": {
            "$ref": "#/components/schemas/Error"
          }
        }
      },
      "Article": {
        "type": "object",
        "required": [
          "title",
          "fetched_at",
          "canonical_url",
          "link_status",
          "content_hash",
          "markdown"
        ],
        "properties": {
          "title": {
            "type": "string"
          },
          "account": {
            "type": "string"
          },
          "author": {
            "type": "string"
          },
          "summary": {
            "type": "string"
          },
          "published_at": {
            "type": "string",
            "description": "上游提供的发布时间"
          },
          "fetched_at": {
            "type": "string",
            "format": "date-time"
          },
          "canonical_url": {
            "type": "string",
            "format": "uri"
          },
          "link_status": {
            "type": "string",
            "enum": [
              "stable",
              "temporary"
            ]
          },
          "content_hash": {
            "type": "string",
            "description": "Markdown 正文的 SHA-256，带 sha256: 前缀"
          },
          "markdown": {
            "type": "string",
            "description": "正文及微信图片链接"
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "$ref": "#/components/schemas/Error"
          }
        }
      },
      "Error": {
        "type": "object",
        "required": [
          "code",
          "message"
        ],
        "properties": {
          "code": {
            "type": "string"
          },
          "stage": {
            "type": "string",
            "enum": [
              "search",
              "resolve",
              "article"
            ]
          },
          "message": {
            "type": "string"
          }
        }
      }
    }
  }
}
