'appeals', 'FD' => 'district', 'FB' => 'bankruptcy', 'FS' => 'special']; function api(string $token, string $path, array $params = []): array { usleep(PAUSE_MICROSECONDS); $curl = curl_init(API . $path . '?' . http_build_query($params)); curl_setopt_array($curl, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 60, CURLOPT_USERAGENT => USER_AGENT, CURLOPT_HTTPHEADER => ["Authorization: Token $token"], ]); $body = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); if ($status === 429 && preg_match('/available in (\d+) seconds?/', $body, $wait)) { sleep((int)$wait[1] + 1); return api($token, $path, $params); } if ($body === false || $status >= 400) { fail("CourtListener request failed ($status): " . ($body === false ? curl_error($curl) : substr($body, 0, 200))); } return json_decode($body, true); } function line(array $fields): void { echo implode('|', array_map('clean_field', $fields)), "\n"; } function emit(array $rows, array $keys, bool $verbose): void { if ($verbose) { echo json_encode(array_map(fn($r) => array_combine($keys, $r), $rows), JSON_PRETTY_PRINT), "\n"; return; } array_map('line', $rows); } /** Every federal court CourtListener knows, as [id, name, level, jurisdiction], cached on disk. */ function courts(string $token, string $cache, bool $refresh): array { $file = "$cache/cl-courts.json"; if (!$refresh && is_file($file)) { return json_decode(file_get_contents($file), true); } $rows = []; foreach (array_keys(LEVELS) as $jurisdiction) { $page = api($token, 'courts/', ['page_size' => 500, 'in_use' => 'true', 'jurisdiction' => $jurisdiction]); while (true) { foreach ($page['results'] as $c) { $rows[] = ["cl:{$c['id']}", $c['full_name'], LEVELS[$jurisdiction], 'federal']; } if (!$page['next']) { break; } parse_str(parse_url($page['next'], PHP_URL_QUERY), $next); $page = api($token, 'courts/', $next); } } file_put_contents($file, json_encode($rows)); return $rows; } /** The stored result for $key when younger than $ttl seconds (forever when null); otherwise fetches and stores it. */ function cached(string $key, ?int $ttl, callable $fetch): array { $file = ($GLOBALS['cache'] ?? sys_get_temp_dir()) . '/cl-' . md5($key) . '.json'; $fresh = is_file($file) && ($ttl === null || time() - filemtime($file) < $ttl); if ($fresh && !isset($GLOBALS['opts']['refresh'])) { return json_decode(file_get_contents($file), true); } $result = $fetch(); file_put_contents($file, json_encode($result)); return $result; } function docket_entries(string $token, string $docket, ?string $entry, ?int $ttl): array { return cached("entries $docket $entry", $ttl, fn() => fetch_docket_entries($token, $docket, $entry)); } /** The newest docket entries, or the one entry numbered $entry. Entries carry document text, so one small page. */ function fetch_docket_entries(string $token, string $docket, ?string $entry): array { $params = ['docket' => $docket, 'order_by' => '-date_filed', 'page_size' => ENTRIES_PER_PAGE]; if ($entry !== null) { $params['entry_number'] = $entry; } return api($token, 'docket-entries/', $params)['results']; } $opts = getopt('', ['action:', 'token:', 'court:', 'query:', 'docket:', 'entry:', 'cache:', 'refresh', 'verbose']); $action = $opts['action'] ?? fail('Missing --action'); $token = $opts['token'] ?? fail('Missing --token'); $verbose = isset($opts['verbose']); $docket = $opts['docket'] ?? null; $cache = $opts['cache'] ?? sys_get_temp_dir(); const LISTING_TTL = 3600; switch ($action) { case 'courts': emit(courts($token, $opts['cache'] ?? sys_get_temp_dir(), isset($opts['refresh'])), ['id', 'name', 'level', 'jurisdiction'], $verbose); break; case 'search': $params = ['q' => $opts['query'] ?? fail('Missing --query'), 'type' => 'r']; if (isset($opts['court'])) { $params['court'] = $opts['court']; } $rows = array_map(fn($r) => [$r['docket_id'], $r['caseName'], $r['court_id'], $r['docketNumber'], $r['dateFiled'], $r['dateTerminated']], cached('search ' . json_encode($params), LISTING_TTL, fn() => api($token, 'search/', $params))['results']); emit($rows, ['docket', 'case_name', 'court', 'docket_number', 'date_filed', 'date_terminated'], $verbose); break; case 'case': $docket ?? fail('Missing --docket'); $d = cached("docket $docket", LISTING_TTL, fn() => api($token, "dockets/$docket/")); $fields = [ 'Case' => ['Name' => $d['case_name'], 'Docket Number' => $d['docket_number'], 'Court' => $d['court_id'], 'Cause' => $d['cause'], 'Nature of Suit' => $d['nature_of_suit']], 'Status' => ['Filed' => $d['date_filed'], 'Terminated' => $d['date_terminated'], 'Last Filing' => $d['date_last_filing']], 'Judge' => ['Assigned To' => $d['assigned_to_str'], 'Referred To' => $d['referred_to_str']], ]; $rows = []; foreach ($fields as $section => $pairs) { foreach ($pairs as $label => $value) { $rows[] = [$section, $label, $value ?? '']; } } emit($rows, ['section', 'label', 'value'], $verbose); break; case 'events': $rows = array_map(fn($e) => ['entry', $e['entry_number'], $e['date_filed'], $e['description'], count(array_filter($e['recap_documents'], fn($r) => $r['is_available']))], docket_entries($token, $docket ?? fail('Missing --docket'), $opts['entry'] ?? null, LISTING_TTL)); emit($rows, ['event', 'number', 'date', 'description', 'documents'], $verbose); break; case 'documents': case 'document': $entries = docket_entries($token, $docket ?? fail('Missing --docket'), $opts['entry'] ?? ($action === 'document' ? fail('Missing --entry') : null), $action === 'document' ? null : LISTING_TTL); $rows = []; foreach ($entries as $e) { foreach ($e['recap_documents'] as $r) { if ($r['is_available']) { $rows[] = ['document', $e['entry_number'], $r['attachment_number'] ?? 0, $r['page_count'], $r['description'], $r['plain_text']]; } } } if ($action === 'document') { foreach ($rows as $r) { echo $r[5], "\n"; } break; } emit(array_map(fn($r) => array_slice($r, 0, 5), $rows), ['document', 'entry', 'attachment', 'pages', 'description'], $verbose); break; default: fail("Unknown action $action"); }