-
Notifications
You must be signed in to change notification settings - Fork 8
/
silkExport.js
147 lines (117 loc) · 4.54 KB
/
silkExport.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict';
/*
* This code is forked from https://gist.github.com/bergmark/77f23bcee8103b1f6917
*
*/
var config = require('./config.js');
var SilkApi = require("silk-api-client");
var Silk = new SilkApi(config.silkApi);
var silkExport = {
go: function(data, ec, onFinished, onError) {
var name = config.appName + ".silk-testing.co";
if (!ec) {
throw new Error("No EntityClass provided, was:", ec);
}
var titleArr = ec.titlePath.split(".");
var titleColumn = (titleArr.length === 2) ? titleArr[1] : false;
// For this simple test script, we store our API objects in these
// global variables.
var site, job;
// Generates error callbacks.
function fail (why) {
return function (message, error, response) {
var msg;
if (error)
msg = why + ": Node error: "+ error; //util.inspect(error, {showHidden: false, depth: null});
else
msg = why + ": HTTP error: "+ message.statusCode + " " + JSON.stringify(response);
console.error(msg);
onError( new Error(msg) );
}
}
// 1. Log in.
// 2. Create site (unless already created).
// 3. Start an import job.
// 4. Get the server's import preview data.
// 5. Start the import job.
// 6. After one second, check its progress.
Silk.User.signin({ email: config.silkUser, password: config.silkPassword }, afterSignin, fail("signin"));
function afterSignin (r) {
console.log("Signed in as", r.name, "(" + r.email + ")");
Silk.Site.available(name, afterAvailable, fail("available"));
}
function afterAvailable (r) {
if (r) {
console.log("Site available.")
Silk.Site.saveByUri(name, { name: name, template: "default" }, afterSiteSave, fail("site save"));
} else {
console.log("Site already created.");
afterSiteSave();
}
}
function afterSiteSave () {
console.log("Site saved. Uploading JSON");
// Upload the json data
site = Silk.Site.byUri(name);
site.Import.uploadJson(data, afterJobCreation , fail("uploadJson"));
}
function afterJobCreation (r) {
console.log("Created import job");
job = site.Import.byId(r.importId);
// r.settings contains default configuration for this import, it can be overridden.
//// Set the collection of all pages, this accepts a string.
r.settings.collection = ec._id;
if (titleColumn) {
r.settings.title = [{ column: titleColumn }];
}
if (data[0] && data[0]._id) {
r.settings.uri = [{ column: "_id" }];
}
//r.settings.fields =
// // Link all the neighbors to other silk pages
// [ { type: "fact", tag: "Neighbor", text: { column:"neighbors" }, pageUrl: { column: "neighbors" } }
// // Make population a textual value
// , { type: "fact", tag: "Population", text: { column:"population" } }
// // Make flags into images
// , { type: "image", tag: "Flag", url : { column:"flag" } }
// // Free text
// , { type: "paragraph", text : { column : "someText" } }
// ];
console.log("Saving settings");
site.Import.saveById(r.importId, r.settings, afterSettingSaving, fail("setting saving"));
}
function afterSettingSaving (r) {
console.log("Saved settings");
job.getPreview(afterPreview, fail("preview"));
}
function afterPreview (r) {
//console.log("Preview:", r);
console.log("Preview done");
job.start({ overrideExisting : true }, afterStart, fail("start"));
}
var intervalId;
function afterStart (r) {
console.log("Started import.");
intervalId = setInterval(checkProgress, 1000);
}
function checkProgress () {
job.getProgress(function (r) {
//console.log("Progress:", r);
if (r.complete) {
console.log("Finished import");
clearInterval(intervalId);
onFinished();
}
}, fail("progress"));
}
}
}
// Register module
if (typeof window === 'undefined') {
module.exports = silkExport;
} else {
//angular
app.factory('silkExport', function() {
return silkExport;
});
}