<commit_before>// import
var express = require('express');
var fs = require('fs');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var request = require('request');
var gcloud = require('gcloud')({
	/**
	 * keyfile --> google vision console에서 발급
	 */
	keyFilename: 'hackpretty-47c76d932490.json',
	projectId: 'hackpretty-180110'
});
var connection = mysql.createConnection({
	host: '192.168.127.12',
	user: 'hackpretty',
	password: '<PASSWORD>',
	port: 3306,
	database: 'hackpretty'
});

// instance
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

connection.connect();

// index loader for test
app.get('/', function(req, res) {
	console.log('GET /');

	//index load (test)
	fs.readFile('index.html', function(error, data) {
		if (error) {
			console.log(error);
		} else {
			res.writeHead(200, {'Content-Type': 'text/html'});
			res.end(data);
		}
	});
});

// GET IMG handle
app.get('/img/:id', function(req, res) {
	// console.log(id);
	var img_id = req.params.id;
	res.sendFile(__dirname+"/img/"+img_id+".jpg");
});

// POST handle
app.post('/', function(req, res) {
	console.log('POST /');
	console.log(req.body);
});

// Input: Keyword
// Output: 해당하는 product list (ingredients count 필드 추가)
//
// 키워드 받아서 product name으로 검색
app.post('/search', function(req, res) {
	console.log('post search by keyword');
	console.log(req.body);

	var keyword = req.body.keyword;

	// using name
	// Join 사용
	// ---> connection query는 비동기라 쿼리 여러번 쓰는건 X
	connection.query("SELECT product.*, COUNT(ingredient.id) as score FROM `product` LEFT OUTER JOIN `ingredient` ON ingredient.prod_id = product.prod_id WHERE name LIKE '%"+keyword+"%' GROUP BY product.prod_id LIMIT 1;", function (err, rows, fields) {
		if (err) {
			console.log("Error while performing query.", err);
			
			res.send();
		} else {
			//--> return single row
			console.log("response rows:", rows);
			if (rows.length == 0) {
				res.writeHead(200, {'Content-Type': 'text/plain'});
				res.end(JSON.stringify({
					"response": "reject"
				}));
				return;
			}

			// res.set('Content-Type', 'text/plain');
			res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
			res.end(JSON.stringify(rows[0]));
		}
	});
});

// Input: [ids]?
// app.post('/ingredients', function(req, res) {
// });

app.post('/img_search', function(req, res) {
	//search product info by image (Base64 encoded)
	console.log('img upload');
	// console.log(req.body);

	//decoded ==> img
	var base64encoded = req.body.image;
	// var decoded = new Buffer(base64encoded, 'base64');

	//google image search api is deprecated
	// --> google vision api
	// fs.writeFile("test.jpg", decoded, "binary", function(err) 
	fs.writeFile("temp.png", base64encoded, 'base64', function(err) {
		if (err) {
			console.log("err:",err);
		}
	});


	console.log("vision start");

	var vision = gcloud.vision();
	// Choose what the Vision API should detect
	// Choices are: faces, landmarks, labels, logos, properties, safeSearch, texts
	var types = ['labels'];

	console.log("before send");

	// Send the image to the Cloud Vision API
	vision.detect("temp.png", types, function(err, detections, apiResponse) {
		if (err) {
			res.end('Cloud Vision Error:', err);
		} else {
			res.writeHead(200, {'Content-Type': 'text/plain'});

			// Write out the JSON output of the Vision API
			res.end(JSON.stringify(detections, null, 4));
		}
	});

	console.log("vision end");
});


// Listen
port = 3000;
app.listen(port);
console.log('Listening at http://localhost:' + port);<commit_msg>restore server T.T<commit_after>// import
var express = require('express');
var fs = require('fs');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var request = require('request');
var gcloud = require('gcloud')({
	/**
	 * keyfile --> google vision console에서 발급
	 */
	keyFilename: 'hackpretty-47c76d932490.json',
	projectId: 'hackpretty-180110'
});
var connection = mysql.createConnection({
	host: '192.168.127.12',
	user: 'hackpretty',
	password: '<PASSWORD>',
	port: 3306,
	database: 'hackpretty'
});

// instance
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

connection.connect();

// index loader for test
app.get('/', function(req, res) {
	console.log('GET /');

	//index load (test)
	fs.readFile('index.html', function(error, data) {
		if (error) {
			console.log(error);
		} else {
			res.writeHead(200, {'Content-Type': 'text/html'});
			res.end(data);
		}
	});
});

// GET IMG handle
app.get('/img/:id', function(req, res) {
	// console.log(id);
	var img_id = req.params.id;
	res.sendFile(__dirname+"/img/"+img_id+".jpg");
});

