HTTP · JSON · SSE

Presence 接口文档Presence API

Presence 是 now.ignat.ai 背后的服务:把 Mac、手机等设备上报的信号整理成“此刻大致在做什么”,并长期记录每天各类活动的时间。本页只介绍读取数据的接口。

Presence powers now.ignat.ai: it turns signals from a Mac, a phone and other devices into “what I'm roughly doing right now”, and keeps a long-term record of daily activity time. This page covers the read endpoints only.

BASEhttps://api.ignat.ai/presence/api
  1. 此刻状态GET /now 拿一次当前状态,或用 GET /stream 实时接收变化。
  2. Right nowGET /now once, or follow changes live with GET /stream.
  3. 某一天GET /today、GET /day?date= 返回当天的时间线和各类别时长。
  4. One dayGET /today and GET /day?date= return that day's timeline and per-category totals.
  5. 一段时间GET /history 返回每天的时长汇总和游戏、听歌榜。
  6. A rangeGET /history returns daily totals plus game and music charts.

通用约定Conventions

  • 公开接口不需要任何凭证,只接受 GET(/media 另接受 HEAD)。
  • 跨域:浏览器里只有 https://now.ignat.ai 和 https://ignat.ai 能直接读取(响应带 Access-Control-Allow-Origin);其他网站的前端请求会被浏览器拦下。服务器端脚本、curl 不受影响。
  • JSON 接口都带 Cache-Control: no-store,每次都是最新数据。
  • 时间戳是 Unix 秒(可能带小数);日期写成 YYYY-MM-DD,按 Asia/Shanghai 时区、凌晨 4 点切分,凌晨 4 点前仍算前一天。
  • 时长字段(*_sec、seconds)是取整后的秒数。
  • Public endpoints need no credentials and accept GET only (/media also takes HEAD).
  • CORS: in browsers only https://now.ignat.ai and https://ignat.ai may read responses (they get Access-Control-Allow-Origin); front-end calls from other sites are blocked by the browser. Server-side scripts and curl are unaffected.
  • JSON responses carry Cache-Control: no-store, so they are always fresh.
  • Timestamps are Unix seconds (possibly fractional). Dates are YYYY-MM-DD in Asia/Shanghai, split at 4 a.m.: anything before 4 a.m. belongs to the previous day.
  • Durations (*_sec, seconds) are whole seconds.

快速上手Quick start

curl

shell
# Current status
curl https://api.ignat.ai/presence/api/now

# Follow changes live (Ctrl+C to stop)
curl -N https://api.ignat.ai/presence/api/stream

# Last week, day by day
curl 'https://api.ignat.ai/presence/api/history?from=2026-10-20&to=2026-10-26'

JavaScript

browser · node
const API = 'https://api.ignat.ai/presence/api'

const now = await fetch(`${API}/now`).then(r => r.json())
console.log(now.main.label)                       // "正在上《最优化方法》"

const es = new EventSource(`${API}/stream`)
es.addEventListener('now', e => render(JSON.parse(e.data)))

Python

python
import json, urllib.request

API = "https://api.ignat.ai/presence/api"
with urllib.request.urlopen(f"{API}/today") as r:
    day = json.load(r)
for cat, sec in day["totals"]["activity"].items():
    print(day["names"][cat], round(sec / 60), "min")

GET/now

此刻的状态:一个主状态 main 和若干伴随状态 side。服务器每 2 秒推进一次状态。

The current status: one main item plus any side items. The server advances its state every 2 seconds.

响应 200Response 200

json
{
  "main": {
    "type": "anchor", "category": "class", "label": "正在上《最优化方法》",
    "since": 1793008800.0, "until": 1793017200.0,
    "slacking": { "since": 1793014014.68 }
  },
  "side": [
    { "type": "activity", "category": "dev", "label": "搞开发", "tier": 1,
      "since": 1793014014.68, "detail": "Presence", "today_sec": 20491 }
  ],
  "updated_at": 1793014738.57
}

