okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8667a3dbf
main
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 ) => { 11if ( key [ 0 ] === '#' ) { 12const code = key [ 1 ]. toLowerCase () === 'x' ?parseInt ( key . slice ( 2 ), 16 ) :parseInt ( key . slice ( 1 ), 10 ); 13return Number . isFinite ( code ) && code > 0 && code < 0x110000 ?String . fromCodePoint ( code ) :whole ; 14} 15return 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 ) { 23const value = decode ( href ). trim (); 24try { 25const url = new URL ( value , 'https://www.bing.com/' ); 26if ( / ( ^ | \. ) bing\.com$ / i . test ( url . hostname )) { 27const u = url . searchParams . get ( 'u' ); 28if ( u && / ^a1 / . test ( u )) { 29const target = Buffer . from ( u . slice ( 2 ). replace ( / - / g , '+' ). replace ( / _ / g , '/' ), 'base64' ). toString ( 'utf8' ); 30const real = new URL ( target ); 31return / ^https ? :$ / . test ( real . protocol ) ?real . href :null ; 32} 33return null ; // Bing's own page (images, maps, "more results") 34} 35return / ^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 ) { 41const out = []; 42const seen = new Set (); 43const blocks = String ( html ). split ( / <li\b [ ^ > ] * \bclass=" [ ^ " ] * \bb_algo\b [ ^ " ] * " / i ). slice ( 1 ); 44for ( const block of blocks ) { 45const end = block . search ( / <li\b [ ^ > ] * \bclass=" [ ^ " ] * \bb_ (?: algo | ans | ad | pag ) \b / i ); 46const body = end > 0 ?block . slice ( 0 , end ) :block ; 47const h2 = / <h2\b [ ^ > ] * >\s * <a\b ([ ^ > ] * ) > ([ \s\S ] * ? ) <\/a> / i . exec ( body ); 48if ( ! h2 ) continue ; 49const href = / \bhref=" ([ ^ " ] * ) " / i . exec ( h2 [ 1 ])?.[ 1 ]; 50const url = href ?bingTarget ( href ) :null ; 51const title = plain ( h2 [ 2 ]); 52if ( ! url || ! title || seen . has ( url )) continue ; 53const 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 ); 55const snippet = snippetMatch ?plain ( snippetMatch [ 1 ]) :'' ; 56seen . add ( url ); 57out . push ({ url, title, ...( snippet ?{ snippet} :{}) }); 58} 59return 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 ) { 64if ( status === 403 || status === 429 || status === 503 ) return true ; 65const text = String ( html ). slice ( 0 , 80000 ); 66if ( / \bclass=" [ ^ " ] * \bb_algo\b / . test ( text )) return false ; 67return / captcha | unusual traffic | are you a robot | verify you are (?: a ) ? human | \bcf-chl | challenge-platform / i . test ( text ); 68}