How to call REC Registery API and return the returned JSON to some kind of database

I would like to break this down into smaller, more rigorous questions, but I don't know what I don't know enough yet. So hopefully you can get specific answers to help do this.

the area to solve requires obtaining and analyzing a large number of records, there were ~ 17 million certificate (s) in 2013, while I am only interested in very small subsets of the order of 40,000 records.

In pseudocode:

  • iterate dates (thisDate)
    • send an API message for thisDate
    • receive JSONS as todaysRecords
    • check todaysRecords look for any criteria that match within the structure
    • add a subset of todaysRecords to recordsOut
  • save Out records to SQL / CSV file.

There is a large database of renewable energy certifications for use in the Australian RET schema system called REC Registery and also the web interface linked to here has an API that has simple call logic like this

http://rec-registry.gov.au/rec-registry/app/api/public-register/certificate-actions?date=<user provided date>

Where:

  • The date part of the url must be user supplied
  • Date format must be YYYY-MM-DD (no angle brackets and 1 date constraint)

JSON is returned (with potentially 100,000 entries per day).

The API documentation (13pp PDF) is here , but it mostly explains the members of the returned structure, which is less relevant to my question. Includes two sample JSON responses.

Although I know Javascript (mostly not in the web context), I am not sure how to send this message to the script and I will need to do it server side to be able to process (filter) the returned information and then save the entries. that interest me. I won't have a problem parsing JSON (if I can use JS) and copying the objects I want to store, I'm not sure where to even start doing this. I need a LAMP setup for this (or MAMP as I'm on OS X), or is there an easier JS way I can follow. I never knew how to save a file from a JS web browser, I thought it was banned for security reasons, but I think these are the ways and means.

If I can rewrite this question to be clearer and more efficient in finding an answer, then I'm glad to be editing the question too.

Perhaps I need some boilerplate code to call a simple API like this and the stack or application context in which I should be doing it. I understand there are several ways to accomplish this, but looking for the easiest ones for someone with JS knowledge and not much PHP / Python experience (but wanting to know what is needed).

Is it easy?

+3


source to share


1 answer


Good to point you in the right direction.

Requirements

If the selected language is Javascript, you need to install Node.js . No server needed at all.

The same is true for PHP or Python or whatever. No need for apache, just lang int.

Running script with node

Create file.js

somewhere. To run it, you just need to type (in the console) node file.js

(in the directory where the file is located.

Getting onfo from REC web service

Here's an example of a GET request:

var https = require('https');
var fs = require('fs');
var options = {
    host: 'rec-registry.gov.au',
    port: 443,
    path: '/rec-registry/app/api/public-register/certificate-actions?date=2015-06-03'
};
var jsonstr = '';

var request = https.get(options, function(response) {
  process.stdout.write("downloading data...");
  response.on('data', function (chunk) {
    process.stdout.write(".");
    jsonstr += chunk;
  });

  response.on('end', function () {
    process.stdout.write("DONE!");
    console.log(' ');
    console.log('Writing to file...');
    fs.writeFile("data.json", jsonstr, function(err) {
      if(err) {
        return console.error('Error saving file'); 
      }
    console.log('The file was saved!');
    });
  });
})

request.on('error', function(e) {
  console.log('Error downloading file: ' + e.message);
});

      

Convert json string to object / array

use JSON.parse

Data parsing

explore todaysRecords to find any matching criteria within the framework

Can't help you there, but it should be relatively easy to look for the correct object properties.

NOTE. Basically, what you get from a query is a string. Then you parse that line with



var foo = JSON.parse(jsonstr)

      

In this case, it foo

is an object. The "certificates" results are actually inside a property result

, which is an array

var results = foo.result;

      

In this example, the array contains about 1700 entries, and the certificate structure looks something like this:

"actionType": "STC created",
"completedTime": "2015-06-02T21:51:26.955Z",
"certificateRanges": [{
  "certificateType": "STC",
  "registeredPersonNumber": 10894,
  "accreditationCode": "PVD2259359",
  "generationYear": 2015,
  "generationState": "QLD",
  "startSerialNumber": 1,
  "endSerialNumber": 72,
  "fuelSource": "S.G.U. - solar (deemed)",
  "ownerAccount": "Solargain PV Pty Ltd",
  "ownerAccountId": 25782,
  "status": "Pending audit"
}]

      

So, to access, for example, the "ownerAccount" from the first "certificates" for the first "certificate" you would do:

var results = JSON.parse(jsonstr).result;

var ownerAccount = results[0].certificateRanges[0].ownerAccount;

      

Generating csv

The best way is to create an abstract structure (which suits your needs) and convert it to csv.

There's a nice npm library called json2csv that can help you out there

Example:

var fs = require('fs');
var json2csv = require('json2csv'); 
var fields = ['car', 'price', 'color']; // csv titles
var myCars = [
  {
    "car": "Audi",
    "price": 40000,
    "color": "blue"
  }, {
    "car": "BMW",
    "price": 35000,
    "color": "black"
  }, {
    "car": "Porsche",
    "price": 60000,
    "color": "green"
  }
];

json2csv({ data: myCars, fields: fields }, function(err, csv) {
  if (err) console.log(err);
  fs.writeFile('file.csv', csv, function(err) {
    if (err) throw err;
    console.log('file saved');
  });
});

      

If you want to add instead of writing to a new file, you can use

fs.appendFile('file.csv', csv, function (err) { });

      

+2


source







All Articles