状态项字段Item fields

除 type、category、label 外,其余字段只在有值时出现。

Apart from type, category and label, fields appear only when they have a value.

字段Field类型Type说明Description
typestringanchor 锚定状态、activity 活动、page 做正事时顺带看的网页、idle 没有任何状态anchor, activity, page (a web page viewed during serious work) or idle (nothing going on)
categorystring类别,见类别;idle 时为 away(设备在线但没在做事)或 offlineSee Categories; for idle it is away (devices online but nothing active) or offline
labelstring直接展示的中文文案Ready-to-show Chinese text
since / untilnumber开始时间;预计结束时间(上课时为下课时间)Start time; expected end (end of class)
tiernumber活动等级,越小越优先Activity tier, lower wins
detailstring补充说明,例如科研、开发时的项目名Extra detail, e.g. the project name during research or dev
slackingobject{since}:从什么时候开始摸鱼{since}: when slacking started
today_secnumber该活动今天累计的秒数(等级 1 的活动,或合并进主状态的活动)Seconds spent on this activity today (tier-1 activities, or one folded into the main status)
activity / activity_namestring锚定状态合并进来的活动及其简称,例如在办公室时的“科研”Activity folded into an anchor and its short name, e.g. research while at the office
app / iconstring刷手机时的前台 App 名;now.ignat.ai 上的本地图标名(/icons/apps/<icon>.png)Foreground phone app; local icon name on now.ignat.ai (/icons/apps/<icon>.png)
cardobject详情卡片,见下表Detail card, see below

卡片 cardCards

出现于On字段Fields
听歌 musicmusicplayer player_name title artist album state position duration cover updated_at session_since
看视频 videovideosite site_name id page title owner url state position duration rate viewing cover updated_at session_since
游戏 gamegamename appid since cover
网页(kind: "web")Web page (kind: "web")title domain cover
  • position / duration 是秒,position 是 updated_at 那一刻的进度;播放中(state: "playing")可按经过的时间自行推算当前进度。
  • session_since 是连续听歌 / 看视频的起点,换歌、换视频不重置。
  • cover 是完整的图片地址,指向 /media/{sha256}。视频标题、封面等是服务器异步查到后补上的,刚开始时可能只有进度。
  • position / duration are seconds; position is the progress at updated_at. While state is "playing", extrapolate from elapsed time.
  • session_since marks the start of a continuous listening / watching session; switching tracks or videos does not reset it.
  • cover is a full image URL pointing to /media/{sha256}. Video titles and covers are looked up asynchronously, so early on a card may only have progress.

GET/stream

Server-Sent Events 事件流。连上后立即推送一次当前状态,之后每当状态变化就再推送一次。数据格式与 /now 完全相同。

A Server-Sent Events stream. It sends the current status right after connecting and again whenever it changes. The data is identical to /now.

event-stream
event: now
data: {"main": {"type": "anchor", "category": "class", …}, "side": [ … ], "updated_at": 1793014771.6}

: ping
  • 事件名固定为 now,用 addEventListener('now', …) 接收(不是 onmessage)。
  • 空闲时每 25 秒发一行 : ping 注释保持连接。
  • 全站同时最多 200 条连接,满了返回 503。服务器重启时会主动断开,浏览器的 EventSource 会自动重连。
  • The event name is always now; listen with addEventListener('now', …), not onmessage.
  • A : ping comment every 25 idle seconds keeps the connection open.
  • At most 200 streams site-wide; beyond that you get 503. The server closes streams on restart and EventSource reconnects on its own.

GET/today

今天(凌晨 4 点起)的主状态时间线和各类别时长。

Today's (from 4 a.m.) main-status timeline and per-category totals.

响应 200Response 200

