{
  "openapi": "3.1.0",
  "info": {
    "title": "ELH Trends API（流行熱度趨勢）",
    "version": "0.2.0",
    "description": "台灣華語媒體話題的每日熱度、歷史、關聯詞與自動分類。熱度＝媒體報導熱度。前三名話題需要會員金鑰（x-api-key）才會顯示；訪客每日 60 次、免費會員每月 300 次，付費方案額度更高。"
  },
  "servers": [
    {
      "url": "https://api.easylifehub.net/trends",
      "description": "正式"
    }
  ],
  "paths": {
    "/v1/trends": {
      "get": {
        "summary": "熱門、新出現或升溫話題榜單（預設前 20 名）",
        "description": "排序：先比獨立來源數，再比去重文章數；兩個原始數值都附上可自行重排。本期與前期用等長期間比較；前期資料不完整時 growth_pct 為 null，前期為零標為 new。",
        "parameters": [
          {
            "name": "date",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            },
            "description": "台北日期，預設最近一次採集日"
          },
          {
            "name": "period",
            "in": "query",
            "schema": {
              "type": "integer",
              "enum": [
                1,
                3,
                7,
                15,
                20
              ],
              "default": 1
            }
          },
          {
            "name": "market",
            "in": "query",
            "schema": {
              "type": "string",
              "default": "TW"
            },
            "description": "報導市場"
          },
          {
            "name": "lang",
            "in": "query",
            "schema": {
              "type": "string",
              "default": "zh-Hant"
            },
            "description": "文章語言"
          },
          {
            "name": "category",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "主分類或子類代碼，見 /v1/categories"
          },
          {
            "name": "type",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "hot",
                "new",
                "rising"
              ],
              "default": "hot"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 20,
              "maximum": 100
            }
          },
          {
            "name": "sort",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "sources",
                "articles"
              ],
              "default": "sources"
            }
          },
          {
            "name": "balance",
            "in": "query",
            "schema": {
              "type": "integer"
            },
            "description": "每個主分類最多幾個"
          }
        ],
        "responses": {
          "200": {
            "description": "榜單",
            "content": {
              "application/json": {
                "example": {
                  "query": {
                    "date": "2026-11-28",
                    "period": 3,
                    "market": "TW",
                    "lang": "zh-Hant",
                    "category": null,
                    "type": "hot",
                    "limit": 20
                  },
                  "ranking_rule": "先比獨立來源數，再比去重文章數（兩個原始數值都附上，可自行重排）",
                  "period": {
                    "current": {
                      "from": "2026-11-26",
                      "to": "2026-11-28"
                    },
                    "previous": {
                      "from": "2026-11-23",
                      "to": "2026-11-25"
                    }
                  },
                  "items": [
                    {
                      "rank": 1,
                      "id": null,
                      "term": "前三名・解鎖查看",
                      "locked": true,
                      "category": {
                        "main": "politics"
                      },
                      "sources": 3,
                      "articles": 21
                    },
                    {
                      "rank": 1,
                      "id": 12,
                      "term": "全家",
                      "kind": "lexicon",
                      "category": {
                        "main": "food_travel",
                        "categories": [
                          "food_travel",
                          "lifestyle"
                        ],
                        "tags": [
                          "restaurant"
                        ],
                        "status": "classified"
                      },
                      "sources": 3,
                      "articles": 5,
                      "first_seen": "2026-11-26",
                      "change": {
                        "status": "new",
                        "prev_sources": 0,
                        "prev_articles": 0,
                        "growth_pct": null
                      }
                    }
                  ],
                  "access": {
                    "plan": "anonymous",
                    "plan_name": "訪客",
                    "unlocked": false,
                    "remaining": 57,
                    "upgrade_url": "https://api.easylifehub.net/trends/#plans",
                    "message": "免費加入會員即可解鎖前三名與話題行情。"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/v1/topics/{id}/history": {
      "get": {
        "summary": "單一話題每日文章數、來源數、排名與資料完整度",
        "description": "每天的 status：observed（有提及）、zero_mentions（來源有抓到但零提及）、missing_data（來源沒抓到，不是零）、before_observation（尚未開始觀測）。",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer"
            }
          },
          {
            "name": "days",
            "in": "query",
            "schema": {
              "type": "integer",
              "enum": [
                1,
                3,
                7,
                15,
                20
              ],
              "default": 20
            }
          },
          {
            "name": "to",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "日序列"
          },
          "404": {
            "description": "找不到話題"
          },
          "402": {
            "description": "前三名話題，需要會員解鎖"
          }
        }
      }
    },
    "/v1/topics/{id}/related": {
      "get": {
        "summary": "關聯話題、共同出現次數與證據，可比較兩段期間",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer"
            }
          },
          {
            "name": "from",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            },
            "description": "預設 to 往前 6 天"
          },
          {
            "name": "to",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "compare_from",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            },
            "description": "預設為本期之前的等長期間"
          },
          {
            "name": "compare_to",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 20
            }
          }
        ],
        "responses": {
          "200": {
            "description": "關聯詞（含 added_in_current、dropped_from_previous）"
          },
          "404": {
            "description": "找不到話題"
          },
          "402": {
            "description": "前三名話題，需要會員解鎖"
          }
        }
      }
    },
    "/v1/topics/search": {
      "get": {
        "summary": "用名稱或別名找話題 id",
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "符合的話題"
          }
        }
      }
    },
    "/v1/categories": {
      "get": {
        "summary": "分類代碼、中英文名稱、子類與分類版本",
        "responses": {
          "200": {
            "description": "分類表"
          }
        }
      }
    },
    "/v1/classify": {
      "post": {
        "summary": "傳入標題或關鍵字，回傳主分類、標籤、判定依據與分類版本（不計入熱度）",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "text": {
                    "type": "string"
                  },
                  "title": {
                    "type": "string"
                  },
                  "keywords": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  },
                  "context": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    },
                    "description": "選填：相關標題，幫助判斷新詞"
                  },
                  "items": {
                    "type": "array",
                    "maxItems": 50,
                    "description": "批次：每筆同上"
                  }
                }
              },
              "example": {
                "text": "央行公布穩定幣政策"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "分類結果",
            "content": {
              "application/json": {
                "example": {
                  "input": "央行公布穩定幣政策",
                  "main": "crypto",
                  "categories": [
                    {
                      "code": "crypto",
                      "score": 3
                    },
                    {
                      "code": "politics",
                      "score": 4
                    },
                    {
                      "code": "finance",
                      "score": 3
                    }
                  ],
                  "tags": [
                    "stablecoin",
                    "policy",
                    "macro"
                  ],
                  "metrics": [],
                  "status": "classified",
                  "evidence": [
                    {
                      "keyword": "穩定幣",
                      "category": "crypto",
                      "tag": "stablecoin",
                      "where": "text",
                      "score": 3
                    }
                  ],
                  "version": "tax-2026.11.28-r2+rules-r1",
                  "counted_in_heat": false
                }
              }
            }
          }
        }
      }
    },
    "/v1/overview": {
      "get": {
        "summary": "儀表板總覽：數字卡、分類分佈、Top 20 走勢、話題版圖、各分類前 5",
        "parameters": [
          {
            "name": "date",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "period",
            "in": "query",
            "schema": {
              "type": "integer",
              "enum": [
                1,
                3,
                7,
                15,
                20
              ]
            }
          },
          {
            "name": "sort",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "sources",
                "articles"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "總覽"
          }
        }
      }
    },
    "/v1/keys": {
      "post": {
        "summary": "申請免費會員金鑰（同一個網路每月一把）",
        "responses": {
          "201": {
            "description": "金鑰（只顯示一次）"
          },
          "429": {
            "description": "本月已申請過"
          }
        }
      }
    },
    "/v1/me": {
      "get": {
        "summary": "查詢目前身分、剩餘額度與方案",
        "responses": {
          "200": {
            "description": "身分與額度"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "x-api-key"
      }
    }
  },
  "security": [
    {},
    {
      "ApiKey": []
    }
  ]
}