This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plex-place.js
366 lines (296 loc) · 12.1 KB
/
plex-place.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
const API_KEY = process.env.API_KEY || ""; // put your API_KEY from developer.here.com here
// where to put temporary files
const OUTDIR = "c:/temp";
const fs = require("fs");
const path = require("path");
const argv = require("minimist")(process.argv.slice(2));
const request = require("superagent");
const events = require("events");
const ev = new events.EventEmitter();
const unzipper = require("unzipper");
const etl = require("etl");
const csv = require("csvtojson");
const plex = require("./plex.js");
const exif = require("./exif.js");
const iso = require("./iso3166.js");
const usage = `
usage node plex-place.js [-h] [-l] [-g [-f]] [-s jobId]
-h : show this help
-l : list places
-g : get reverse geocoding on all files with GPS coord if not already updated in Plex.
-f : force the reverse geocoding for all images
-n XX : process on X reverse geocode
-c jobId: check that job is completed
-s jobId: set places into Plex from jobId
-d delete all Places from library
--debug : to see various traces
`;
let statProcessing = 0,
exifProcessing = 0; // processing EXIF ongoing
let TheRGC = []; // liste des rgc a calculer
// Need to track when to end EXIF process and close DB connection
// emitted when change in exifProcessing or statProcessing
function evHandler() {
if (argv.debug)
console.log(`exifProcessing ${exifProcessing} `); // eslint-disable-line no-console
if (exifProcessing <= 0 && statProcessing <= 0) {
exif.end();
//console.log("TheRGC", TheRGC);
//console.log("TheRGC length", TheRGC.length);
runBatchGC();
}
}
function findXmlTag(res, tag) {
let regex = new RegExp(`(<${tag}>)([A-z0-9]+)(</${tag}>)`);
let id = res.match(regex);
if (id) return id[2];
else return null;
}
function gid2Filename(gid) {
return path.join(OUTDIR, "bgc_" + gid + ".txt");
}
/**
* prend le ficher TheRGC et lance le batch reversege geocoding
*/
function runBatchGC() {
if (TheRGC.length == 0) {
console.log("nothing to batch geocode"); // eslint-disable-line no-console
return;
}
let url = ["https://batch.geocoder.ls.hereapi.com/6.2/jobs?",
"apiKey=", API_KEY,
"&mode=retrieveAddresses",
"&action=run",
"&header=true",
"&inDelim=|",
"&outDelim=|&outCols=city,county,district,country",
"&outputcombined=true",
"&language=en"
].join("");
let i = 0;
let body = "recId|prox\n" + TheRGC.map(elt => {
return `${i++}|${elt.latlng}`;
}).join("\n");
request.post(url)
.send(body)
.set("Content-Type", "text/plain")
// .set('Accept', 'application/xml')
.then(res => {
let result = res.body.toString();
//console.log(result);
// extrait ReqestId
const gid = findXmlTag(result, "RequestId");
if (!gid) {
console.error("No RequestId found");
return;
}
console.log(); // eslint-disable-line no-console
console.log(`${TheRGC.length} reverse geocodes sent`); // eslint-disable-line no-console
console.log(`to check status: node plex-place.js -c ${gid} `); // eslint-disable-line no-console
// write temp file with the request to batch geocoder
const matching = TheRGC.map(elt => elt.ids).join("\n");
const fileOut = gid2Filename(gid);
fs.writeFile(fileOut, matching, (err) => {
if (err) throw err;
});
})
.catch(err => {
console.error("Error requesting batch geocode", err.message);
});
}
// get result of batch geocoding, match to correspondance file and add EXIF
// tag_valye : 10:country 20:region 30:city 40: 50: road
function listPlaces() {
plex.init();
let places = plex.scanPlacesTags();
console.log("Places ", places); // eslint-disable-line no-console
console.log("Number of Countries (10)", places.filter(place => place.tag_value == 10).length); // eslint-disable-line no-console
console.log("Number of Region (20)", places.filter(place => place.tag_value == 20).length); // eslint-disable-line no-console
console.log("Number of City (30)", places.filter(place => place.tag_value == 30).length); // eslint-disable-line no-console
console.log("Number of Urban Area (40)", places.filter(place => place.tag_value == 40).length); // eslint-disable-line no-console
console.log("Number of Streets/POI (50)", places.filter(place => place.tag_value == 50).length); // eslint-disable-line no-console
plex.end();
}
// get result of batch geocoding, match to correspondance file and add EXIF
function addAddresses(gid) {
plex.init();
plex.scanPlacesTags();
// now get result of batch geocoding
let url = ["https://batch.geocoder.ls.hereapi.com/6.2/jobs/",
gid,
"/result?",
"apiKey=", API_KEY
].join("");
// read matching file
const fileOut = gid2Filename(gid);
let data = null;
try {
data = fs.readFileSync(fileOut, "utf8");
} catch (err) {
console.error(err.message);
}
if (!data)
return;
const matching = data.split("\n");
// lit et dezippe la réponse
// une seulle entrée ou plusieurs ? not clear in HERE batch geoding dcoument
request.get(url)
.pipe(unzipper.Parse())
.pipe(etl.map(async entry => {
const content = await entry.buffer();
const txt = content.toString();
let result = [],
empty = [];
csv({
noheader: false,
delimiter: "|"
})
.fromString(txt)
.subscribe((json) => {
if (json.SeqNumber == "1")
result.push(json);
if (json.seqLength == "0") // no result for this entry
empty.push(json);
//console.log("json ",json);
//jsonObj: {"person.number":1234,"person":{"comment":"hello"}}
})
.on("done", () => {
//if (result.length > 0) console.log("reverse geocoding result", result);
//if (empty.length > 0) console.log("reverse geocoding empty results", empty);
result.forEach(rec => {
let country = iso.whereAlpha3(rec.country).country;
const addr = {
city: rec.city,
county: rec.county,
district: rec.district,
country: country
};
let mids = matching[rec.recId].split(","); // get list of mids
//console.log(mids, addr); // eslint-disable-line no-console
mids.forEach(mid => {
plex.deletePlaceTags(mid); // delete existing tags for this image
plex.addPlaceTags(mid, addr); // add new tags
//console.log(mid, addr);
});
});
//mark empty answers as processed with timestamp into Plex
empty.forEach(rec => {
let mids = matching[rec.recId].split(","); // get list of mids
mids.forEach(mid => {
plex.updatePlaceImageTimestamp(mid); // delete existing tags for this image
//console.log(mid, addr);
});
});
// clean Place tags not referenced anywhere
plex.cleanLonePlaceTags();
});
}));
}
// check if result is available
function checkResultAvailable(gid) {
// now get result of batch geocoding
let url = ["https://batch.geocoder.ls.hereapi.com/6.2/jobs/",
gid,
"?action=status",
"&apiKey=", API_KEY
].join("");
// lit et dezippe la réponse
// une seulle entrée ou plusieurs ? not clear in HERE batch geoding dcoument
request.get(url)
.then(status => {
const result = status.body.toString();
//console.log("status ", result);
console.error("Status ", findXmlTag(result, "Status")); // eslint-disable-line no-console
console.error("TotalCount ", findXmlTag(result, "TotalCount")); // eslint-disable-line no-console
console.error("ValidCount ", findXmlTag(result, "ValidCount")); // eslint-disable-line no-console
console.error("InvalidCount ", findXmlTag(result, "InvalidCount")); // eslint-disable-line no-console
console.log(`\nWhen status completed: node plex-place.js -s ${gid} `); // eslint-disable-line no-console
})
.catch(err => {
console.error("Error checking batch job", err.message);
});
}
function DoMainScan() {
plex.init();
let recs = plex.scanPhotos();
// eslint-disable-next-line no-console
console.log("Total photos", recs.length, "\n");
if (recs.length == 0)
return;
plex.addColumnPlaceUpdate();
function doTheUpdate(rec) {
//console.log("doTheUpdate ", rec.file,count,exifProcessing);
//let ptags = plex.getPlaceTags(rec.mid);
exifProcessing++;
exif.getFromImage(rec.file).then(tags => {
if (tags.pos) {
count++;
const latlng = tags.pos.lat + "," + tags.pos.lng;
let elt = TheRGC.find(n => n.latlng == latlng);
if (elt)
elt.ids.push(rec.mid);
else
TheRGC.push({
latlng: latlng,
ids: [rec.mid],
file: rec.file
});
} else
plex.updatePlaceImageTimestamp(rec.mid); // updated with no position
exifProcessing--;
ev.emit("exif");
}).catch(err => {
console.log(err.message); // eslint-disable-line no-console
exifProcessing--;
ev.emit("exif");
});
}
console.log(`${recs.length} photos under review`); // eslint-disable-line no-console
let count = 0;
recs.forEach(rec => {
//console.log(rec);
if (argv.n && count >= argv.n)
return;
count++;
if (!rec.PlaceUpdateTime) rec.PlaceUpdateTime = 0;
let datePlaceUpdate = Date.parse(rec.PlaceUpdateTime);
if (argv.f) // force the update
doTheUpdate(rec);
else {
statProcessing++;
fs.stat(rec.file, (err, stat) => {
statProcessing--;
if (stat && stat.mtimeMs > datePlaceUpdate)
doTheUpdate(rec);
ev.emit("stat");
});
}
});
//ready to track stat and exif messages
ev.on("stat", evHandler);
ev.on("exif", evHandler);
}
/******************** So what do we do with all that ?********* */
if (!API_KEY) {
console.log("Missing credentials !"); // eslint-disable-line no-console
console.log("1/ create credentials from https://developer.here.com"); // eslint-disable-line no-console
console.log("2/ add API_KEY as environment variable or put it into file plex-place.js"); // eslint-disable-line no-console
process.exit(0);
}
if (argv.h) {
console.log(usage); // eslint-disable-line no-console
process.exit(0);
}
if (argv.c)
checkResultAvailable(argv.c);
if (argv.l)
listPlaces();
if (argv.d) {
plex.init();
plex.deleteAllPlaceTags();
plex.end();
}
if (argv.g)
DoMainScan();
if (argv.s)
addAddresses(argv.s);