-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
112 lines (96 loc) · 2.95 KB
/
app.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
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const flash = require('express-flash');
const MongoStore = require('connect-mongo')(session);
const queryProcess = require('./utils/query');
const app = express();
const passport = require('./passport');
const db = require('./db');
const models = require('./models');
const CONFIG = require('./config2');
const updateTime = 45000
const setIntervalTime = 20000
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended : true }));
app.set('view engine', 'ejs');
app.use(flash());
app.use(cookieParser(CONFIG.COOKIE_SECRET_KEY));
app.use(session({
secret: CONFIG.SESSION_SECRET,
resave: false,
saveUninitialized: true,
store: new MongoStore({ mongooseConnection: db.connection })
}));
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next)=>{
res.locals.user = req.user;
next();
});
app.use('/', require('./routes'));
setInterval(() => {
models.Query.find({ lastUpdated: { $lte: Date.now() - updateTime } })
.then((queries) => {
console.log(Date.now())
console.log(queries.length);
queries.forEach(query => {
console.log(query.email);
console.log('in set interval')
text = ''
query.keywords.forEach(single => {
text = text + ' ' + single
})
console.log(text)
queryProcess(text, query)
.then(text => {
console.log(text);
})
.catch(err => {
console.log(err);
})
})
})
.catch(err => {
console.log("Error in query finding");
console.log(err);
})
}, Number(setIntervalTime));
const chalJaBhai = () => {
return new Promise((resolve,reject)=>{
const { spawn } = require('child_process');
const pyProg = spawn('python', ['public_static/python/try.py']);
console.log('yaha0')
pyProg.stdout.on('data', (data) => {
data = data.toString();
console.log('yaha')
console.log(data)
resolve(data);
});
pyProg.stderr.on('data', (err) => {
console.log('ye to aya')
console.log(err.toString())
reject(err);
})
});
}
app.get('/python', (req,res)=>{
chalJaBhai()
.then(noErr => {
console.log(noErr)
console.log('chal gya')
res.redirect('/success')
})
.catch(err=>{
console.log(`Err: ${err}`)
res.redirect('/failure')
})
})
app.get('*', (req, res) => {
res.redirect('/');
})
app.listen(CONFIG.SERVER.PORT, ()=>{
console.log(`Server Started at http://localhost:${CONFIG.SERVER.PORT}/`);
})