nx_fin_edgar_fetch.nx source
↩ module page · 84 lines · 4414 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"
11const K_MAGIC_4194304: i64 = 4194304
12const K_MAGIC_8388608: i64 = 8388608
13
14func pp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
15func pn(v: i64) -> i64 {
16 if v==0 { sys_write(1,"0" as *u8,1); return 0 }
17 var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m }
18 let d: *u8=sys_mmap(24); var k: i64=0
19 while m>0 { d[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
20 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 }
21 return 0
22}
23func 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 }
24func eg_strlen(s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){i=i+1} return i }
25
26// offset just AFTER the last occurrence of key in buf[0..n), else -1.
27func eg_find_last(buf: *u8, n: i64, key: *u8) -> i64 {
28 let klen: i64 = eg_strlen(key)
29 var last: i64 = 0 - 1; var i: i64 = 0
30 while i <= n - klen {
31 var hit: i64 = 1; var j: i64 = 0
32 while j < klen { if (buf[i+j] as i64) != (key[j] as i64) { hit = 0; j = klen } else { j = j + 1 } }
33 if hit == 1 { last = i + klen }
34 i = i + 1
35 }
36 return last
37}
38// parse a (possibly negative) integer starting at off.
39func eg_parse_int(buf: *u8, off: i64, n: i64) -> i64 {
40 var i: i64 = off; var v: i64 = 0; var neg: i64 = 0
41 if i < n { if (buf[i] as i64) == 45 { neg = 1; i = i + 1 } }
42 var stop: i64 = 0
43 while stop == 0 {
44 if i >= n { stop = 1 } else {
45 let c: i64 = buf[i] as i64
46 if c < 48 { stop = 1 } else { if c > 57 { stop = 1 } else { v = v*10 + (c - 48); i = i + 1 } }
47 }
48 }
49 if neg == 1 { v = 0 - v }
50 return v
51}
52
53func main() -> i64 {
54 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304)
55 if r <= 0 { pp("certdata load failed\n" as *u8); return 1 }
56 let store: *TrustStore = r as *TrustStore
57
58 // Apple Inc. (CIK 0000320193), us-gaap:Assets -- FREE_RAW public-domain primary source.
59 let url: *u8 = sys_mmap(256); var o: i64 = 0
60 o = eg_cat(url, o, "https://data.sec.gov/api/xbrl/companyconcept/CIK0000320193/us-gaap/Assets.json" as *u8)
61 url[o] = 0 as u8
62
63 let cap: i64 = K_MAGIC_8388608
64 let scratch: *u8 = sys_mmap(cap)
65 let status: *i64 = sys_mmap(8) as *i64
66 let n: i64 = nx_https_fetch_follow(url, store, scratch, cap, 6, status)
67 pp("EDGAR Apple/us-gaap:Assets status=" as *u8); pn(status[0]); pp(" bytes=" as *u8); pn(n); pp("\n" as *u8)
68 if n <= 0 { pp("FETCH-FAIL\n" as *u8); return 1 }
69
70 // build the search key "val": as bytes (avoids quote-escape questions in string literals)
71 let key: *u8 = sys_mmap(8)
72 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
73 let vpos: i64 = eg_find_last(scratch, n, key)
74 if vpos < 0 { pp("no \"val\" found in JSON\n" as *u8); return 1 }
75 let assets: i64 = eg_parse_int(scratch, vpos, n)
76 pp("latest reported us-gaap:Assets (USD) = " as *u8); pn(assets); pp("\n" as *u8)
77 var sfrom: i64 = vpos - 26; if sfrom < 0 { sfrom = 0 }
78 pp(" JSON context: " as *u8); sys_write(1, ((scratch as i64)+sfrom) as *u8, 90); pp("\n" as *u8)
79
80 pp("\nFREE_RAW PROVEN: real SEC fundamentals from the public-domain primary source -- no key, no payment.\n" as *u8)
81 pp("POLITE follow-up (per nx_fin_datasource doctrine): add a declared User-Agent to the shared fetcher for\n" as *u8)
82 pp("SEC fair-access compliance before repeated/production EDGAR pulls (EDGAR tolerated the default here).\n" as *u8)
83 return 0
84}