做这个的起因是dota查战绩这件事一直很麻烦。V 社官方 API 时不时就用不了,小黑盒之类的 App 搜用户有时候直接返回空,干脆自己搭一个查询页。

站点在 dota.zhangkun.dev,代码在 zk39/dota-stat。后续可能会拿go 重构一个

结构

纯 HTML 前端加一个 Python 后端。后端不到一千行,只用标准库,没有任何第三方依赖。

前端不能直接调 API

  • 跨域:api.steampowered.com 不返回 CORS 头,浏览器里直接 fetch 会被拦
  • key 安全:API key 放在后端环境变量里,安全一点

部署用 Docker,前面挂 nginx 反代,sqlite 文件挂到宿主机上

数据源

  • OpenDota:核心。对局详情、玩家资料、常玩英雄、按昵称搜人
  • STRATZ:辅助。OpenDota 查对局或玩家出错时,自动切到 STRATZ 再查一次
  • V 社官方 API:辅助。英雄列表和玩家头像、昵称从这里拿

查询接口支持 source=auto / opendota / stratz,默认 auto 就是”核心失败自动换备用”。两边都失败才会报错,而且报错信息会写清楚是哪一家挂。

先查SQlite,没有再调 API

dota的对局一局打完数据就不会变了。所以按对局 ID 查过的数据,直接存进 SQLite

查对局 → 本地库里有这个 match_id?
          ├─ 有 → 直接返回,跳过外部 API
          └─ 没有 → 调 OpenDota → 结果写进库 → 返回

同一局比赛只会调一次 OpenDota, 直接从库里返回,节省 API 配额

TTL 缓存

玩家资料、搜索结果、近期战绩这些会变的数据,不能永久存,就放在内存里缓存一段时间:

  • 默认缓存 10 分钟,同一个请求 10 分钟内重复查,直接走缓存。
  • 段位缓存 30 分钟,段位变得没那么快。
  • 搜索没结果只缓存 60 秒 针对”搜不到”的问题:OpenDota 的索引偶尔滞后,要是一次没搜到就缓存 10 分钟,用户会一直看到”没这个人”。
  • 出错的结果不缓存 上游超时不能当成”查无此人”存起来,下次请求会重新查。
  • 缓存条目有上限,满了按写入时间删掉最早的一批。

同一个 key 有多个请求同时进来时,只有第一个去调上游,其他的等它结果出来直接拿。这样热门查询就不会一瞬间把上游打爆。

防止被刷

站点放到公网上以后,加了几层限流

  • 出站调用全局限速,大约每秒一次;遇到 429 就指数退避重试(3、6、12、24 秒)。
  • 每个 IP 每分钟最多 60 次 API 请求。
  • 同时处理的请求数有上限,超过直接返回 503。
  • nginx 层也配了一份 limit_req,跟后端加起来是两道限流。

遇到的问题:搜索一直”没搜到”

页面上线后发现,有时候搜一个名字一直提示”没搜到”,可人明明存在。查下来原因是几件事叠在一起:

  • OpenDota 的 /search 是全文检索,经常要 10 秒以上才返回,之前的版本把超时设成 6 秒,还关掉了重试。
  • 超时以后没有报错,而是静默返回了一个空列表。
  • 空列表又被当成正常结果,进了 10 分钟的缓存,所以这 10 分钟里搜同一个名字一直是”没搜到”。

修改:

  • 超时改成 20 秒,遇到超时或 5xx 重试一次。
  • 读取阶段的超时(TimeoutError)以前没被捕获,现在也会处理。
  • 搜索失败时返回错误,不缓存,页面显示”搜索服务暂时不可用”和重试按钮,不再误报。
  • OpenDota 有时候返回的是一个错误对象而不是列表,以前直接 sort 会崩溃,现在会先检查。
  • 真的搜不到人时,空结果只缓存 60 秒。

ps:按昵称搜人目前只有 OpenDota 能用。V 社没有这个接口,STRATZ 的公开 API 只接受数字 ID,所以搜索这条路没有备用数据源

