-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplex-geo.js
More file actions
389 lines (314 loc) · 10.8 KB
/
plex-geo.js
File metadata and controls
389 lines (314 loc) · 10.8 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"use strict";
require("dotenv").config();
const debug = require("debug")("Geo");
require("colors");
const fsPromises = require("fs").promises;
const yesno = require("yesno");
const API_KEY = process.env.API_KEY || ""; // put your API_KEY from developer.here.com here
// where to put temporary files
const TMP = process.env.TMP || "c:/temp";
const MAX_QUEUE = 5000; // max number of image exif to be put in queue
const fs = require("fs");
const path = require("path");
const argv = require("minimist")(process.argv.slice(2));
const request = require("superagent");
const unzipper = require("unzipper");
const etl = require("etl");
const csv = require("csvtojson");
const plex = require("./js/plex.js");
const exif = require("./js/exif.js");
const iso = require("./js/iso3166.js");
const plexPlaces = require("./js/plex-place.js");
const usage = `
usage node plex-geo.js [-h] [-l] [-g [-f]] [-s jobId]
-h : show this help
--list list existing places
--check jobId check that job is completed
--set jobId set places into Plex from jobId
--delete delete all Places from library
--debug to see various traces
`;
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(TMP, "bgc_" + gid + ".txt");
}
/**
* prend le ficher TheRGC et lance le batch reversege geocoding
*/
function runBatchGC() {
if (ForRGC.length == 0)
return console.log("No new GPS location to convert"); // eslint-disable-line no-console
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("");
// limit to a certain limit of RGC to void overlaod of transactions but also not too big requests on Plex Media Server, even though limit is unclear
if (ForRGC.length > MAX_QUEUE) ForRGC.length = MAX_QUEUE;
let i = 0;
let body =
"recId|prox\n" +
ForRGC.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)
return console.error("No RequestId found");
console.log(); // eslint-disable-line no-console
console.log(`${ForRGC.length} coords sent for reverse geocoding`); // eslint-disable-line no-console
console.log(`To check status: node plex-geo.js --check ${gid} `); // eslint-disable-line no-console
// write temp file with the request to batch geocoder
const matching = ForRGC.map(elt => elt.mids).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
function addPlaces(gid) {
debug("addPlaces");
// 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 = for each line, a comma list mids
const fileOut = gid2Filename(gid);
let data = null;
try {
data = fs.readFileSync(fileOut, "utf8");
} catch (err) {
console.error(err.message);
}
if (!data) return;
// each line contains one or several mid
// mid = media_items.metadata_item_id
const mids = data.split("\n");
const allmids = data.match(/\d+/g);
// lit et dezippe la réponse
// une seule entrée ou plusieurs ? not clear in HERE batch geocoding dcoument
request
.get(url)
.pipe(unzipper.Parse())
.pipe(
etl.map(async (entry) => {
const content = await entry.buffer();
const txt = content.toString();
let tags = [],
empty = [];
csv({
noheader: false,
delimiter: "|",
})
.fromString(txt)
.subscribe((json) => {
if (json.SeqNumber == "1") tags.push(json);
if (json.seqLength == "0")
// no result for this entry
empty.push(json);
})
.on("done", async () => {
// console.log("unzipped the result of rgc");
// for backup purpose, write result of rgc
const fileRgc = `${fileOut}_result.json`;
fs.writeFile(fileRgc, JSON.stringify(tags, null, 2), (err) => {
if (err) console.error(`Error writing ${fileRgc}`, err);
//console.log(`Note: RGC results written in file ${fileRgc}`);
});
// collect all tags,and add country as full text
tags = tags.map((rec) => {
rec.country = iso.whereAlpha3(rec.country).country;
return rec;
});
// add the new places
await plexPlaces.addPlaces(mids, tags);
// mark all the mids as updated
await plex.markImagesAsUpdated(allmids, ["PLACE"]);
});
})
);
}
// check if result is available
function checkResultAvailable(gid) {
if (!gid || typeof gid !== 'string')
return console.log(usage);
// 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 seule 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(
`\nOnce status is completed, run:
node plex-geo.js --set ${gid} `
); // eslint-disable-line no-console
})
.catch((err) => {
console.error("Error checking batch job", err.message);
});
}
// array of {latlng: "lat,lng" ,ids: [rec.mid],file} where mid = media_items.metadata_item_id
let ForRGC = [];
// array of mids
let NoRGC = [];
let loopId;
function consoleSameLine(txt) {
process.stdout.clearLine(); // clear current text
process.stdout.cursorTo(0);
process.stdout.write(txt);
}
/**
* look for GPS coordinate in the image, adnd mark for RGC or no RGC
* RGC : add in ForRGC
* No RGC : add in NoRGC
* @param {*} rec
* @returns a promise with 1 if new RGC to do, 0 otherwise
*/
function markForRGC(rec) {
//console.log("markForRGC ", rec);
return exif.analyze(rec.file).then(tags => {
if (tags.pos == null) {
NoRGC.push(rec.mid); // to be updated with no position
return { add: 0, id: loopId };
}
const elt = ForRGC.find(n => n.latlng == tags.pos.latlng); // check if we have it already in the latlng ot process
if (elt) { //we have the location already in the table TheRGC, no need to rgc again
elt.mids.push(rec.mid); // add the media_item_id of table media_items
return { add: 0, id: loopId };
}
else {
ForRGC.push({
latlng: tags.pos.latlng,
mids: [rec.mid],
file: rec.file,
});
return { add: 1, id: loopId };
}
})
.catch(console.error);
}
/**
* Go through all Images to see which one to RGC
* @returns
*/
async function scanForRGC() {
let recs = plex.listPhotosPatched();
console.log("Total photos under review", recs.length, "\n");
if (recs.length == 0) return;
// go through all images
let promises = [];
let count = 0; //how many RGc to do have we received
let stop = false;
let promisesProcessed = 0;
loopId = 0;
do {
if (stop) {
console.log();
console.log("we have reached the maximum queue size. Iterate on the same process to complete");
break;
}
const rec = recs[loopId];
if (!rec.PlaceUpdateTime) rec.PlaceUpdateTime = 0; // no update time
const datePlaceUpdate = Date.parse(rec.PlaceUpdateTime);
consoleSameLine(`${loopId + 1}/${recs.length} scanned, Queuing: ${promisesProcessed}/${promises.length}, ${count} ForRGC / ${NoRGC.length} NoRGC`);
// what is update time of the file ?
const stat = await fsPromises.stat(rec.file);
if (stat.mtimeMs > datePlaceUpdate) { // file most recent than last Place Update
//console.log(`${count} for RGC / ${i} scanned ${rec.file} fresher than PlaceUpdate `, stat.mtimeMs, datePlaceUpdate);
consoleSameLine(`${loopId + 1}/${recs.length} scanned, Queuing: ${promisesProcessed}/${promises.length}, ${count} ForRGC / ${NoRGC.length} NoRGC`);
const p = markForRGC(rec);
promises.push(p);
if (promises.length >= MAX_QUEUE) stop = true;
p.then(elt => {
count += elt.add;
promisesProcessed++;
consoleSameLine(`${elt.id + 1}/${recs.length} scanned, Queuing: ${promisesProcessed}/${promises.length}, ${count} ForRGC / ${NoRGC.length} NoRGC`);
});
}
} while (loopId++ < recs.length - 1);
//wait for all promises
await Promise.all(promises);
console.log();
console.log(`Ending exif engine`);
exif.end(); // we terminate the exif engine
console.log(`ForRGC`, ForRGC.length);
console.log(`NoRGC`, NoRGC.length);
//return;
plex.markImagesAsUpdated(NoRGC, ["PLACE"]);
// we are good to run the batch RGC
runBatchGC();
}
/******************** So what do we do with all that ?********* */
if (!API_KEY) {
console.log(`Missing credentials !`.red);
console.log(`1/ create credentials from https://developer.here.com`.yellow);
console.log(
`2/ add API_KEY as environment variable or put it into file plex-place.js`
.yellow
);
process.exit(0);
}
if (argv.h || argv.help) {
console.log(usage); // eslint-disable-line no-console
process.exit(0);
}
if (argv.check) checkResultAvailable(argv.check);
if (argv.list) {
plex.init();
const resp = plex.listFancyPlaces();
plex.end();
console.log(resp);
}
async function deleteAllPlaces() {
const ok = await yesno({
question: 'Are you sure you want to delete ALL places in the Plex database ?'
});
if (!ok) return;
plexPlaces.deleteAllPlaces();
}
if (argv.delete) deleteAllPlaces();
if (argv.rgc) {
plex.init();
scanForRGC();
plex.end();
}
if (argv.set) addPlaces(argv.set);