This repository has been archived by the owner on Sep 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdecompile_datajson.js
executable file
·131 lines (124 loc) · 3.96 KB
/
decompile_datajson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env node
var yargs = require('yargs');
var fs = require('fs');
var pkg = require('./package.json');
var request = require('request');
var slugify = function(a)
{
//return "0100-01-01-"+a.replace(/,/g, '_');
return "0100-01-01-"+a;
};
var esc = function(a)
{
return a.replace(/"/g, '\\"');
};
var json_to_md = function(dest, index, json) {
var path = dest+"/"+slugify(json.identifier)+".md";
var md = "";
if(json.identifier == "100,765")
{
//md += JSON.stringify(json)
}
md += "---\n";
md += "layout: wrapper_text\n";
md += "category: datasets\n";
md += "\n";
md += "# Basic\n";
md += "identifier: \""+json.identifier+"\"\n";
md += "title: \""+json.title+"\"\n";
md += "describedBy: \""+(json["describedBy"]||"")+"\"\n";
md += "description: \""+esc(json.description)+"\"\n";
md += "programCode:\n";
md += " - \""+json.programCode[0]+"\"\n";
md += "bureauCode:\n";
md += " - \""+json.bureauCode[0]+"\"\n";
md += "\n";
md += "# Dates\n";
md += "modified: \""+json.modified+"\"\n";
md += "\n";
md += "# POC\n";
md += "poc:\n";
md += " type: \""+json.contactPoint["@type"]+"\"\n";
md += " fn: \""+json.contactPoint.fn+"\"\n";
md += " hasEmail: \""+json.contactPoint.hasEmail+"\"\n";
md += "\n";
md += "# Publisher\n";
md += "publisher:\n";
md += " type: \""+json.publisher["@type"]+"\"\n";
md += " name: \""+json.publisher.name+"\"\n";
md += "\n";
md += "# Spatiotemporal\n";
md += "spatial: \""+json.spatial+"\"\n";
md += "temporal: \""+json.temporal+"\"\n";
md += "\n";
md += "# Distribution\n";
md += "distribution:\n";
for(var i = 0; i < json.distribution.length; i++)
{
var d = json.distribution[i];
md += " - type: \""+d["@type"]+"\"\n";
if(d["accessURL"] !== undefined)
md += " accessURL: \""+d["accessURL"]+"\"\n";
if(d["downloadURL"] !== undefined)
md += " downloadURL: \""+d["downloadURL"]+"\"\n";
if(d["mediaType"] !== undefined)
md += " mediaType: \""+d["mediaType"]+"\"\n";
if(d["format"] !== undefined)
md += " format: \""+d["format"]+"\"\n";
}
md += "\n";
md += "# Keywords\n";
md += "keyword:\n";
for(var i = 0; i < json.keyword.length; i++)
{
var kw = json.keyword[i];
md += " - \""+kw+"\"\n";
}
md += "---";
fs.writeFile(path, md, function(err) {});
};
var decompile = function () {
var argv = yargs.usage('Usage: $0 decompile_datajson --dest [dest] --file [file] --url [url]').demand(['dest']).argv;
var dest = argv.dest;
var file = argv.file;
var url = argv.url;
if(dest !== undefined && file != undefined)
{
fs.readFile(file, 'utf8', function(error, data) {
if (!error) {
var datajson = JSON.parse(data);
if(datajson.dataset !== undefined)
{
for(var i = 0; i < datajson.dataset.length; i++)
{
var dataset = datajson.dataset[i];
json_to_md(dest, i, dataset);
}
}
}
});
}
else if(dest !== undefined && url !== undefined)
{
process.stdout.write(url+"\n");
process.stdout.write("Processing url\n");
request(url, function(error, response, body) {
if (!error && response.statusCode == 200) {
var datajson = JSON.parse(body);
if(datajson.dataset !== undefined)
{
for(var i = 0; i < datajson.dataset.length; i++)
{
var dataset = datajson.dataset[i];
json_to_md(dest, i, dataset);
}
}
}
});
}
else
{
process.stdout.write("You need to specify file or url.");
}
};
decompile();