This repository was archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharco_setup_luoghi_portali_regionali.js
More file actions
232 lines (168 loc) · 7.68 KB
/
Copy patharco_setup_luoghi_portali_regionali.js
File metadata and controls
232 lines (168 loc) · 7.68 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const MongoClient = require('mongodb').MongoClient;
const nodeRequest = require('request');
const promiseRequest = require('request-promise');
const fs = require('fs');
const process = require('process');
const stringSimilarity = require('string-similarity');
const auth = require('./users/arco/users');
let countNome = 0
let countNomeComune = 0
let countNomeIndirizzo = 0
const Config = require('./config').Config;
if (!fs.existsSync('./users/'+ process.argv[2] + '/queries.js')) {
console.error('Task non trovato')
process.exit(1)
}
const queries = require('./users/'+ process.argv[2] + '/queries');
const parser = require('./users/'+ process.argv[2] + '/parser');
const enrichments = require('./users/'+ process.argv[2] + '/enrichments');
const config = new Config(JSON.parse(fs.readFileSync('./app/js/config/'+ process.argv[2] + '.json')));
parser.configInit(config);
let items;
const database = 'arco';
// Initialize recursive functions
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms))
};
function createOptions(item) {
let res = [];
items.forEach(el => {
if (el.siteLabel.value && item.name && stringSimilarity.compareTwoStrings(el.siteLabel.value, item.name) > 0.8 && el.siteLabel.value.length > 0 && item.name.length > 0 ) {
countNome++
res.push(el)
console.log('trovato per il nome')
} else if ( el.siteLabel.value && item.name && el.comune.value && item.comune && stringSimilarity.compareTwoStrings(el.comune.value, item.comune) > 0.8 && stringSimilarity.compareTwoStrings(el.siteLabel.value, item.name) > 0.6 && el.siteLabel.value.length > 0 && item.name.length > 0) {
countNomeComune++;
res.push(el)
console.log('trovato')
} else if ( el.comune.value && item.comune && stringSimilarity.compareTwoStrings(el.comune.value, item.comune) > 0.8 && stringSimilarity.compareTwoStrings(el.addr.value, item.indirizzo) > 0.6 && el.addr.value !== '' && item.indirizzo !== '') {
countNomeIndirizzo++;
res.push(el)
console.log('trovato')
}
})
return {results: {bindings: res}}
}
function enrichAgent(id, classLabel, driver) {
// Log enrichment
console.log('Enrich: ' + id);
// Compose author query
let queryAuthor = queries.authorSelect(id);
// Make request
return new Promise((resolve, reject) => {
promiseRequest(queryAuthor, (err, res, body) => {
let author;
try {author = parser.parseAuthor(JSON.parse(body))}
catch(e) {return reject(e)}
options = createOptions(author);
parser.parseAuthorOptions(author, [options], (options) => {
let responseObject = {
author: author,
options: options
};
return resolve(responseObject);
});
})
});
}
function retrieveUsers(driver, users) {
return Promise.all(users.map(user => Promise.all(user.map(hash => auth.getUserByHash(driver, hash)))))
}
function recursiveEnrichment(driver, count, total, errors) {
enrichments.getNotEnrichedAgents(driver, 1, (agents) => {
if (agents) {
// Promise enrichments in order to make multiple requests at time
let requests = agents.map(agent => enrichAgent(agent.id, agent.classLabel, driver));
Promise.all(requests).then((responses) => {
// Update counter
let enrichment = responses.map(response => enrichments.storeEnrichment(driver, response));
// Store enrichments
Promise.all(enrichment).then((responses) => {
console.log((count + responses.length) + '/' + total + ' places enriched');
recursiveEnrichment(driver, count + 1, total, 0);
if ( total === count) {
console.log(countNome, countNomeComune, countNomeIndirizzo)
process.exit(0)
}
})
}).catch((err) => {
// Log error
console.error(err);
// Handle
if(errors > 2) {
console.log('Too much errors occurred! Skipping enrichment...');
// Skip enrichment
let skip = agents.map(agent => enrichments.skipEnrichment(driver, agent));
Promise.all(skip).then((responses) => {
console.log('Done!');
recursiveEnrichment(driver, count + responses.length, total, 0);
})
} else {
console.log('An error occurred! Retrying enrichment after ' + ((errors + 1)*10) + ' seconds.');
// Retry after (errors + 1)*10 seconds
sleep((errors + 1)*10000).then(() => {
recursiveEnrichment(driver, count, total, errors + 1);
});
}
});
} else process.exit(0);
})
}
function storeAgents(driver, index, callback) {
nodeRequest(queries.getPlaces(index), (err, res, body) => {
// Store uris
body = JSON.parse(body);
let uris = body.results.bindings.map(el => el.site.value);
if(uris.length > 0){
// Store matches and skips
let matches = body.results.bindings.map(el => el.matches.value).map(el => el.split('###'));
let skips = body.results.bindings.map(el => el.skip.value).map(el => el.split('###'));
matches = matches.map(match => match.filter(el => !!el));
skips = skips.map(skip => skip.filter(el => !!el));
retrieveUsers(driver, matches).then(matchUsers => {
retrieveUsers(driver, skips).then(skipUsers => {
matchUsers = matchUsers.map(match => match.filter(el => !!el));
skipUsers = skipUsers.map(skip => skip.filter(el => !!el));
// Store uris/user map
let uriAndUsers = [];
uris.forEach((uri, index) => {
uriAndUsers.push({
uri: uri,
matches: matchUsers[index].map(user => user.username),
skip: skipUsers[index].map(user => user.username)
});
});
// Store things and callback
enrichments.insertThings(driver, uriAndUsers, () => {
console.log('- Added ' + ((10000*(index)) + uris.length) + ' places');
storeAgents(driver, index + 1, callback);
})
});
});
} else callback();
})
}
if(process.argv.length < 3) {
console.log("Sono necessari più parametri da linea di comando.")
process.exit(1);
}
// Setting up ArCo database
MongoClient.connect("mongodb://localhost:27017/", (err, client) => {
// Handle errors
if(err) throw err;
// Connect to database
let db = client.db(database);
console.log('Connected to database');
console.log('Dropped old collection');
db.collection(process.argv[2]).drop();
nodeRequest(queries.getICCDplaces(), (err, res, body) => {
items = JSON.parse(body).results.bindings;
storeAgents(db, 0, () => {
console.log('Agents stored');
db.collection(process.argv[2]).find({enriched: false, error: {$ne: true}}).count((err, total) => {
if(err) throw err;
recursiveEnrichment(db, 0, total, 0);
});
});
});
});