I built this because looking up Dota stats has always been a pain. Valve’s official API goes down every now and then, and apps like Xiaoheihe (a popular Chinese gaming app) sometimes return nothing when you search for a player. So I just built my own lookup page.

The site is at dota.zhangkun.dev and the code is at zk39/dota-stat. I might rewrite it in Go at some point.

Structure

A plain HTML frontend plus a Python backend. The backend is under a thousand lines, uses only the standard library, and has no third-party dependencies.

The frontend can’t call the APIs directly:

  • CORS: api.steampowered.com doesn’t send CORS headers, so a direct fetch from the browser gets blocked.
  • Key safety: the API key lives in a backend environment variable, which is a bit safer.

It’s deployed with Docker behind an nginx reverse proxy, and the SQLite file is mounted on the host.

Data sources

  • OpenDota: the core. Match details, player profiles, most-played heroes, and searching players by name.
  • STRATZ: the fallback. When OpenDota fails on a match or player lookup, it automatically retries on STRATZ.
  • Valve’s official API: supplementary. The hero list and player avatars and names come from here.

The lookup endpoints accept source=auto / opendota / stratz. The default, auto, means “if the core source fails, switch to the fallback.” It only returns an error when both fail, and the error says which one went down.

Check SQLite first, then call the API

Once a Dota match is over, its data never changes. So any match I’ve looked up by ID goes straight into SQLite.

Look up match → is this match_id in the local DB?
                 ├─ yes → return it, skip the external API
                 └─ no  → call OpenDota → write result to DB → return

Each match only hits OpenDota once. After that it comes straight from the DB, which saves API quota.

TTL cache

Player profiles, search results, and recent matches change over time, so they can’t be stored forever. Those get cached in memory for a while instead:

  • The default cache time is 10 minutes. Repeat the same request within 10 minutes and it’s served from the cache.
  • Ranks are cached for 30 minutes, since they don’t change that fast.
  • Empty search results are only cached for 60 seconds. This targets the “can’t find the player” problem: OpenDota’s index sometimes lags, and if one empty search got cached for 10 minutes, users would keep seeing “no such player.”
  • Errors are never cached. An upstream timeout shouldn’t be stored as “player not found,” so the next request tries again.
  • The cache has a size limit. When it’s full, the oldest entries get dropped in a batch.

When several requests for the same key arrive at once, only the first one calls upstream and the rest wait for its result. That way a popular lookup can’t hammer the upstream API all at once.

Protection against abuse

Once the site was public, I added a few layers of rate limiting:

  • Outgoing API calls are globally limited to about one per second. On a 429, it backs off exponentially and retries (3, 6, 12, 24 seconds).
  • Each IP gets at most 60 API requests per minute.
  • There’s a cap on concurrent requests. Anything over it gets a 503.
  • nginx has its own limit_req too, so together with the backend that’s two layers of rate limiting.

The problem: search kept saying “not found”

After launch, I noticed that searching for a name would sometimes keep showing “not found,” even though the player definitely existed. It turned out to be several things stacking up:

  • OpenDota’s /search is a full-text search and often takes more than 10 seconds to respond. The earlier version set the timeout to 6 seconds and had retries turned off.
  • On timeout it didn’t raise an error. It quietly returned an empty list.
  • That empty list was treated as a normal result and cached for 10 minutes, so for those 10 minutes every search for that name came back “not found.”

The fix:

  • Timeout raised to 20 seconds, with one retry on a timeout or 5xx.
  • Timeouts during the read phase (TimeoutError) weren’t being caught before. Now they are.
  • A failed search returns an error and isn’t cached. The page shows “search is temporarily unavailable” with a retry button instead of a false “not found.”
  • OpenDota sometimes returns an error object instead of a list, and calling sort on it used to crash. Now the type gets checked first.
  • When a player genuinely isn’t found, the empty result is only cached for 60 seconds.

P.S. Searching by player name only works through OpenDota right now. Valve has no endpoint for it, and STRATZ’s public API only accepts numeric IDs, so there’s no fallback for search.