-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (145 loc) · 4.7 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//adapted from https://github.com/jettro/nodejs-photo-indexer
//usage: node . --photodir /Volumes/Photos --hostandport es-instance:9200
'use strict';
var walk = require('walk');
var fs = require('fs')
var path = require('path');
var exif = require('exif2');
var readline = require('readline');
var elasticsearch = require('elasticsearch')
var argv = require('minimist')(process.argv.slice(2));
var mapping = require('./mapping.json');
var startDir = argv.photodir;
var esFlushBufferLength = 100;
var hostAndPort = argv.hostandport;
var indexName = 'photos';
var docType = 'photo';
var items = [];
// First create the Indices
var client = new elasticsearch.Client({
host: hostAndPort
});
client.indices.create({
index: indexName,
body: {},
ignore: [400] //This lets us ignore the error when the index already exists.
}).then(
function (body) {
mapping.index = indexName;
mapping.type = docType;
console.log("create index!");
client.indices.putMapping(mapping).then(function (body) {
console.log("Put mapping !");
}, function (err) {
console.log(err);
});
},
function (err) {
console.error(err);
}
);
// Go through each directory and extract data
var walker = walk.walk(startDir);
walker.on('file', function (root, stat, next) {
console.log("Walk " + stat.name);
// Add this file to the list of files
var name = stat.name.toLowerCase();
if (name.indexOf('.jpg') > -1 || name.indexOf('.jpeg') > -1) {
extractData(root + '/' + stat.name, next);
} else {
next();
}
});
walker.on('end', function() {
flushItems(items);
console.log('done!!!!!!');
});
/* This is the core work horse that calls the
functions to get the data from the images an add
it to a search object */
function extractData(file, next) {
exif(file, function (err, obj) {
if (err) {
console.error(err);
next();
}
else {
//console.log(obj);
var searchObj = {};
searchObj.id = file;
searchObj.tags = obj['subject'];
searchObj.people = obj['region name'];
searchObj.dateTaken = obj["create date"];
searchObj.file_name = obj["file name"];
searchObj.directory = obj["directory"];
searchObj.path = obj["directory"] + '/' + obj["file name"];
searchObj.file_size = obj["file size"];
searchObj.make = obj["make"];
searchObj.camera_model_name = obj["camera model name"];
searchObj.orientation = obj["orientation"];
searchObj.flash = obj["flash"];
searchObj.lens = obj["lens"];
searchObj.aperture = obj["aperture"];
searchObj.megapixels = obj["megapixels"];
searchObj.x_resolution = obj["x resolution"];
searchObj.y_resolution = obj["y resolution"];
searchObj.resolution_unit = obj["resolution unit"];
searchObj.focal_length = obj["focal length"];
searchObj.focus_position = obj["focus position"];
searchObj.focus_distance = obj["focus distance"];
searchObj.lens_f_stops = obj["lens f stops"];
searchObj.shutter_speed = obj["shutter speed"];
searchObj.depth_of_field = obj["depth of field"];
searchObj.GPS_Altitude = obj["gps altitude"];
searchObj.GPS_Date_Time = obj["gps date/time"];
searchObj.GPS_Latitude = obj["gps latitude"];
searchObj.GPS_Longitude = obj["gps longitude"];
searchObj.gps_altitude = obj["gps altitude"];
obj["gps position"] > "" ? searchObj.location = gpstodd(obj["gps position"]) : 1;
sendToElasticsearch(searchObj, next);
}
});
};
// Convert from GPS Degrees in EXIF to Degree Decimal so the ES understands the GPS
function gpstodd(input) {
input = input.replace(/\'/g, " min").replace(/\"/g, ' sec').replace(/\,/g, "").split(" ")
var lat = (parseFloat(input[0]) + parseFloat(input[2] / 60) + parseFloat(input[4] / (60 * 60))) * (input[6] == "S" ? -1 : 1);
var lng = (parseFloat(input[7]) + parseFloat(input[9] / 60) + parseFloat(input[11] / (60 * 60))) * (input[13] == "W" ? -1 : 1);
//console.log(searchObj)
return {
"lat": lat,
"lon": lng
}
}
//Collect and Flsuh using the Bulk Index
function sendToElasticsearch(searchObj, next) {
console.log("Sending to elastic");
//We'll do an upsert here b/c we don't which feature will return first
items.push({
"update": {
"_id": searchObj.id
}
}, {
"doc": searchObj,
"doc_as_upsert": true
});
//console.log(items);
if (items.length >= 100) {
var new_items = items
flushItems(new_items, next);
new_items = [];
items = [];
}
}
function flushItems(new_items, next) {
console.log("Flushing items");
client.bulk({
index: indexName,
type: docType,
body: new_items
}, function (err, response) {
if (err) console.error(err);
console.log(response);
next();
});
}