Surf Studio commited on
Commit
7b0d635
·
1 Parent(s): 85623cd

新鲜度修复:轮询全部已知分享者(不再只查前 10 个);t.co 网页跳转解析;无链接的送会员帖也收录;Telegram 频道读取修复

Browse files
Files changed (2) hide show
  1. lib/mystery.js +57 -17
  2. lib/telegramUser.js +11 -2
lib/mystery.js CHANGED
@@ -48,10 +48,12 @@ const TCO_RE = /https:\/\/t\.co\/[A-Za-z0-9]+/g
48
  const GIFT_LINK_RE = /asksurf\.ai\/mystery-box\/g\/([A-Za-z0-9]+)/i
49
  const SURF_BOX_RE = /asksurf\.ai\/mystery-box/i
50
  const BOX_TALK_RE = /mystery\s*box|mystery-box|盲盒/i
 
 
51
 
52
  const MAX_NEW_POSTS = 40
53
  const MAX_RELINK_CHECKS = 12
54
- const MAX_TIMELINE_HANDLES = 10
55
  const MAX_INVITE_CHECKS = 40
56
  const LINK_CONCURRENCY = 6
57
 
@@ -89,6 +91,13 @@ async function resolveTco(url) {
89
  const location = res.headers.get('location')
90
  if (location) return location
91
  if (res.url && res.url !== url) return res.url
 
 
 
 
 
 
 
92
  return null
93
  } catch {
94
  return null
@@ -253,28 +262,58 @@ async function timelineToRow(tweet, handle, { resolveLinks = true } = {}) {
253
  }
254
  }
255
 
256
- /** Sharers that recently posted gift links first, then the seed list. */
 
 
 
 
 
 
 
257
  async function timelineHandles() {
 
 
 
 
 
 
 
 
 
 
258
  const ordered = []
259
  const seen = new Set()
 
 
 
 
 
 
260
 
261
- const { rows } = await dbQuery(
262
- `SELECT handle, MAX(created_at) AS last_at FROM mystery_posts
263
- WHERE has_link = true AND handle IS NOT NULL
264
- GROUP BY handle ORDER BY last_at DESC LIMIT 20`
265
- )
266
  for (const row of rows) {
267
- const handle = String(row.handle || '').toLowerCase()
268
- if (!handle || seen.has(handle)) continue
269
- seen.add(handle)
270
- ordered.push(handle)
271
  }
272
- for (const handle of SEED_HANDLES) {
273
- const lower = handle.toLowerCase()
274
- if (seen.has(lower)) continue
275
- seen.add(lower)
276
- ordered.push(lower)
 
 
 
 
 
 
 
 
 
 
277
  }
 
278
  return ordered.slice(0, MAX_TIMELINE_HANDLES)
279
  }
280
 
