# -*- coding: utf-8 -*- """Rebuild the third party source corpus this paper was coded from. WHY THIS EXISTS The paper codes 410 startup failure post mortems and 1,404 unicorn rows from CB Insights. Those pages carry "all rights reserved", so the programme cannot redistribute CB Insights' own prose under this package's CC BY 4.0 licence, and `failures.json` as published therefore carries the facts (company name, funding, peak valuation) without the description text. Removing the text would make the coding unverifiable, which is not acceptable in a programme whose whole argument is that a reader can check the work. So this script rebuilds the exact corpus the coding was performed against, and `source-manifest.json` records a SHA-256 for every description so a rebuild can be proved identical to what was coded, byte for byte, without this repository ever hosting the text. WHAT IT DOES NOT DO It does not scrape on your behalf silently. It reads a local copy you supply, because whether to fetch those pages is your decision under their terms, not a decision this script should make for you. USAGE python rebuild_source_corpus.py --from verify a local copy python rebuild_source_corpus.py --check report what is missing The file you supply is any JSON list of objects with 'name' and 'desc'. """ import argparse import hashlib import io import json import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) MANIFEST = os.path.join(HERE, 'source-manifest.json') PUBLISHED = os.path.join(HERE, 'failures.json') def sha(text): return hashlib.sha256(text.encode('utf-8')).hexdigest() def load_manifest(): if not os.path.exists(MANIFEST): sys.exit('source-manifest.json is missing; this paper cannot be verified') return json.load(io.open(MANIFEST, encoding='utf-8')) def main(): ap = argparse.ArgumentParser() ap.add_argument('--from', dest='src') ap.add_argument('--check', action='store_true') a = ap.parse_args() man = load_manifest() if a.check or not a.src: print('Source corpus for %s' % man['source']['publisher']) print(' pages : %s' % ', '.join(man['source']['urls'])) print(' retrieved : %s' % man['source']['retrieved']) print(' terms : %s' % man['source']['terms']) print(' entries : %d failures, %d unicorns' % (man['failures']['entries'], man['unicorns']['entries'])) print(' description text, total characters: %d' % man['failures']['desc_characters']) print(' per entry SHA-256 recorded : %d' % len(man['failures']['desc_sha256'])) print('') print('The text itself is not redistributed here. Supply a local copy') print('with --from and this script will tell you whether it') print('is the same corpus the coding was performed against.') return rows = json.load(io.open(a.src, encoding='utf-8')) if isinstance(rows, dict): rows = rows.get('companies') or rows.get('data') or [] got = {r['name']: (r.get('desc') or '') for r in rows if r.get('name')} want = man['failures']['desc_sha256'] missing = [n for n in want if n not in got] differs = [n for n in want if n in got and sha(got[n]) != want[n]] extra = [n for n in got if n not in want] print('entries expected %d, supplied %d' % (len(want), len(got))) print(' missing : %d' % len(missing)) print(' differing : %d' % len(differs)) print(' extra : %d' % len(extra)) for n in (missing + differs)[:8]: print(' %s' % n) if not missing and not differs: print('') print('IDENTICAL to the corpus this paper was coded from.') print('The coding in failures-coded.csv can be checked against it line ' 'by line.') else: print('') print('NOT identical. CB Insights edits these pages, so a later ' 'retrieval will differ; the manifest records what was coded on ' '%s.' % man['source']['retrieved']) if __name__ == '__main__': main()