-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (124 loc) · 3.92 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
const express = require("express");
const app = express();
const twitter = require("./helper/gettweet.js");
const geocoder = require("./helper/getlocation.js");
const datasets = require("./helper/loaddataset.js");
var bodyParser = require("body-parser");
var nodemailer = require("nodemailer");
const fs = require("fs");
var bayes = require("bayes");
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database("./database/database.db");
var transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "[email protected]",
pass: "*********"
}
});
var mailOptions = {
from: "[email protected]",
to: "[email protected]",
subject: "ALERT!!!!",
text: ""
};
var paths = require("path");
var path = paths.join(__dirname, "public/datasets");
// front-end declarations
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//Ensure unique tweets are published
var newpost = "first";
/************Async requests*********/
//for displaying on map
app.get("/loadlocation", function(req, res) {
geocoder.locator(function(response) {
res.send(response);
});
});
//loading datasets
app.get("/loaddataset", function(req, res) {
datasets.loaddataset();
res.send("Success");
});
app.get("/stoploaddataset", function(req, res) {
process.exit(1);
res.send("Success");
});
//for displaying tweets
app.get("/loaddata", function(req, res) {
twitter.gettweet(function(response) {
if (response.statusCode === 200 && response.created_at == newpost) {
response.duplicate = true;
} else response.duplicate = false;
newpost = response.created_at;
res.send(response);
});
});
//For predictions
app.get("/predict", function(req, res) {
twitter.gettweet(function(response) {
if (response.statusCode === 200 && response.created_at == newpost) {
response.duplicate = true;
} else response.duplicate = false;
newpost = response.created_at;
var outputflename =
"../FINALYEARPROJECT/public/classification/classifier.json";
let rawdata = fs.readFileSync(outputflename);
var classifier = bayes.fromJson(rawdata);
if (!response.text != "undefined") {
var alert = classifier.categorize(response.text);
console.log(alert);
if (alert === "1" && !response.duplicate) {
mailOptions.text = response.text;
db.each("SELECT name,email,location FROM users", function(err, row) {
console.log(row);
mailOptions.text = "Alert " + row.name + " " + response.text;
mailOptions.to = row.email;
if (response.toLowerCase().indexOf(row.location.toLowerCase()) != -1)
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
});
}
response.alert = alert;
} else {
response.alert = "0";
}
res.send(response);
});
});
app.post("/registeraction", function(req, res) {
var query = `INSERT INTO users(name,email,location) VALUES("${
req.body.name
}","${req.body.email}","${req.body.location}")`;
console.log(query);
var stmt = db.prepare(query);
stmt.run();
stmt.finalize();
console.log(`User added successfully..........`);
res.render("register", { user_created: true });
});
//Website Redirection
app.get("/", (req, res) => res.render("index"));
app.get("/:route", function(req, res) {
if (req.params.route !== "favicon.ico") {
if (req.params.route === "analyzedata") {
fs.readdir(path, function(err, items) {
res.render(req.params.route, { datasets: items });
});
} else {
res.render(req.params.route, { user_created: false });
}
}
});
//Initialization
app.listen(3000, () =>
console.log("Application is running at url:localhost:3000")
);