@@ -354,7 +393,8 @@ async function syncTimelines(existing, errors) {
354
  const relevant =
355
  row.has_link ||
356
  SURF_BOX_RE.test(row.text_content) ||
357
- (BOX_TALK_RE.test(row.text_content) && row.links.length > 0)
 
358
  if (!relevant) continue
359
  if (row.has_link) withLink += 1
360
  if (!known) added += 1
 
48
  const GIFT_LINK_RE = /asksurf\.ai\/mystery-box\/g\/([A-Za-z0-9]+)/i
49
  const SURF_BOX_RE = /asksurf\.ai\/mystery-box/i
50
  const BOX_TALK_RE = /mystery\s*box|mystery-box|盲盒/i
51
+ /** "今天送了 142 份 Surf Pro 会员" style posts — no link yet, but worth showing. */
52
+ const GIFT_TALK_RE = /surf[^。!\n]{0,24}(会员|盲盒|mystery\s*box|pro\s*会员)|(送|赠|免费领|白嫖|自取)[^。!\n]{0,20}(surf|会员|pro|盲盒)/i
53
 
54
  const MAX_NEW_POSTS = 40
55
  const MAX_RELINK_CHECKS = 12
56
+ const MAX_TIMELINE_HANDLES = 14
57
  const MAX_INVITE_CHECKS = 40
58
  const LINK_CONCURRENCY = 6
59
 
 
91
  const location = res.headers.get('location')
92
  if (location) return location
93
  if (res.url && res.url !== url) return res.url
94
+
95
+ // t.co often answers 200 with a tiny HTML hop page instead of a 3xx.
96
+ const body = await res.text().catch(() => '')
97
+ const viaScript = /location\.replace\("([^"]+)"/.exec(body)?.[1]
98
+ const viaMeta = /URL=([^">\s]+)/i.exec(body)?.[1]
99
+ const target = viaScript || viaMeta
100
+ if (target) return target.replace(/\\\//g, '/')
101
  return null
102
  } catch {
103
  return null
 
262
  }
263
  }
264
 
265
+ /**
266
+ * Which sharers to poll this round.
267
+ *
268
+ * Search results lag behind the live timeline by hours, so profile polling is
269
+ * the freshness lifeline: authors who posted recently are always checked, and
270
+ * everyone else is polled on a rotating cursor so a growing list of sharers
271
+ * still gets full coverage.
272
+ */
273
  async function timelineHandles() {
274
+ const { rows } = await dbQuery(
275
+ `SELECT handle, MAX(created_at) AS last_at, COUNT(*) AS posts
276
+ FROM mystery_posts
277
+ WHERE handle IS NOT NULL
278
+ GROUP BY handle
279
+ ORDER BY MAX(created_at) DESC
280
+ LIMIT 80`
281
+ )
282
+
283
+ const now = Math.floor(Date.now() / 1000)
284
  const ordered = []
285
  const seen = new Set()
286
+ const push = (handle) => {
287
+ const lower = String(handle || '').toLowerCase().replace(/^@/, '')
288
+ if (!lower || seen.has(lower)) return
289
+ seen.add(lower)
290
+ ordered.push(lower)
291
+ }
292
 
293
+ // Always poll authors active in the last 6 hours — that is where new links appear.
294
+ const active = []
295
+ const rest = []
 
 
296
  for (const row of rows) {
297
+ if (now - Number(row.last_at || 0) <= 6 * 3600) active.push(row.handle)
298
+ else rest.push(row.handle)
 
 
299
  }
300
+ active.forEach(push)
301
+
302
+ const seeds = SEED_HANDLES.filter((handle) => !seen.has(String(handle).toLowerCase()))
303
+ const rotating = [...rest, ...seeds]
304
+
305
+ if (rotating.length > 0) {
306
+ const cursor = Number(await getSetting('timeline_cursor')) || 0
307
+ const start = cursor % rotating.length
308
+ const room = Math.max(0, MAX_TIMELINE_HANDLES - ordered.length)
309
+ const picked = []
310
+ for (let i = 0; i < rotating.length && picked.length < room; i += 1) {
311
+ picked.push(rotating[(start + i) % rotating.length])
312
+ }
313
+ picked.forEach(push)
314
+ await setSetting('timeline_cursor', (start + picked.length) % Math.max(rotating.length, 1))
315
  }
316
+
317
  return ordered.slice(0, MAX_TIMELINE_HANDLES)
318
  }
319
 
 
393
  const relevant =
394
  row.has_link ||
395
  SURF_BOX_RE.test(row.text_content) ||
396
+ (BOX_TALK_RE.test(row.text_content) && row.links.length > 0) ||
397
+ GIFT_TALK_RE.test(row.text_content)
398
  if (!relevant) continue
399
  if (row.has_link) withLink += 1
400
  if (!known) added += 1
lib/telegramUser.js CHANGED
@@ -22,6 +22,8 @@ const MESSAGES_PER_CHANNEL = 50
22
  const GIFT_LINK_RE = /asksurf\.ai\/mystery-box\/g\/([A-Za-z0-9]+)/i
23
  const SURF_BOX_RE = /asksurf\.ai\/mystery-box/i
24
  const BOX_TALK_RE = /mystery\s*box|mystery-box|盲盒/i
 
 
25
  const URL_RE = /https?:\/\/[^\s<>()"']+/g
26
 
27
  // GramJS's internal ping loop can reject with a bare TIMEOUT while a client is
@@ -421,7 +423,8 @@ function messageToRow(message, channel) {
421
  function isRelevant(row) {
422
  if (row.has_link) return true
423
  if (SURF_BOX_RE.test(row.text_content)) return true
424
- return BOX_TALK_RE.test(row.text_content) && row.links.length > 0
 
425
  }
426
 
427
  /** Read every configured channel with the user session and store the posts. */
@@ -463,7 +466,13 @@ async function sync({ upsert, upsertParams }) {
463
  {
464
  for (const channel of list) {
465
  try {
466
- const entity = await c.getEntity(channel.ref || channel.id)
 
 
 
 
 
 
467
  const messages = await c.getMessages(entity, { limit: MESSAGES_PER_CHANNEL })
468
  scanned += messages.length
469
  for (const message of messages) {
 
22
  const GIFT_LINK_RE = /asksurf\.ai\/mystery-box\/g\/([A-Za-z0-9]+)/i
23
  const SURF_BOX_RE = /asksurf\.ai\/mystery-box/i
24
  const BOX_TALK_RE = /mystery\s*box|mystery-box|盲盒/i
25
+ /** "今天送了 142 份 Surf Pro 会员" style posts — no link, but worth showing. */
26
+ const GIFT_TALK_RE = /surf[^。!\n]{0,24}(会员|盲盒|mystery\s*box|pro\s*会员)|(送|赠|免费领|白嫖|自取)[^。!\n]{0,20}(surf|会员|pro|盲盒)/i
27
  const URL_RE = /https?:\/\/[^\s<>()"']+/g
28
 
29
  // GramJS's internal ping loop can reject with a bare TIMEOUT while a client is
 
423
  function isRelevant(row) {
424
  if (row.has_link) return true
425
  if (SURF_BOX_RE.test(row.text_content)) return true
426
+ if (BOX_TALK_RE.test(row.text_content) && row.links.length > 0) return true
427
+ return GIFT_TALK_RE.test(row.text_content)
428
  }
429
 
430
  /** Read every configured channel with the user session and store the posts. */
 
466
  {
467
  for (const channel of list) {
468
  try {
469
+ // Older entries only carry a username, newer ones a ref/id — accept all.
470
+ const target = channel.ref || channel.id || channel.username
471
+ if (!target) {
472
+ errors.push(`${channel.title || '未命名频道'}: 频道信息不完整,请删除后重新添加`)
473
+ continue
474
+ }
475
+ const entity = await c.getEntity(target)
476
  const messages = await c.getMessages(entity, { limit: MESSAGES_PER_CHANNEL })
477
  scanned += messages.length
478
  for (const message of messages) {