code wiki / (root) / nx_fin_edgar_fetch.nx

nx_fin_edgar_fetch.nx source

↩ module page · 97 lines · 5668 B

1// nx_fin_edgar_fetch.nx -- FREE_RAW in action: pull REAL company fundamentals straight from SEC EDGAR 2// (data.sec.gov XBRL companyconcept), the public-domain PRIMARY source that aggregators paywall. No key, no 3// payment. Extracts the latest reported value of a us-gaap concept by finding the last "val": in the JSON. 4// Demonstrates the nx_fin_datasource doctrine: FREE_RAW, and POLITE = we SHOULD send a declared User-Agent per 5// SEC fair-access (our shared fetcher doesn't yet -- flagged as the compliance follow-up; EDGAR currently 6// tolerates the default request). Composes the proven sovereign TLS fetcher. license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_x509_trust_store.nx" 9import "nx_trust_store_load_from_certdata.nx" 10import "nx_https_fetch_follow.nx" 11import "nx_edgar_lib.nx" // edgar_ua_header -- the ONE declared-identity definition (was duplicated here) 12const K_MAGIC_4194304: i64 = 4194304 13const K_MAGIC_8388608: i64 = 8388608 14 15func pp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 16func pn(v: i64) -> i64 { 17 if v==0 { sys_write(1,"0" as *u8,1); return 0 } 18 var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } 19 let d: *u8=sys_mmap(24); var k: i64=0 20 while m>0 { d[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } 21 var i: i64=k-1; while i>=0 { let o: *u8=sys_mmap(1); o[0]=d[i]; sys_write(1,o,1); i=i-1 } 22 return 0 23} 24func eg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){ dst[off+i]=s[i]; i=i+1 } return off+i } 25func eg_strlen(s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){i=i+1} return i } 26 27// offset just AFTER the last occurrence of key in buf[0..n), else -1. 28func eg_find_last(buf: *u8, n: i64, key: *u8) -> i64 { 29 let klen: i64 = eg_strlen(key) 30 var last: i64 = 0 - 1; var i: i64 = 0 31 while i <= n - klen { 32 var hit: i64 = 1; var j: i64 = 0 33 while j < klen { if (buf[i+j] as i64) != (key[j] as i64) { hit = 0; j = klen } else { j = j + 1 } } 34 if hit == 1 { last = i + klen } 35 i = i + 1 36 } 37 return last 38} 39// parse a (possibly negative) integer starting at off. 40func eg_parse_int(buf: *u8, off: i64, n: i64) -> i64 { 41 var i: i64 = off; var v: i64 = 0; var neg: i64 = 0 42 if i < n { if (buf[i] as i64) == 45 { neg = 1; i = i + 1 } } 43 var stop: i64 = 0 44 while stop == 0 { 45 if i >= n { stop = 1 } else { 46 let c: i64 = buf[i] as i64 47 if c < 48 { stop = 1 } else { if c > 57 { stop = 1 } else { v = v*10 + (c - 48); i = i + 1 } } 48 } 49 } 50 if neg == 1 { v = 0 - v } 51 return v 52} 53 54// (the declared-identity loader moved to nx_edgar_lib as edgar_load_ua / edgar_ua_header -- 55// ONE definition shared by every EDGAR consumer, so there is no second copy to drift.) 56func main() -> i64 { 57 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304) 58 if r <= 0 { pp("certdata load failed\n" as *u8); return 1 } 59 let store: *TrustStore = r as *TrustStore 60 61 // Apple Inc. (CIK 0000320193), us-gaap:Assets -- FREE_RAW public-domain primary source. 62 let url: *u8 = sys_mmap(256); var o: i64 = 0 63 o = eg_cat(url, o, "https://data.sec.gov/api/xbrl/companyconcept/CIK0000320193/us-gaap/Assets.json" as *u8) 64 url[o] = 0 as u8 65 66 let cap: i64 = K_MAGIC_8388608 67 let scratch: *u8 = sys_mmap(cap) 68 let status: *i64 = sys_mmap(8) as *i64 69 // SEC fair-access asks requesters to identify themselves and be contactable. The HEADER-CAPABLE TLS 70 // fetch ALREADY EXISTED (nx_https_fetch_follow_hdr_best; with xhdr_len=0 it is byte-identical to the 71 // plain call), so this needed no new fetcher, no signature change and no touch of its ~200 call 72 // sites -- only ADOPTION. The debt this organ's own header flagged was never a missing capability. 73 let xh: *u8 = sys_mmap(320) 74 let xo: i64 = edgar_ua_header(xh, 320) 75 if xo > 0 { pp("declared UA header from knowledge/fetch_identity.conf, bytes=" as *u8); pn(xo); pp("\n" as *u8) } 76 if xo <= 0 { pp("NO UA DECLARED (conf absent/empty) -- sending none, never a placeholder\n" as *u8) } 77 let n: i64 = nx_https_fetch_follow_hdr_best(url, store, scratch, cap, 6, status, xh, xo) 78 pp("EDGAR Apple/us-gaap:Assets status=" as *u8); pn(status[0]); pp(" bytes=" as *u8); pn(n); pp("\n" as *u8) 79 if n <= 0 { pp("FETCH-FAIL\n" as *u8); return 1 } 80 81 // build the search key "val": as bytes (avoids quote-escape questions in string literals) 82 let key: *u8 = sys_mmap(8) 83 key[0]=34 as u8; key[1]=118 as u8; key[2]=97 as u8; key[3]=108 as u8; key[4]=34 as u8; key[5]=58 as u8; key[6]=0 as u8 84 let vpos: i64 = eg_find_last(scratch, n, key) 85 if vpos < 0 { pp("no \"val\" found in JSON\n" as *u8); return 1 } 86 let assets: i64 = eg_parse_int(scratch, vpos, n) 87 pp("latest reported us-gaap:Assets (USD) = " as *u8); pn(assets); pp("\n" as *u8) 88 var sfrom: i64 = vpos - 26; if sfrom < 0 { sfrom = 0 } 89 pp(" JSON context: " as *u8); sys_write(1, ((scratch as i64)+sfrom) as *u8, 90); pp("\n" as *u8) 90 91 pp("\nFREE_RAW PROVEN: real SEC fundamentals from the public-domain primary source -- no key, no payment.\n" as *u8) 92 pp("POLITE: PAID 2026-08-15. The declared User-Agent is DATA (knowledge/fetch_identity.conf) and rides\n" as *u8) 93 pp("nx_https_fetch_follow_hdr_best, which already carried caller headers -- adoption, not new capability.\n" as *u8) 94 pp("Fail-closed: absent conf sends NO UA rather than a placeholder. NAMED LIMITATION (inherited, not new):\n" as *u8) 95 pp("the TLS-1.2 fallback leg carries no caller header, so a 1.2-only host is fetched unidentified.\n" as *u8) 96 return 0 97}