"""Reproduce AutoMarketAPI's 2026-10-18 pinned-publication audit. Requires: pip install requests pandas pyarrow Run: python snapshot-audit-20261018.py --data-dir ./audit-data These are dated listing files, not live inventory or independently verified sales. Code copyright AutoMarketAPI 2026, licensed under MIT. Data retains each file's license. """ from pathlib import Path import argparse,hashlib,json,re import requests,pandas as pd def run(): parser=argparse.ArgumentParser();parser.add_argument('--data-dir',default='./audit-data');parser.add_argument('--manifest',default='https://automarketapi.com/data-coverage.json');args=parser.parse_args();root=Path(args.data_dir);root.mkdir(parents=True,exist_ok=True) if args.manifest.startswith('https://'): r=requests.get(args.manifest,timeout=45);r.raise_for_status();manifest=r.json() else:manifest=json.loads(Path(args.manifest).read_text(encoding='utf-8')) results={} for key,d in manifest['publications'].items(): frames=[] for f in d['files']: p=root/(key+'-'+Path(f['name']).name) if not p.exists(): with requests.get(f['url'],stream=True,timeout=(20,120)) as r: r.raise_for_status() with p.open('wb') as out: for chunk in r.iter_content(1024*1024):out.write(chunk) assert hashlib.sha256(p.read_bytes()).hexdigest()==f['sha256'],'File hash mismatch: '+str(p) frames.append(pd.read_parquet(p)) df=pd.concat(frames,ignore_index=True);rows=len(df);missing={} for c in df: mask=df[c].isna() if df[c].dtype=='object' or isinstance(df[c].dtype,pd.StringDtype):mask=mask|df[c].map(lambda v:isinstance(v,str) and not v.strip()) missing[c]=int(mask.sum()) ids=df['id'].dropna().astype(str);ids=ids[ids.str.strip()!=''];sources=df['source_site'].fillna('Unknown') if 'source_site' in df else pd.Series([key]*rows) out={'rows':rows,'unique_nonempty_ids':int(ids.nunique()),'duplicate_id_rows':int(len(ids)-ids.nunique()),'empty_id_rows':int(rows-len(ids)),'source_rows':{str(k):int(v) for k,v in sources.value_counts().items()},'all_field_missing_rows':missing} if 'vin' in df:out['vin_structure_matches']=int(df['vin'].fillna('').str.fullmatch(r'[A-HJ-NPR-Z0-9]{17}').sum()) country='seller_country' if 'seller_country' in df else None;price=d['asking_price_field'];groups=df.groupby(country) if country else [('US' if key=='cars-com' else 'GB',df)];out['asking_price_summary']={} for code,group in groups: vals=pd.to_numeric(group[price],errors='coerce');vals=vals[vals>0] if len(vals):out['asking_price_summary'][str(code)]={'positive_price_rows':len(vals),'median':float(vals.median()),'min':float(vals.min()),'max':float(vals.max())} for field in ['rows','unique_nonempty_ids','duplicate_id_rows','empty_id_rows','source_rows','all_field_missing_rows','asking_price_summary']:assert out[field]==d[field],key+' mismatch: '+field results[key]=out;print(key+': '+str(rows)+' rows; pinned hash and measurements match.') (root/'reproduced-audit.json').write_text(json.dumps(results,indent=2),encoding='utf-8') if __name__=='__main__':run()