okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-split-int (a3024ed10) into f-split-qa1: qa2's s3237-s3238 kept, my 21 strings renumbered s3260-s32808667a3dbf

main
3.4 KiB68 linesraw
1/**
2 * The second keyless source for the agent's web search: Bing's plain result page, used only when DuckDuckGo refuses.
3 *
4 * It is read the way a browser reads it (an ordinary User-Agent, the page's own request shape, nothing hidden) and is held to the same
5 * manners as DuckDuckGo (see web-search-ddg.mjs): one request at a time, spaced out, a cooldown after a refusal. Results are not ranked or
6 * filtered here. Pure parsing lives in this file so it can be tested on saved pages.
7 */
8
9const ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ', '#39': "'", '#x27': "'", '#x2F': '/', '#183': '·' };
10const decode = text => String(text).replace(/&(#x?[0-9a-f]+|[a-z]+);/gi, (whole, key) => {
11  if (key[0] === '#') {
12    const code = key[1].toLowerCase() === 'x' ? parseInt(key.slice(2), 16) : parseInt(key.slice(1), 10);
13    return Number.isFinite(code) && code > 0 && code < 0x110000 ? String.fromCodePoint(code) : whole;
14  }
15  return ENTITIES[key.toLowerCase()] ?? whole;
16});
17const plain = html => decode(String(html).replace(/<[^>]*>/g, '')).replace(/\s+/g, ' ').trim();
18
19export const BING_URL = 'https://www.bing.com/search';
20
21/** Bing wraps every result link in its own redirect (`/ck/a?...&u=a1<base64url of the real address>`). */
22export function bingTarget(href) {
23  const value = decode(href).trim();
24  try {
25    const url = new URL(value, 'https://www.bing.com/');
26    if (/(^|\.)bing\.com$/i.test(url.hostname)) {
27      const u = url.searchParams.get('u');
28      if (u && /^a1/.test(u)) {
29        const target = Buffer.from(u.slice(2).replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8');
30        const real = new URL(target);
31        return /^https?:$/.test(real.protocol) ? real.href : null;
32      }
33      return null;                                       // Bing's own page (images, maps, "more results")
34    }
35    return /^https?:$/.test(url.protocol) ? url.href : null;
36  } catch { return null; }
37}
38
39/** The result list of the page. Tolerant: a result needs a link and a title; the snippet is optional. */
40export function parseBingResults(html) {
41  const out = [];
42  const seen = new Set();
43  const blocks = String(html).split(/<li\b[^>]*\bclass="[^"]*\bb_algo\b[^"]*"/i).slice(1);
44  for (const block of blocks) {
45    const end = block.search(/<li\b[^>]*\bclass="[^"]*\bb_(?:algo|ans|ad|pag)\b/i);
46    const body = end > 0 ? block.slice(0, end) : block;
47    const h2 = /<h2\b[^>]*>\s*<a\b([^>]*)>([\s\S]*?)<\/a>/i.exec(body);
48    if (!h2) continue;
49    const href = /\bhref="([^"]*)"/i.exec(h2[1])?.[1];
50    const url = href ? bingTarget(href) : null;
51    const title = plain(h2[2]);
52    if (!url || !title || seen.has(url)) continue;
53    const snippetMatch = /<p\b[^>]*class="[^"]*\bb_lineclamp\d*\b[^"]*"[^>]*>([\s\S]*?)<\/p>/i.exec(body)
54      ?? /<div\b[^>]*class="[^"]*\bb_caption\b[^"]*"[^>]*>[\s\S]*?<p\b[^>]*>([\s\S]*?)<\/p>/i.exec(body);
55    const snippet = snippetMatch ? plain(snippetMatch[1]) : '';
56    seen.add(url);
57    out.push({ url, title, ...(snippet ? { snippet } : {}) });
58  }
59  return out;
60}
61
62/** Bing serves a challenge page, or a refusal, instead of results. A page with results is never "blocked". */
63export function bingLooksBlocked(status, html) {
64  if (status === 403 || status === 429 || status === 503) return true;
65  const text = String(html).slice(0, 80000);
66  if (/\bclass="[^"]*\bb_algo\b/.test(text)) return false;
67  return /captcha|unusual traffic|are you a robot|verify you are (?:a )?human|\bcf-chl|challenge-platform/i.test(text);
68}