Ethscriptions commited on
Commit
86ce5a4
·
verified ·
1 Parent(s): de08b06

账号优先读取,发现更快

Browse files
Files changed (1) hide show
  1. server.js +39 -30
server.js CHANGED
@@ -82793,6 +82793,13 @@ var hasAccountFetcher = () => accountFetcher != null;
82793
  async function fetchPage(channel, before, f = fetch) {
82794
  const key = channel.toLowerCase();
82795
  if (accountFetcher && accountOnly.has(key)) return accountFetcher(channel, before);
 
 
 
 
 
 
 
82796
  let page = null;
82797
  try {
82798
  page = await fetchWebPage(channel, before, f);
@@ -82800,9 +82807,9 @@ async function fetchPage(channel, before, f = fetch) {
82800
  if (!accountFetcher) throw e;
82801
  }
82802
  if (accountFetcher && (!page || !page.isChannelPreview || before == null && page.posts.length === 0)) {
82803
- const viaAccount = await accountFetcher(channel, before);
82804
  accountOnly.add(key);
82805
- return viaAccount;
82806
  }
82807
  if (serverSide && page && !page.isChannelPreview) throw new Error("\u8FD9\u4E2A\u9891\u9053\u5173\u95ED\u4E86\u7F51\u9875\u9884\u89C8\u3002\u70B9\u53F3\u4E0A\u89D2\u300CTelegram \u8D26\u53F7\u300D\u767B\u5F55\u540E\u5C31\u80FD\u6293\u53D6");
82808
  return page;
@@ -83524,6 +83531,7 @@ function toPost(m) {
83524
  char_count: [...text].length
83525
  };
83526
  }
 
83527
  async function fetchViaAccount(channel, before) {
83528
  const c = client;
83529
  if (!c) throw new Error("Telegram \u8D26\u53F7\u672A\u767B\u5F55");
@@ -83538,7 +83546,11 @@ async function fetchViaAccount(channel, before) {
83538
  let title = channel;
83539
  let description = "";
83540
  let avatar = "";
83541
- if (before == null) {
 
 
 
 
83542
  await throttle();
83543
  const ent = await c.getEntity(peer);
83544
  if (ent instanceof import_telegram.Api.Channel) {
@@ -83558,6 +83570,7 @@ async function fetchViaAccount(channel, before) {
83558
  } else if (ent instanceof import_telegram.Api.Chat) {
83559
  title = ent.title;
83560
  }
 
83561
  }
83562
  let offsetId = before ?? 0;
83563
  for (let round = 0; round < 10; round++) {
@@ -83872,27 +83885,18 @@ function judge(texts) {
83872
  samples
83873
  };
83874
  }
83875
- async function recentTexts(c) {
83876
- if (c.kind !== "group") {
83877
- try {
83878
- const p1 = await fetchWebPage(c.username, null);
83879
- if (p1.isChannelPreview && p1.posts.length) {
83880
- let posts = p1.posts;
83881
- if (posts.length < 30) {
83882
- const min = Math.min(...posts.map((p) => p.post_id));
83883
- if (min > 1) posts = posts.concat((await fetchWebPage(c.username, min).catch(() => null))?.posts ?? []);
83884
- }
83885
- posts.sort((a, b) => b.post_id - a.post_id);
83886
- return {
83887
- texts: posts.slice(0, 30).map((p) => p.text || ""),
83888
- title: p1.title,
83889
- last: posts[0]?.posted_at ?? null,
83890
- kind: "channel"
83891
- };
83892
- }
83893
- } catch {
83894
- }
83895
- }
83896
  const cl = getClient();
83897
  if (!cl) throw new Error("\u9700\u8981\u767B\u5F55 Telegram \u8D26\u53F7");
83898
  await sleep3(2500);
@@ -83902,12 +83906,17 @@ async function recentTexts(c) {
83902
  const texts = msgs.filter((m) => m instanceof import_telegram2.Api.Message && !!m.message?.trim()).slice(0, 30).map((m) => m.message);
83903
  const kind = ent instanceof import_telegram2.Api.Channel ? ent.megagroup || ent.gigagroup ? "group" : "channel" : "group";
83904
  const first = msgs.find((m) => m instanceof import_telegram2.Api.Message);
83905
- return {
83906
- texts,
83907
- title: ent.title,
83908
- last: first ? new Date(first.date * 1e3).toISOString() : null,
83909
- kind
83910
- };
 
 
 
 
 
83911
  }
83912
  async function check(c) {
83913
  try {
 
82793
  async function fetchPage(channel, before, f = fetch) {
82794
  const key = channel.toLowerCase();
82795
  if (accountFetcher && accountOnly.has(key)) return accountFetcher(channel, before);
82796
+ if (accountFetcher && serverSide) {
82797
+ try {
82798
+ return await accountFetcher(channel, before);
82799
+ } catch (e) {
82800
+ if (typeof window === "undefined") console.log(`account fetch ${channel}: ${e.message}, trying the web preview`);
82801
+ }
82802
+ }
82803
  let page = null;
82804
  try {
82805
  page = await fetchWebPage(channel, before, f);
 
82807
  if (!accountFetcher) throw e;
82808
  }
82809
  if (accountFetcher && (!page || !page.isChannelPreview || before == null && page.posts.length === 0)) {
82810
+ const viaAccount2 = await accountFetcher(channel, before);
82811
  accountOnly.add(key);
82812
+ return viaAccount2;
82813
  }
82814
  if (serverSide && page && !page.isChannelPreview) throw new Error("\u8FD9\u4E2A\u9891\u9053\u5173\u95ED\u4E86\u7F51\u9875\u9884\u89C8\u3002\u70B9\u53F3\u4E0A\u89D2\u300CTelegram \u8D26\u53F7\u300D\u767B\u5F55\u540E\u5C31\u80FD\u6293\u53D6");
82815
  return page;
 
83531
  char_count: [...text].length
83532
  };
83533
  }
83534
+ var metaCache = /* @__PURE__ */ new Map();
83535
  async function fetchViaAccount(channel, before) {
83536
  const c = client;
83537
  if (!c) throw new Error("Telegram \u8D26\u53F7\u672A\u767B\u5F55");
 
83546
  let title = channel;
83547
  let description = "";
83548
  let avatar = "";
83549
+ const cached = metaCache.get(key);
83550
+ if (before == null && cached && Date.now() - cached.at < 6 * 36e5) {
83551
+ ;
83552
+ ({ title, description, avatar } = cached);
83553
+ } else if (before == null) {
83554
  await throttle();
83555
  const ent = await c.getEntity(peer);
83556
  if (ent instanceof import_telegram.Api.Channel) {
 
83570
  } else if (ent instanceof import_telegram.Api.Chat) {
83571
  title = ent.title;
83572
  }
83573
+ metaCache.set(key, { title, description, avatar, at: Date.now() });
83574
  }
83575
  let offsetId = before ?? 0;
83576
  for (let round = 0; round < 10; round++) {
 
83885
  samples
83886
  };
83887
  }
83888
+ async function viaWeb(c) {
83889
+ const p1 = await fetchWebPage(c.username, null);
83890
+ if (!p1.isChannelPreview || !p1.posts.length) return null;
83891
+ let posts = p1.posts;
83892
+ if (posts.length < 30) {
83893
+ const min = Math.min(...posts.map((p) => p.post_id));
83894
+ if (min > 1) posts = posts.concat((await fetchWebPage(c.username, min).catch(() => null))?.posts ?? []);
83895
+ }
83896
+ posts.sort((a, b) => b.post_id - a.post_id);
83897
+ return { texts: posts.slice(0, 30).map((p) => p.text || ""), title: p1.title, last: posts[0]?.posted_at ?? null, kind: "channel" };
83898
+ }
83899
+ async function viaAccount(c) {
 
 
 
 
 
 
 
 
 
83900
  const cl = getClient();
83901
  if (!cl) throw new Error("\u9700\u8981\u767B\u5F55 Telegram \u8D26\u53F7");
83902
  await sleep3(2500);
 
83906
  const texts = msgs.filter((m) => m instanceof import_telegram2.Api.Message && !!m.message?.trim()).slice(0, 30).map((m) => m.message);
83907
  const kind = ent instanceof import_telegram2.Api.Channel ? ent.megagroup || ent.gigagroup ? "group" : "channel" : "group";
83908
  const first = msgs.find((m) => m instanceof import_telegram2.Api.Message);
83909
+ return { texts, title: ent.title, last: first ? new Date(first.date * 1e3).toISOString() : null, kind };
83910
+ }
83911
+ async function recentTexts(c) {
83912
+ try {
83913
+ return await viaAccount(c);
83914
+ } catch (e) {
83915
+ if (c.kind === "group") throw e;
83916
+ const w = await viaWeb(c).catch(() => null);
83917
+ if (w) return w;
83918
+ throw e;
83919
+ }
83920
  }
83921
  async function check(c) {
83922
  try {