Spaces:
Sleeping
Sleeping
Surf Studio commited on
Commit ·
45dafaf
1
Parent(s): 5ec9871
推送防重复:改为原子领取 + 失败自动重试;同一赠链只推一次;README 补充定时频率说明
Browse files- README.md +11 -1
- lib/notify.js +24 -9
README.md
CHANGED
|
@@ -20,7 +20,7 @@ pinned: false
|
|
| 20 |
- **Telegram**
|
| 21 |
- 用手机号登录 Telegram 账号(验证码 + 两步验证),可以读取**私有频道 / 私有群**里的赠链消息
|
| 22 |
- 未登录时以公开模式读取公开频道
|
| 23 |
-
- **名额检查**:每 5 分钟
|
| 24 |
- **企业微信推送**:一旦发现有「还有名额」的赠链,立刻推送到群机器人(每个赠链只推一次)
|
| 25 |
- **界面**:收录列表、来源筛选(X / Telegram)、还有名额的赠链一键复制、倒计时
|
| 26 |
|
|
@@ -35,6 +35,16 @@ pinned: false
|
|
| 35 |
|
| 36 |
X Cookie、Telegram 登录、企业微信地址都保存在数据库里,可以在页面「数据源设置」里随时修改。
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
## 接口
|
| 39 |
|
| 40 |
- `GET /api/tracker` — 收录的帖子与统计
|
|
|
|
| 20 |
- **Telegram**
|
| 21 |
- 用手机号登录 Telegram 账号(验证码 + 两步验证),可以读取**私有频道 / 私有群**里的赠链消息
|
| 22 |
- 未登录时以公开模式读取公开频道
|
| 23 |
+
- **名额检查**:每 5 分钟复查剩余名额(每个码 4 分钟内不重复查,遇到接口限流自动退避 10 分钟)
|
| 24 |
- **企业微信推送**:一旦发现有「还有名额」的赠链,立刻推送到群机器人(每个赠链只推一次)
|
| 25 |
- **界面**:收录列表、来源筛选(X / Telegram)、还有名额的赠链一键复制、倒计时
|
| 26 |
|
|
|
|
| 35 |
|
| 36 |
X Cookie、Telegram 登录、企业微信地址都保存在数据库里,可以在页面「数据源设置」里随时修改。
|
| 37 |
|
| 38 |
+
## 定时频率
|
| 39 |
+
|
| 40 |
+
| 频率 | 做什么 |
|
| 41 |
+
| --- | --- |
|
| 42 |
+
| 每 5 分钟 | 读取 Telegram 新消息、复查赠链名额、发现有效赠链立刻推送 |
|
| 43 |
+
| 每 15 分钟 | 刷新 X 分享者主页 + 实时搜索(3 个关键词) |
|
| 44 |
+
| 每 6 小时 | 全量搜索网页索引 |
|
| 45 |
+
|
| 46 |
+
Telegram 若被限流(FLOOD_WAIT)会自动暂停读取;赠链接口被限流会自动冷却 10 分钟;企业微信只在出现有效赠链时推送,且每个赠链只推一次(多个实例并发也不会重复推送)。
|
| 47 |
+
|
| 48 |
## 接口
|
| 49 |
|
| 50 |
- `GET /api/tracker` — 收录的帖子与统计
|
lib/notify.js
CHANGED
|
@@ -65,22 +65,38 @@ function alertContent(post) {
|
|
| 65 |
return lines.join('\n')
|
| 66 |
}
|
| 67 |
|
| 68 |
-
/** Codes that are usable right now and have never been announced. */
|
| 69 |
async function pendingAlerts() {
|
| 70 |
const { rows } = await dbQuery(
|
| 71 |
-
`SELECT
|
|
|
|
| 72 |
seats_left, box_tier, invite_expires_at
|
| 73 |
FROM mystery_posts
|
| 74 |
WHERE gift_code IS NOT NULL
|
| 75 |
AND notified_at IS NULL
|
| 76 |
AND seats_left > 0
|
| 77 |
AND COALESCE(invite_status, '') <> 'EXPIRED'
|
| 78 |
-
ORDER BY invite_checked_at DESC NULLS LAST
|
| 79 |
LIMIT 5`
|
| 80 |
)
|
| 81 |
return rows
|
| 82 |
}
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
/**
|
| 85 |
* Announce every newly usable gift code. Returns what was pushed so the caller
|
| 86 |
* can surface it in the run summary.
|
|
@@ -91,17 +107,16 @@ async function notifyUsableGifts() {
|
|
| 91 |
const errors = []
|
| 92 |
|
| 93 |
for (const post of pending) {
|
|
|
|
|
|
|
| 94 |
try {
|
| 95 |
await send({ msgtype: 'markdown', markdown: { content: alertContent(post) } })
|
| 96 |
-
await dbQuery('UPDATE mystery_posts SET notified_at = now() WHERE tweet_id = $1', [
|
| 97 |
-
post.tweet_id,
|
| 98 |
-
])
|
| 99 |
pushed.push(post.gift_code)
|
| 100 |
} catch (err) {
|
| 101 |
errors.push(`${post.gift_code}: ${err.message}`)
|
| 102 |
-
//
|
| 103 |
-
await dbQuery('UPDATE mystery_posts SET notified_at =
|
| 104 |
-
post.
|
| 105 |
]).catch(() => {})
|
| 106 |
}
|
| 107 |
}
|
|
|
|
| 65 |
return lines.join('\n')
|
| 66 |
}
|
| 67 |
|
| 68 |
+
/** Codes that are usable right now and have never been announced (one row per code). */
|
| 69 |
async function pendingAlerts() {
|
| 70 |
const { rows } = await dbQuery(
|
| 71 |
+
`SELECT DISTINCT ON (gift_code)
|
| 72 |
+
tweet_id, handle, author_name, text_content, tweet_url, source, gift_code,
|
| 73 |
seats_left, box_tier, invite_expires_at
|
| 74 |
FROM mystery_posts
|
| 75 |
WHERE gift_code IS NOT NULL
|
| 76 |
AND notified_at IS NULL
|
| 77 |
AND seats_left > 0
|
| 78 |
AND COALESCE(invite_status, '') <> 'EXPIRED'
|
| 79 |
+
ORDER BY gift_code, invite_checked_at DESC NULLS LAST
|
| 80 |
LIMIT 5`
|
| 81 |
)
|
| 82 |
return rows
|
| 83 |
}
|
| 84 |
|
| 85 |
+
/**
|
| 86 |
+
* Claim a code before pushing, so two running instances can never announce the
|
| 87 |
+
* same gift twice. Returns true when this instance won the claim.
|
| 88 |
+
*/
|
| 89 |
+
async function claim(post) {
|
| 90 |
+
const { rows } = await dbQuery(
|
| 91 |
+
`UPDATE mystery_posts
|
| 92 |
+
SET notified_at = now()
|
| 93 |
+
WHERE gift_code = $1 AND notified_at IS NULL
|
| 94 |
+
RETURNING tweet_id`,
|
| 95 |
+
[post.gift_code]
|
| 96 |
+
)
|
| 97 |
+
return rows.length > 0
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
/**
|
| 101 |
* Announce every newly usable gift code. Returns what was pushed so the caller
|
| 102 |
* can surface it in the run summary.
|
|
|
|
| 107 |
const errors = []
|
| 108 |
|
| 109 |
for (const post of pending) {
|
| 110 |
+
if (!(await claim(post))) continue
|
| 111 |
+
|
| 112 |
try {
|
| 113 |
await send({ msgtype: 'markdown', markdown: { content: alertContent(post) } })
|
|
|
|
|
|
|
|
|
|
| 114 |
pushed.push(post.gift_code)
|
| 115 |
} catch (err) {
|
| 116 |
errors.push(`${post.gift_code}: ${err.message}`)
|
| 117 |
+
// Release the claim so the next round retries instead of losing the alert.
|
| 118 |
+
await dbQuery('UPDATE mystery_posts SET notified_at = NULL WHERE gift_code = $1', [
|
| 119 |
+
post.gift_code,
|
| 120 |
]).catch(() => {})
|
| 121 |
}
|
| 122 |
}
|