code wiki / _hdl_build / nx_media_extract.nx

nx_media_extract.nx source

↩ module page · 73 lines · 4389 B

1// nx_media_extract.nx -- the INTELLIGENT EXTRACTION ORCHESTRATOR: one callable that runs the discovery layers 2// over a page and returns the BEST media URL, ranked by the SELF-HEALING pattern table (nx_extract_heal: 3// xh_classify / xh_best). Consolidates the layers: (1) raw-HTML URL harvest (JSON-`\/`-aware) -> classify -> 4// catches inline + escaped URLs incl extension-LESS ones (YouTube videoplayback); (2) embedded-state blobs 5// (__NUXT__ / ytInitialPlayerResponse) harvested the same way. Union -> xh_best. Because harvest AND rank both 6// key off the DATA-DRIVEN table (seeded from xt_classify's knowledge, extensible by xh_learn), a URL the system 7// LEARNED from a past failure flows through end-to-end -- the extractor adapts without a code edit. A live 8// caller passes its persisted/healed table to mx_extract_t; mx_extract seeds a fresh one. license_tier: ORIGINAL 9import "nx_syscalls.nx" 10import "nx_media_signal.nx" 11import "nx_media_state.nx" 12import "nx_extract_heal.nx" 13 14// harvest http(s) URLs from `src` (unescape JSON `\/`), classify each via the TABLE, and APPEND the media ones 15// (confidence>0) to `out` at offset `outoff` (newline-sep, deduped). Returns the new offset. 16func mx_harvest(src: *u8, srclen: i64, out: *u8, outoff: i64, cap: i64, table: *i64) -> i64 { 17 let url: *u8 = sys_mmap(4096) 18 let kb: *i64 = sys_mmap(8) as *i64 19 var wp: i64 = outoff 20 var i: i64 = 0 21 while i < srclen { 22 var hit: i64 = 0 23 if xt_find(src, i + 6, "https:" as *u8, 6, i) == i { hit = 1 } 24 if hit != 0 { if xt_find(src, i + 5, "http:" as *u8, 5, i) == i { hit = 1 } } 25 if hit == 1 { 26 var k: i64 = 0 27 var j: i64 = i 28 var scan: i64 = 1 29 while scan == 1 { 30 if j >= srclen { scan = 0 } 31 else { 32 let c: i64 = src[j] & 0xff 33 if c == 92 { if (j + 1) < srclen { if (src[j+1]&0xff) == 47 { if k<4095 { url[k]=47 as u8; k=k+1 } j=j+2 } else { scan=0 } } else { scan=0 } } 34 else { 35 if c==34 { scan=0 } else { if c==39 { scan=0 } else { if c==32 { scan=0 } else { if c==60 { scan=0 } else { if c==44 { scan=0 } else { if c==125 { scan=0 } else { if c==93 { scan=0 } else { if c==9 { scan=0 } else { if c==10 { scan=0 } else { 36 if k < 4095 { url[k] = c as u8; k = k + 1 } j = j + 1 37 } } } } } } } } } 38 } 39 } 40 } 41 url[k] = 0 as u8 42 let conf: i64 = xh_classify(table, url, k, kb) 43 if conf > 0 { if xt_find(out, wp, url, k, 0) < 0 { if (wp + k + 1) < cap { var a: i64=0; while a<k { out[wp]=url[a]; wp=wp+1; a=a+1 } out[wp]=10 as u8; wp=wp+1 } } } 44 i = j 45 } else { i = i + 1 } 46 } 47 out[wp] = 0 as u8 48 return wp 49} 50 51// EXTRACT with a caller-supplied TABLE (persisted / healed): run the layers over `html`, pick the best media URL 52// -> best_url; set *kind; return confidence (0 = nothing). Both harvest and rank use `table`, so a learned 53// pattern is honored throughout. Layers: raw HTML + __NUXT__ + ytInitialPlayerResponse. 54func mx_extract_t(html: *u8, hlen: i64, best_url: *u8, cap: i64, kind: *i64, table: *i64) -> i64 { 55 let cand: i64 = 1048576 56 let list: *u8 = sys_mmap(cand) 57 var off: i64 = 0 58 off = mx_harvest(html, hlen, list, off, cand, table) // layer 1: raw HTML 59 let blob: *u8 = sys_mmap(1048576) 60 let b1: i64 = ms_extract_state(html, hlen, "__NUXT__\x00" as *u8, blob, 1048576) 61 if b1 > 0 { off = mx_harvest(blob, b1, list, off, cand, table) } // layer 2a: __NUXT__ state 62 let b2: i64 = ms_extract_state(html, hlen, "ytInitialPlayerResponse\x00" as *u8, blob, 1048576) 63 if b2 > 0 { off = mx_harvest(blob, b2, list, off, cand, table) } // layer 2b: player response 64 return xh_best(table, list, off, best_url, cap, kind) // table-driven rank -> the winner 65} 66 67// EXTRACT (default): seed a fresh table from the base knowledge, then run mx_extract_t. Backward-compatible 68// signature. For live self-healing, load a persisted table (xh_load) and call mx_extract_t directly instead. 69func mx_extract(html: *u8, hlen: i64, best_url: *u8, cap: i64, kind: *i64) -> i64 { 70 let table: *i64 = xh_new() 71 xh_seed(table) 72 return mx_extract_t(html, hlen, best_url, cap, kind, table) 73}