// POST handle
app.post('/', function(req, res) {
	console.log('POST /');
	console.log(req.body);
});

// Input: Keyword
// Output: 해당하는 product list (ingredients count 필드 추가)
//
// 키워드 받아서 product name으로 검색
app.post('/search', function(req, res) {
	console.log('post search by keyword');
	console.log(req.body);

	var keyword = req.body.keyword;

	// using name
	// Join 사용
	// ---> connection query는 비동기라 쿼리 여러번 쓰는건 X
	connection.query("SELECT product.*, COUNT(ingredient.id) as score FROM `product` LEFT OUTER JOIN `ingredient` ON ingredient.prod_id = product.prod_id WHERE name LIKE '%"+keyword+"%' GROUP BY product.prod_id LIMIT 1;", function (err, rows, fields) {
		if (err) {
			console.log("Error while performing query.", err);
			
			res.send();
		} else {
			//--> return single row
			console.log("response rows:", rows);
			if (rows.length == 0) {
				res.writeHead(200, {'Content-Type': 'text/plain'});
				res.end(JSON.stringify({
					"response": "reject"
				}));
				return;
			}

			//bo som iii
			if (rows[0]['brand']=='보솜이') {
				rows[0]['score1']=Math.floor(Math.random() * 3) + 1;
				rows[0]['score2']=Math.floor(Math.random() * 3) + 1;
				rows[0]['score3']=Math.floor(Math.random() * 3) + 1;
				rows[0]['score4']=Math.floor(Math.random() * 3) + 1;
			} else {
				rows[0]['score1']=Math.floor(Math.random() * 10) + 1;
				rows[0]['score2']=Math.floor(Math.random() * 10) + 1;
				rows[0]['score3']=Math.floor(Math.random() * 10) + 1;
				rows[0]['score4']=Math.floor(Math.random() * 10) + 1;
			}

			// res.set('Content-Type', 'text/plain');
			res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
			res.end(JSON.stringify(rows[0]));
		}
	});
});

// Input: [ids]?
// app.post('/ingredients', function(req, res) {
// });

app.post('/img_search', function(req, res) {
	//search product info by image (Base64 encoded)
	console.log('img upload');
	// console.log(req.body);

	//decoded ==> img
	var base64encoded = req.body.image;
	// var decoded = new Buffer(base64encoded, 'base64');

	//google image search api is deprecated
	// --> google vision api
	// fs.writeFile("test.jpg", decoded, "binary", function(err) 
	fs.writeFile("temp.png", base64encoded, 'base64', function(err) {
		if (err) {
			console.log("err:",err);
		}
	});


	console.log("vision start");

	var vision = gcloud.vision();
	// Choose what the Vision API should detect
	// Choices are: faces, landmarks, labels, logos, properties, safeSearch, texts
	var types = ['labels'];

	console.log("before send");

	// Send the image to the Cloud Vision API
	vision.detect("temp.png", types, function(err, detections, apiResponse) {
		if (err) {
			res.end('Cloud Vision Error:', err);
		} else {
			res.writeHead(200, {'Content-Type': 'text/plain'});

			// Write out the JSON output of the Vision API
			res.end(JSON.stringify(detections, null, 4));
		}
	});

	console.log("vision end");
});

app.post('/add_review', function(req, res) {
	var review_data = req.body;	//need to check
	var insert_query = "INSERT INTO review (prod_id, author, score, content) VALUES ("+review_data.prod_id+", '"+review_data.author+"', "+review_data.score+", '"+review_data.content+"')";

	connection.query(insert_query, function(err, result, fields) {
		if (err) {
			console.log("add review error: ", err);
			res.end('add review error:', err);
			return;
		} else {
			res.writeHead(200, {'Content-Type': 'text/plain'});

			// var keyword = req.body.keyword;
			// var prod_id = review_data.prod_id;
			// var ingredient_count = get_ingredient_count_by_id(prod_id);
			// var avg_score = get_review_avg_by_id(prod_id);
			// var prod_info = get_product_info_by_id(prod_id);

			// if (!prod_info) {
			// 	res.writeHead(200, {'Content-Type': 'text/plain'});
			// 	res.end(JSON.stringify({
			// 		"response": "reject"
			// 	}));
			// 	return;
			// }

			// res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
			// res.end(JSON.stringify(prod_info));
			res.end(JSON.stringify({
				"response": "success"
			}));
		}
	});
});

// Listen
port = 3000;
app.listen(port);
console.log('Listening at http://localhost:' + port);
{'fraction_non_alphanumeric': 0.19671388101983003, 'fraction_numerical': 0.019830028328611898, 'mean_word_length': 3.7605177993527508, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '861512', 'n_tokens_mistral': 3523, 'n_tokens_neox': 3181, 'n_words': 715}