json
{
  "date": "2026-10-26",
  "start": 1792958400.0, "end": 1793044800.0,
  "timeline": [
    { "start": 1792958400.0, "end": 1792976496.3, "category": "sleep", "label": "睡觉" },
    { "start": 1792994400.0, "end": 1793000400.0, "category": "class", "label": "卫星控制系统建模仿真与数字孪生技术" }
  ],
  "totals": {
    "anchor":   { "sleep": 18096, "class": 11755 },
    "activity": { "dev": 20492, "research": 5984, "music": 438 }
  },
  "names": { "sleep": "睡觉", "class": "上课", "dev": "开发", "research": "科研", "music": "听歌" },
  "updated_at": 1793014739.71
}
字段Field说明Description
start / end这一天的起止时间戳Bounds of the day
timeline主状态色块,按时间排序、互不重叠;没有任何状态的时段不出现。今天最后一块的 end 是当前时间。上课的 label 是课程名Main-status blocks, sorted and non-overlapping; gaps with no status are left out. Today's last block ends at the current time. For class, label is the course name
totals每条线各类别的秒数。不同类别可以重叠(边听歌边开发),所以加起来可能超过 24 小时;正事里摸鱼的时间不计入Seconds per category on each line. Categories may overlap (music while coding), so sums can exceed 24 h; slacking time inside serious work is excluded
names本次出现的类别的中文简称Chinese short names of the categories present

GET/day

任意一天的公开时间线和时长,格式与 /today 相同。

Any day's public timeline and totals, in the same shape as /today.

查询参数Query

参数Param说明Description
date必填,YYYY-MM-DDRequired, YYYY-MM-DD
shell
curl 'https://api.ignat.ai/presence/api/day?date=2026-10-25'

缺少 date 返回 422,格式不对返回 400。没有记录的日子返回空的 timeline 和 totals。

A missing date returns 422; a malformed one returns 400. Days without records return an empty timeline and totals.

GET/history

一段时间内每天的各类别时长和正事时长,以及区间内的游戏榜、听歌榜。不含项目名、网站和视频。

Daily per-category totals and serious-work time over a range, plus game and music charts for the range. No project names, sites or videos.

查询参数Query

参数Param说明Description
from optional开始日期(含)。不写时为 to 往前 182 天(约半年)First day (inclusive). Defaults to 182 days before to (about six months)
to optional结束日期(含)。不写时为今天Last day (inclusive). Defaults to today

一次最多 400 天;from 晚于 to 或超过上限返回 400。days 只列到今天为止,将来的日期不出现。

At most 400 days per call; from after to or a longer range returns 400. days stops at today; future dates are omitted.

响应 200Response 200

json
{
  "from": "2026-10-24", "to": "2026-10-26",
  "days": [
    { "date": "2026-10-24", "totals": { "anchor": {}, "activity": {} }, "serious_sec": 0 },
    { "date": "2026-10-26",
      "totals": { "anchor": { "sleep": 18096 }, "activity": { "dev": 20493, "research": 5984 } },
      "serious_sec": 26477 }
  ],
  "games": [],
  "music": [
    { "key": "Smile. DK - Butterfly", "title": "Butterfly", "artist": "Smile. DK", "seconds": 174, "plays": 2 }
  ],
  "names": { "sleep": "睡觉", "dev": "开发", "research": "科研" },
  "updated_at": 1793014741.0
}
  • serious_sec:当天实际在做正事(科研、开发)的秒数,几种正事重叠只算一次,不含摸鱼。
  • games / music:按时长从多到少,各最多 10 项。plays 是播放次数,暂停后继续算同一次。
  • serious_sec: seconds of actual serious work (research, dev) that day; overlaps count once and slacking is excluded.
  • games / music: sorted by time, top 10 each. plays counts plays; resuming after a pause is the same play.

GET/media/{sha256}

封面、网站图标等图片。地址里是图片内容的 SHA-256(64 位小写十六进制),一般直接使用卡片里的 cover 地址即可,不需要自己拼。

