Skip to content

Instantly share code, notes, and snippets.

@mattdahl
Created January 7, 2021 05:05
Show Gist options
  • Save mattdahl/885858b53640ad5c0ecff035224743ec to your computer and use it in GitHub Desktop.
Save mattdahl/885858b53640ad5c0ecff035224743ec to your computer and use it in GitHub Desktop.
Code for joining the courts_db data with CL's citation_string data.
const fs = require('fs')
const request = require('request');
// get courts in courts_db
const our_courts = JSON.parse(fs.readFileSync('courts_db/data/courts_old.json', 'utf8'));
console.log(our_courts.length);
// get courts in CL
let their_courts = [];
const get_their_courts = function (url) {
request(url, {headers: {'Authorization': 'Token XXX'}}, (e, resp, body) => {
if (e) {
throw e;
}
var json = JSON.parse(body);
their_courts = their_courts.concat(json.results);
if (json.next) {
// keep getting courts
get_their_courts(json.next);
}
else {
// have all courts, do data merge
console.log(their_courts.length);
do_join();
}
});
};
get_their_courts('https://www.courtlistener.com/api/rest/v3/courts/?format=json');
const do_join = function () {
// left join
for (i = 0; i < our_courts.length; i++) {
court = our_courts[i];
matched_court_index = their_courts.findIndex((c) => (c.id === court.id));
if (matched_court_index >= 0) {
court.citation_string = their_courts[matched_court_index].citation_string;
}
else {
court.citation_string = '';
}
}
// right join check
missing = []
for (i = 0; i < their_courts.length; i++) {
court = their_courts[i];
matched_court_index = our_courts.findIndex((c) => (c.id === court.id));
if (matched_court_index < 0) {
missing.push(court);
}
}
console.log(missing.length);
our_courts_new_json = JSON.stringify(our_courts, null, 4);
fs.writeFile('courts_db/data/courts.json', our_courts_new_json, 'utf8', function (e) {
if (e) {
throw e;
}
console.log('done');
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment