nx_archive_media_fetch.nx source
↩ module page · 171 lines · 10078 B
1// nx_archive_media_fetch.nx -- R6 step A (fetch half): pull the ACTUAL media bytes of the reconstructed page3.com
2// 1997 page and store them ourselves. Reads the cached CDX + page (web_assets/archive/_cache_*), extracts the
3// page3.com <img> URLs, fetches each archived image (web/<ts14>id_/<imgurl>), saves to
4// web_assets/archive/media/<basename>, content-addresses it (CID), and writes media/manifest.tsv
5// (name<TAB>origurl<TAB>bytes<TAB>cid). Cache-first per image (skip if already saved) so we never re-hammer the
6// archive. Fetch stack only -- decode/hash is the separate nx_archive_media_recovery (avoids the
7// syscalls.nx/nx_syscalls.nx clash). Distinct from the existing nx_media_fetch (MediaPool primitive). license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_x509_trust_store.nx"
10import "nx_trust_store_load_from_certdata.nx"
11import "nx_https_fetch_follow.nx"
12import "nx_cdx_parse.nx"
13import "nx_temporal_index.nx"
14const K_MAGIC_1000000007: i64 = 1000000007
15const K_MAGIC_4194304: i64 = 4194304
16const K_MAGIC_4096: i64 = 4096
17const K_MAGIC_19970601: i64 = 19970601
18const K_MAGIC_2048: i64 = 2048
19
20func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
21func gn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var w: i64=0; var q: i64=k-1; while q>=0 { o[w]=t[q]; w=w+1; q=q-1 } sys_write(1,o,w); return 0 }
22func ap(buf: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8) { buf[off+i]=s[i]; i=i+1 } return off+i }
23func apn(buf: *u8, off: i64, v: i64) -> i64 { if v==0 { buf[off]=0x30 as u8; return off+1 } var m: i64=v; let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } var o: i64=off; var q: i64=k-1; while q>=0 { buf[o]=t[q]; o=o+1; q=q-1 } return o }
24func apslice(dst: *u8, off: i64, src: *u8, so: i64, sl: i64) -> i64 { var i: i64=0; while i<sl { dst[off+i]=src[so+i]; i=i+1 } return off+sl }
25func cid_hash(buf: *u8, n: i64) -> i64 { var h: i64=0; var i: i64=0; while i<n { h=(h*131+(buf[i] as i64))%K_MAGIC_1000000007; i=i+1 } return h }
26func dstarts(buf: *u8, off: i64, n: i64, lit: *u8, litlen: i64) -> i64 { if off+litlen>n { return 0 } var i: i64=0; while i<litlen { var a: i64=buf[off+i] as i64; if a>=0x41 { if a<=0x5a { a=a+0x20 } } if a != (lit[i] as i64) { return 0 } i=i+1 } return 1 }
27func contains_lit(buf: *u8, off: i64, len: i64, lit: *u8, litlen: i64) -> i64 { var i: i64=off; let e: i64=off+len-litlen; while i<=e { if dstarts(buf,i,off+len,lit,litlen)==1 { return 1 } i=i+1 } return 0 }
28// real-image magic check (rejects 404 HTML pages saved as .gif): GIF / JPEG / PNG / BMP.
29func is_image(buf: *u8, n: i64) -> i64 {
30 if n<4 { return 0 }
31 let a: i64=buf[0] as i64; let b: i64=buf[1] as i64; let c: i64=buf[2] as i64; let d: i64=buf[3] as i64
32 if a==0x47 { if b==0x49 { if c==0x46 { return 1 } } }
33 if a==0xff { if b==0xd8 { if c==0xff { return 1 } } }
34 if a==0x89 { if b==0x50 { if c==0x4e { if d==0x47 { return 1 } } } }
35 if a==0x42 { if b==0x4d { return 1 } }
36 return 0
37}
38func save_file(path: *u8, buf: *u8, n: i64) -> i64 { let fd: i64=sys_openat_wr(path, 0x1a4); if fd<0 { return 0 } var off: i64=0; while off<n { let w: i64=sys_write(fd, ((buf as i64)+off) as *u8, n-off); if w<=0 { sys_close(fd); return 0 } off=off+w } sys_close(fd); return 1 }
39func read_file(path: *u8, buf: *u8, cap: i64, lenbox: *i64) -> i64 { let fd: i64=sys_openat_rd(path); if fd<0 { return 0 } let n: i64=sys_read(fd, buf, cap); sys_close(fd); if n<=0 { return 0 } lenbox[0]=n; return n }
40func fetch_retry(url: *u8, store: *TrustStore, out: *u8, cap: i64, st: *i64, tries: i64) -> i64 { var t: i64=0; var n: i64=0-1; while t<tries { n=nx_https_fetch_follow(url, store, out, cap, 6, st); if n>0 { if st[0]==200 { t=tries } else { gw(" [status " as *u8); gn(st[0]); gw(" retry]\n" as *u8); t=t+1 } } else { gw(" [err " as *u8); gn(n); gw(" retry]\n" as *u8); t=t+1 } } return n }
41func next_imgsrc(buf: *u8, n: i64, from: i64, out2: *i64) -> i64 {
42 var i: i64=from
43 while i<n {
44 if dstarts(buf, i, n, "src=" as *u8, 4)==1 {
45 if i+4<n { if (buf[i+4] as i64)==0x22 {
46 let us: i64=i+5
47 var ue: i64=us
48 var go: i64=1
49 while go==1 { if ue<n { if (buf[ue] as i64)==0x22 { go=0 } else { ue=ue+1 } } else { go=0 } }
50 out2[0]=us; out2[1]=ue-us
51 return ue+1
52 } }
53 }
54 i=i+1
55 }
56 return 0-1
57}
58func basename_of(buf: *u8, off: i64, len: i64, dst: *u8) -> i64 {
59 var last: i64=off-1
60 var i: i64=off
61 while i<off+len { if (buf[i] as i64)==0x2f { last=i } i=i+1 }
62 var o: i64=0
63 i=last+1
64 while i<off+len {
65 let c: i64=buf[i] as i64
66 var keep: i64=0
67 if c>=0x30 { if c<=0x39 { keep=1 } }
68 if c>=0x41 { if c<=0x5a { keep=1 } }
69 if c>=0x61 { if c<=0x7a { keep=1 } }
70 if c==0x2e { keep=1 }
71 if c==0x2d { keep=1 }
72 if c==0x5f { keep=1 }
73 if keep==1 { dst[o]=c as u8; o=o+1 }
74 i=i+1
75 }
76 if o==0 { dst[0]=0x78 as u8; o=1 }
77 dst[o]=0 as u8
78 return o
79}
80
81func main() -> i64 {
82 gw("=== nx_archive_media_fetch: pull + store the real page3.com 1997 media bytes ===\n" as *u8)
83 sys_mkdir("web_assets" as *u8, 0x1ed)
84 sys_mkdir("web_assets/archive" as *u8, 0x1ed)
85 sys_mkdir("web_assets/archive/media" as *u8, 0x1ed)
86 let r: i64=nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304)
87 if r<=0 { gw("trust store load failed\n" as *u8); return 1 }
88 let store: *TrustStore=r as *TrustStore
89 let cap: i64=K_MAGIC_4194304
90 let lb: *i64=sys_mmap(8) as *i64
91 let st: *i64=sys_mmap(8) as *i64
92
93 let cdxbuf: *u8=sys_mmap(cap)
94 if read_file("web_assets/archive/_cache_cdx.txt" as *u8, cdxbuf, cap, lb)<=0 { gw("no cached CDX -- run nx_archive_viewer first\n" as *u8); return 1 }
95 let ncdx: i64=lb[0]
96 let RS: *i64=sys_mmap(K_MAGIC_4096) as *i64
97 let RE: *i64=sys_mmap(K_MAGIC_4096) as *i64
98 let nrows: i64=cdx_rows(cdxbuf, ncdx, RS, RE, 480)
99 let keys: *i64=sys_mmap(K_MAGIC_4096) as *i64
100 let dates: *i64=sys_mmap(K_MAGIC_4096) as *i64
101 let tso: *i64=sys_mmap(K_MAGIC_4096) as *i64
102 let tsl: *i64=sys_mmap(K_MAGIC_4096) as *i64
103 let b2: *i64=sys_mmap(16) as *i64
104 var i: i64=0
105 var mm: i64=0
106 while i<nrows {
107 cdx_field(cdxbuf, RS[i], RE[i], 4, b2)
108 var is200: i64=0
109 if b2[1]==3 { if (cdxbuf[b2[0]] as i64)==0x32 { if (cdxbuf[b2[0]+1] as i64)==0x30 { if (cdxbuf[b2[0]+2] as i64)==0x30 { is200=1 } } } }
110 if is200==1 {
111 cdx_field(cdxbuf, RS[i], RE[i], 0, b2)
112 let kb: *u8=sys_mmap(256); var j: i64=0; while j<b2[1] { kb[j]=cdxbuf[b2[0]+j]; j=j+1 } kb[b2[1]]=0 as u8; keys[mm]=kb as i64
113 cdx_field(cdxbuf, RS[i], RE[i], 1, b2); dates[mm]=cdx_ts8(cdxbuf, b2[0], b2[1]); tso[mm]=b2[0]; tsl[mm]=b2[1]
114 mm=mm+1
115 }
116 i=i+1
117 }
118 let q: i64=ti_query(keys, dates, mm, "com,page3)/" as *u8, K_MAGIC_19970601)
119 if q<0 { gw("no as-of-1997 capture\n" as *u8); return 1 }
120 gw("capture " as *u8); gn(dates[q]); gw("\n" as *u8)
121
122 let html: *u8=sys_mmap(cap)
123 if read_file("web_assets/archive/_cache_page.txt" as *u8, html, cap, lb)<=0 { gw("no cached page -- run nx_archive_viewer first\n" as *u8); return 1 }
124 let nhtml: i64=lb[0]
125
126 let img: *u8=sys_mmap(cap)
127 let man: *u8=sys_mmap(cap)
128 var mo: i64=0
129 let o2: *i64=sys_mmap(16) as *i64
130 let nm: *u8=sys_mmap(256)
131 let url: *u8=sys_mmap(K_MAGIC_2048)
132 let path: *u8=sys_mmap(512)
133 var from: i64=0
134 var grabbed: i64=0
135 var seen: i64=0
136 var more: i64=1
137 while more==1 {
138 let p: i64=next_imgsrc(html, nhtml, from, o2)
139 if p<0 { more=0 } else {
140 from=p
141 let uoff: i64=o2[0]; let ulen: i64=o2[1]
142 seen=seen+1
143 if contains_lit(html, uoff, ulen, "page3.com" as *u8, 9)==1 {
144 if grabbed<8 {
145 let nl: i64=basename_of(html, uoff, ulen, nm)
146 var po: i64=0; po=ap(path, po, "web_assets/archive/media/" as *u8); po=apslice(path, po, nm, 0, nl); path[po]=0 as u8
147 var n3: i64=read_file(path, img, cap, lb)
148 var cached: i64=0
149 if n3>0 { n3=lb[0]; cached=1 } else {
150 var uo: i64=0; uo=ap(url, uo, "https://web.archive.org/web/" as *u8); uo=apslice(url, uo, cdxbuf, tso[q], tsl[q]); uo=ap(url, uo, "id_/" as *u8); uo=apslice(url, uo, html, uoff, ulen); url[uo]=0 as u8
151 gw(" fetch " as *u8); gw(nm); gw(" ...\n" as *u8)
152 n3=fetch_retry(url, store, img, cap, st, 6)
153 }
154 if n3>0 { if is_image(img, n3)==1 {
155 if cached==0 { save_file(path, img, n3) }
156 let cid: i64=cid_hash(img, n3)
157 mo=ap(man, mo, nm); man[mo]=0x09 as u8; mo=mo+1
158 mo=apslice(man, mo, html, uoff, ulen); man[mo]=0x09 as u8; mo=mo+1
159 mo=apn(man, mo, n3); man[mo]=0x09 as u8; mo=mo+1
160 mo=apn(man, mo, cid); man[mo]=0x0a as u8; mo=mo+1
161 gw(" " as *u8); gw(nm); gw(" bytes=" as *u8); gn(n3); gw(" cid=" as *u8); gn(cid); if cached==1 { gw(" [cache]" as *u8) } gw("\n" as *u8)
162 grabbed=grabbed+1
163 } else { gw(" " as *u8); gw(nm); gw(" [skip: not an image / 404 page, " as *u8); gn(n3); gw("B]\n" as *u8) } }
164 }
165 }
166 }
167 }
168 save_file("web_assets/archive/media/manifest.tsv" as *u8, man, mo)
169 gw("\nMEDIA-FETCH DONE: img-refs seen=" as *u8); gn(seen); gw(" page3 media grabbed=" as *u8); gn(grabbed); gw(" -> web_assets/archive/media/ + manifest.tsv\n" as *u8)
170 return 0
171}