-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploader.js
180 lines (168 loc) · 5.45 KB
/
uploader.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const CKAN = require('ckan');
function errorGroupExists(error) {
return (error &&
error.__type === 'Validation Error' &&
error.name &&
error.name[0] === 'Group name already exists in database');
}
function errorUrlExists(error) {
return (error &&
error.__type === 'Validation Error' &&
error.name &&
error.name[0] === 'That URL is already in use.');
}
module.exports = function createUploader() {
const apiToken = process.env.CKAN_API_TOKEN;
const client = new CKAN.Client(
process.env.CKAN_URL + '/en',
apiToken
);
function parseDate(stringDate) {
const match = stringDate.match(/(\d\d)\.(\d\d)\.(\d\d\d\d)\ (\d\d):(\d\d)/);
const day = match[1];
const month = match[2];
const year = match[3];
const hh = match[4];
const mm = match[5];
return year + '-' + month + '-' + day + ' ' + hh + ':' + mm;//new Date(year, month, day, hh, mm);//.toISOString();
}
return {
createResource(revision) {
return new Promise(function(resolve, reject) {
let name = revision.title || revision.description;
if(revision.revision_created) {
name = revision.revision_created + ' ' + name;
}
let data = {
package_id: 'data-gov-ua-node-' + revision.dataset_node_id,
url: revision.base_url + revision.url,
revision_id: 'data-gov-ua-revision-' + revision.revision_id,
id: 'data-gov-ua-revision-' + revision.revision_id,
name: name,
description: revision.description,
format: revision.format,
mimetype: revision.filemime,
size: revision.filesize,
};
if(revision.revision_created) {
data['created'] = parseDate(revision.revision_created);
}
client.action('resource_show', { id: data.id }, function(err, result) {
if(result.error && result.error.message === 'Not found') {
console.log('creating ' + revision.revision_id);
client.action('resource_create', data, function(err, result) {
if(errorUrlExists(result.error)) {
console.log(data.id + ' already exists, updating');
client.action('resource_update', data, function(err, result) {
if(err) {
reject(err);
} else {
resolve();
}
});
} else {
if(result.error) {
reject(result.error)
} else {
resolve();
}
}
});
} else {
console.log(revision.revision_id + ' already exists, skipping (TODO: compare fields)');
resolve();
}
});
});
},
createDataset(revision) {
return new Promise(function(resolve, reject) {
let data = {
name: 'data-gov-ua-node-' + revision.dataset_node_id,
title: revision.title || revision.description,
maintainer: revision.responsible_person.name,
maintainer_email: revision.responsible_person.email,
owner_org: 'data-gov-ua-' + revision.organization_id,
state: 'active',
};
client.action('package_create', data, function(err, result) {
if(errorUrlExists(result.error)) {
console.log(data.name + ' already exists, updating');
client.action('package_show', { id: data.name }, function(err, result) {
if(err) {
reject(err);
} else {
data = Object.assign(result.result, data);
client.action('package_update', data, function(err, result) {
if(err) {
reject(err);
} else {
resolve();
}
});
}
})
} else {
if(result.error) {
reject(result.error)
} else {
resolve();
}
}
});
});
},
createOrganization(org) {
return new Promise(function(resolve, reject) {
let extras = [
{
key: 'phone',
value: org.phone,
},
{
key: 'category',
value: org.category,
},
];
if(org.website) {
extras.push({
key: 'website',
value: org.website,
})
};
if(org.address) {
extras.push({
key: 'address',
value: org.address,
});
};
let data = {
name: 'data-gov-ua-' + org.organization_id,
id: 'data-gov-ua-' + org.organization_id,
title: org.organization_name,
state: 'active',
extras: extras,
};
client.action('organization_create', data, function(err, result) {
if(errorGroupExists(result.error)) {
console.log(data.id + ' already exists, updating');
client.action('organization_update', data, function(err, result) {
if(err) {
reject(err);
} else {
resolve();
}
});
} else {
console.log('created ' + org.organization_id);
if(result.error) {
reject(result.error)
} else {
resolve();
}
}
});
});
}
}
}