Cover art, site icons and other images. The path is the SHA-256 of the content (64 lowercase hex chars); normally just use the cover URL from a card.

  • 返回 JPEG、PNG、WebP 或 SVG 图片,带 Cache-Control: public, max-age=31536000, immutable:内容不会变,可以永久缓存。
  • 不存在或地址格式不对时返回 404。
  • Returns a JPEG, PNG, WebP or SVG image with Cache-Control: public, max-age=31536000, immutable; the content never changes, so cache it forever.
  • Unknown or malformed IDs return 404.

GET/health

检查服务是否在运行。

Checks that the service is up.

json
{ "ok": true, "time": 1793014740.45, "streams": 0 }

time 是服务器当前时间,streams 是当前打开的 /stream 连接数。服务器没运行时返回 502。

time is the server clock; streams is the number of open /stream connections. When the server is down you get 502.

私有接口:访问方式Private endpoints: access

  • 需要先在 401.ignat.ai 登录,请求要带上登录 Cookie;浏览器里写 fetch(url, { credentials: 'include' }),并且只接受来自 https://now.ignat.ai 的跨域请求。
  • 未登录返回 401:{"error": "unauthorized", "login": "https://401.ignat.ai/"},可以把用户带到 login 地址登录。
  • 绕过登录直接访问后端时,私有接口一律表现为不存在(404)。
  • Sign in at 401.ignat.ai first and send the session cookie: in a browser use fetch(url, { credentials: 'include' }). Cross-origin calls are accepted only from https://now.ignat.ai.
  • Without a session you get 401: {"error": "unauthorized", "login": "https://401.ignat.ai/"}; send the user to login to sign in.
  • Requests that bypass the login gate see private endpoints as nonexistent (404).
browser · now.ignat.ai
const r = await fetch('https://api.ignat.ai/presence/api/private/day?date=2026-10-26',
                      { credentials: 'include' })
if (r.status === 401) location.href = (await r.json()).login

日期参数的写法、默认值和 400 天上限与 /history 相同。

Date parameters follow the same rules, defaults aside, and 400-day cap as /history.

GET/private/day

某一天的完整记录。参数 date 可选,默认今天。

The full record of one day. date is optional and defaults to today.

json
{
  "date": "2026-10-26", "start": 1792958400.0, "end": 1793044800.0,
  "segments": [
    { "id": "…", "line": "activity", "category": "dev", "start": …, "end": …,
      "tier": 1, "project": "Presence", "source": "…", "slacking": false }
  ],
  "timeline": [ /* same as /today, private records included */ ],
  "totals": { "anchor": { … }, "activity": { … } },
  "media": [
    { "type": "music", "key": "…", "title": "…", "artist": "…", "player": "…",
      "start": …, "end": …, "seconds": 189, "private": false }
  ],
  "web": [ { "domain": "github.com", "title": "…", "start": …, "end": …, "private": false } ],
  "names": { /* every category */ },
  "updated_at": …
}
  • segments:两条线上的全部原始片段,可能带 tier、detail、project、course、domain、source(上报设备),私有片段带 private: true。
  • media:播放记录,type 为 video、music、game、app(手机 App),另可能带 album、page、source。
  • web:浏览记录。
  • segments: every raw segment on both lines, possibly with tier, detail, project, course, domain and source (reporting device); private ones carry private: true.
  • media: playback records with type video, music, game or app (phone apps), possibly with album, page, source.
  • web: browsing records.

GET/private/summary

区间汇总排行。

Ranked totals over a range.

参数Param说明Description
from / to optional默认最近 7 天Defaults to the last 7 days
group optional按什么排:category(默认)、project、site、video、track、game、appGroup by category (default), project, site, video, track, game or app
during optional锚定状态,例如 class:只统计那段时间里的活动,回答“上课时在干什么”An anchor such as class: count only what happened during it (“what was I doing in class?”)
json
{
  "from": "2026-10-20", "to": "2026-10-26", "group": "category", "during": null,
  "items": [
    { "key": "dev", "line": "activity", "title": "开发", "seconds": 20533 }
  ],
  "updated_at": …
}

