-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·146 lines (124 loc) · 3.69 KB
/
index.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
#!/usr/local/bin/iojs
/* eslint no-console: [0] */
'use strict';
const cmd = require('nomnom')
.script('npm start')
.options({
env: {
help: 'Nasjonal Turbase environment',
metavar: 'ENV',
default: 'dev',
},
limit: {
abbr: 'l',
hidden: true,
metavar: 'N',
help: 'Limit number of results per page',
default: 50,
},
skip: {
abbr: 's',
hidden: true,
metavar: 'M',
help: 'Number of results to skip before starting',
default: 0,
},
'no-debug': {
flag: true,
help: 'Do not print debugging info',
},
});
const opts = cmd.parse();
process.env.NTB_API_ENV = opts.env;
const turbasen = require('turbasen');
const async = require('async');
const upload = require('./lib/upload');
const log = require('./lib/log');
const options = {
status: '!Slettet',
'img.0.url': '',
fields: 'img,endret,status,geojson',
sort: 'endret',
limit: opts.limit,
skip: opts.skip,
};
let documents = [];
async.during(function test(callback) {
console.log(options.skip);
turbasen.bilder(options, function getBilder(err, res, body) {
options.skip += 50;
documents = body.documents;
callback(err, documents.length > 0);
});
}, function sync(callback) {
async.eachSeries(documents, function eachSeries(document, cb) {
const url = upload.getImage(document);
const _id = document._id;
if (!url) {
log('turbasen_skipped.txt', document._id, document.status);
return setTimeout(cb, 0);
}
console.log(_id, 'pre upload', url);
upload(url, function uploadCallback(shasum, err, res, body) {
if (err) {
console.error(err);
log('jotunheimr_error.txt', _id, url, err.message);
return setTimeout(cb, 0);
}
if (res.statusCode !== 201) {
log('jotunheimr_failed.txt', _id, url, res.statusCode);
return setTimeout(cb, 0);
}
console.log(_id, 'post upload', res.statusCode);
// Some images have no exif data
if (!body.meta.exif) {
body.meta.exif = {};
}
const data = {
original: {
size: body.meta.size,
sha1: shasum.digest('hex'),
format: body.meta.format,
colorspace: body.meta.colorspace,
height: body.meta.height,
width: body.meta.width,
},
exif: {
Artist: body.meta.exif.Artist,
Copyright: body.meta.exif.Copyright,
DateTime: body.meta.exif.DateTime,
DateTimeDigitized: body.meta.exif.DateTimeDigitized,
DocumentName: body.meta.exif.DocumentName,
ImageDescription: body.meta.exif.ImageDescription,
Make: body.meta.exif.Make,
Model: body.meta.exif.Model,
Software: body.meta.exif.Software,
},
img: body.versions
}
// Attach geometry if the image file on disk has GPS coordinates but the
// image in Nasjonal Turbase does not
if (body.meta.geojson && !document.geojson) {
data.geojson = body.meta.geojson;
}
// Patch the image in Nasjonal Turbase
turbasen.bilder.patch(_id, data, function patchCallback(err, res, body) {
if (err) {
console.error(err);
log('turbasen_error.txt', _id, url, err.message);
return setTimeout(cb, 0);
}
if (res.statusCode !== 200) {
log('turbasen_failed.txt', _id, url, res.statusCode, body.message);
return setTimeout(cb, 0);
}
console.log(_id, 'post patch', res.statusCode);
log('turbasen_success.txt', _id);
return setTimeout(cb, 0);
});
});
}, callback);
}, function done(err) {
console.log('Done!');
console.log(err);
});