forked from karlogonzales/conuhack-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·118 lines (88 loc) · 2.99 KB
/
server.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
var express = require("express");
var multer = require('multer');
var fs = require('fs');
var http = require('https');
var app = express();
var Clarifai = require('clarifai');
var clarifai_app = new Clarifai.App(
'CrJS4jQSyV6qWixZuShUq8zLOwrIIz2yFDex87jV',
'5QxnJDzgJlNlp0DgTP68oVaZ1SGRkipN9AT4OKjV'
);
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback) {
callback(null, file.originalname);
}
});
var upload = multer({ storage: storage }).single("userPhoto");
app.get('/',function(req,res){
res.sendFile(__dirname + "/html/index.html");
});
app.post('/api/photo',function(req,res){
upload(req,res,function(err) {
if(err) {
return res.end("Error uploading file.");
}
console.log('file location and name: ', req.file.destination +'/' +req.file.filename);
var filePath = req.file.destination +'/' +req.file.filename;
// send the buffer to claifai api to recognize, return the token
fs.readFile(filePath, function(err, original_data){
var base64Image = original_data.toString('base64');
clarifai_app.models.predict("church", {base64: base64Image}).then(
function(response) {
var itemName = response.data.outputs[0].data.concepts[0];
var str = itemName.name;
console.log(str);
var adjustedName = itemName.name[0].toUpperCase();
for (var i = 1, len = str.length; i < len; i++) {
if (i > 0 && str.charAt(i-1) == ' ' && i > 0 && str.charAt(i-1) != 'a' && str.charAt(i) != 'l') {
adjustedName += str[i].toUpperCase();
}
else {
adjustedName += str[i];
}
}
console.log(adjustedName);
var titleParts = adjustedName.split(" ");
var titleParam = "";
titleParts.map(function(x) {
titleParam += x + "+";
})
titleParam.slice(0, -1);
var wiki_url = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=" + titleParam;
http.get(wiki_url, function(response) {
// Continuously update stream with data
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
// Data reception is done, do whatever with it!
var parsed = body.split("\"extract\":\"");
//console.log(parsed[1]);
var final_text = parsed[1].slice(0, -5);
var final_result = {};
final_result["place"] = adjustedName;
final_result["description"] = final_text;
// delete the file from the middleware
fs.unlinkSync(filePath);
res.end(JSON.stringify(final_result));
});
});
},
// predict function error
function(err) {
console.error(err);
});
});
});
});
// serves static files
app.get(/^(.+)$/, function(req, res){
res.sendFile( __dirname +"/html/"+ req.params[0]);
});
app.listen(3000,function(){
console.log("Working on port 3000");
});