items 按时长从多到少。line 只在 category 分组时出现;site 分组另带 visits(访问次数);媒体分组(video、track、game、app)的项与 /history 的榜单相同(title、artist、plays),且不限 10 项。

items are sorted by time. line appears only for category; site adds visits; media groups (video, track, game, app) return the same items as the /history charts (title, artist, plays) with no top-10 cut.

GET/private/focus

专注度。参数 from / to,默认最近 7 天。

Focus stats. from / to, defaulting to the last 7 days.

json
{
  "from": "2026-10-20", "to": "2026-10-26",
  "days": [
    { "date": "2026-10-26", "focus_sec": 26477, "longest_sec": 5400, "longest_start": …,
      "interruptions": 6, "slack_sec": 1320 }
  ],
  "channels": [ { "category": "phone", "title": "刷手机", "seconds": 900 } ],
  "updated_at": …
}
  • focus_sec 正事时长;longest_sec / longest_start 最长一段连续专注;interruptions / slack_sec 摸鱼次数和时长。
  • channels:摸鱼时主要在做什么(网站按名字、游戏按名字),最多 10 项;后台听歌不算。
  • focus_sec is serious-work time; longest_sec / longest_start the longest unbroken stretch; interruptions / slack_sec the count and length of slacking.
  • channels: what slacking time went to (sites and games by name), top 10; background music does not count.

GET/private/export

导出区间内的全部记录,作为附件下载(presence-开始_结束.json / .csv)。

Exports every record in the range as a download (presence-FROM_TO.json / .csv).

参数Param说明Description
from / to optional默认今天一天Defaults to today only
format optionaljson(默认):片段、播放记录、浏览记录和时区;csv:只有片段,带 BOM,Excel 直接打开中文不乱码json (default): segments, playback, browsing and the time zone; csv: segments only, with a BOM so Excel shows Chinese correctly

每个片段另带 start_local / end_local(本地时间 ISO 8601)和 seconds。

Each segment also has start_local / end_local (local ISO 8601) and seconds.

类别Categories

当前的类别如下(可能增加)。显示名请以响应里的 names 为准。

Current categories (more may be added). Use names from the response for display.

category线Line简称Meaning说明Notes
customanchor / activity自定义Custom文案自定,等级可设Free text, tier configurable
classanchor上课Class来自课表,带课程名From the timetable, with course name
eatanchor吃饭Eating—
sleepanchor睡觉Sleeping—
officeanchor办公室Office合并科研Folds in research
researchactivity · tier 1科研Research正事,公开项目名Serious; project name public
devactivity · tier 1开发Dev正事,公开项目名Serious; project name public
gameactivity · tier 2游戏Gaming带游戏名With game name
outactivity · tier 2出去玩Out—
videoactivity · tier 3看视频Video—
browseactivity · tier 3上网Browsing—
phoneactivity · tier 4刷手机Phone带 App 名With app name
musicactivity · tier 4听歌Music—
away / offlineidle暂时离开 / 离线Away / offline只出现在 /nowOnly in /now

错误Errors

错误响应是 JSON:{"detail": "..."},detail 是中文说明;参数缺失时 detail 是一个数组。

Errors are JSON: {"detail": "..."} with a Chinese message; for missing parameters detail is an array.

状态码Status含义Meaning
400日期格式不对、开始晚于结束、超过 400 天、group / during / format 取值不对Bad date, from after to, over 400 days, or an invalid group / during / format
401私有接口未登录,见访问方式Private endpoint without a session; see Access
404路径或图片不存在Unknown path or image
422缺少必填参数(/day 的 date)Missing required parameter (date on /day)
502服务器没在运行The server is not running
503/stream 连接数已满/stream is full