-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
320 lines (303 loc) · 9.48 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* Created by ParikanshAndAtikant on 10/07/2017.
*/
var express = require('express');
var fs = require('fs');
var app = express();
var todolist = [];
var boards = [];
var users = [];
var current_user = "parikansh";
var sql = require('./db');
var request = require('request');
var cheerio = require('cheerio');
app.get('/getcodechef' , function (req,res) {
console.log("chheerio");
var final = '';
var url = 'https://www.codechef.com/problems/school';
request(url,function (err,ress,html) {
console.log("cheerio");
if(!err)
{
console.log("in cheerio");
var $ = cheerio.load(html);
$('.problemname a').first().filter(function () {
var data = $(this);
final = data.toString();
});
res.send(final);
}
})
});
app.use('/' , express.static('public_static'));
/*
app.get('/getcodechef' , function (req,res) {
var final = '';
casper.start('https://www.codechef.com/problems/school' , function () {
var message = "Welcome to";
//this.capture('./output/test.png');
var title = this.evaluate(function (message) {
var title = document.querySelector(".problemname a").getAttribute("href").toString();
return message + " " + title;
},message);
final = title;
});
casper.then(function () {
this.exit();
});
res.send(final);
});
*/
/* CHANGED
app.get('/giveuser', function (req,res) {
res.send(current_user);
});
*/
app.get('/pointexists' ,function (req,res) {
var query = 'SELECT * from users WHERE username="'+req.query.name+'"';
sql.notification(query,function (data) {
if(data.length !== 0)
res.send('old user');
else {
var myquery = 'INSERT INTO users(username,points,password,thisweek) VALUES("'+req.query.name+'",0,"'+req.query.second+'",0)';
sql.notification(myquery, function (data) {
res.send("new user")
})
}
});
});
app.get('/setuser' , function (req,res) {
current_user = req.query.name;
fs.readFile('users.txt', function (err, data) {
if (err) throw err;
var i = 0,flag = 0;
users = JSON.parse(data);
for(i=0;i<users.length;i++)
{
if(users[i].name === current_user)
{
flag = 1;
}
}
if(flag===0)
{
users.push({
name : current_user,
points : 0
})
}
fs.writeFile('users.txt' , JSON.stringify(users), function (err) {
if(err) throw err;
console.log("saved user in file");
//console.log(current_user);
res.send('');
});
});
});
app.get('/todo' , function (req,res) {
var obj = {
"username" : req.query.user ,
"task1" : req.query.task1,
"task2" : req.query.task2,
"task3" : req.query.task3,
"done1" : 0,
"done2" : 0,
"done3" : 0,
"locked" :0
};
todolist.push(obj);
fs.writeFile('todo.txt' , JSON.stringify(todolist), function (err) {
if(err) throw err;
console.log("saved todo in file");
res.send('');
});
});
app.get('/todoshow' , function (req,res) {
var i = 0,flag=0,x=0;
current_user = req.query.user;
fs.readFile('todo.txt', function (err, data) {
if (err) throw err;
todolist = JSON.parse(data);
/*console.log("updated from text file");
console.log("todolist array sent");
res.send(todolist);*/
for(i=0;i<todolist.length;i++)
{
if(todolist[i].username === current_user)
{
flag = 1;
x = i;
//res.send(todolist[i]);
}
}
if(flag === 0)
res.send('');
else
res.send(todolist[x]);
});
});
app.get('/setlocked',function (req,res) {
fs.readFile('todo.txt', function (err, data) {
if (err) throw err;
todolist = JSON.parse(data);
current_user = req.query.user;
/*console.log("updated from text file");
console.log("todolist array sent");
res.send(todolist);*/
for(i=0;i<todolist.length;i++) {
if (todolist[i].username === current_user) {
todolist[i].locked = 1;
}
}
fs.writeFile('todo.txt' , JSON.stringify(todolist), function (err) {
if(err) throw err;
console.log("saved locked in file");
res.send('');
});
});
});
app.get('/todoclick',function (req,res) {
var i = 0;
fs.readFile('todo.txt', function (err, data) {
if (err) throw err;
todolist = JSON.parse(data);
current_user = req.query.user;
/*console.log("updated from text file");
console.log("todolist array sent");
res.send(todolist);*/
for(i=0;i<todolist.length;i++) {
if (todolist[i].username === current_user) {
var x = req.query.num;
if(x == "done1")
todolist[i].done1 = 1;
if(x == "done2")
todolist[i].done2 = 1;
if(x == "done3")
todolist[i].done3 = 1;
}
}
fs.writeFile('todo.txt' , JSON.stringify(todolist), function (err) {
if(err) throw err;
console.log("saved todo in file");
res.send('');
});
});
});
app.get('/boardshow' , function (req,res) {
var i = 0,flag=0;
fs.readFile('boards.txt' , function (err,data) {
if(err) throw err;
boards = JSON.parse(data);
res.send(boards);
});
});
app.get('/mynewboard' , function (req,res) {
current_user = req.query.user;
var obj = {
"name" : current_user,
"message" : req.query.mes,
"link" : req.query.link,
"ctr" : 1
};
boards.unshift(obj);
fs.writeFile('boards.txt' , JSON.stringify(boards), function (err) {
if(err) throw err;
console.log("saved board in file");
res.send('');
});
});
app.get('/upvoteme',function (req,res) {
var y = 0;
fs.readFile('boards.txt' , function (err,data) {
if(err) throw err;
boards = JSON.parse(data);
console.log(req.query.id);
boards[req.query.id].ctr++;
current_user = req.query.user;
console.log(boards[req.query.id].name + ' ko upvote mila from ' + current_user);
var query = 'UPDATE users SET points = points + 5, thisweek = thisweek + 5 WHERE username = "'+boards[req.query.id].name + '"';
sql.notification(query,function (data) {
console.log('point increased');
});
y = boards[req.query.id].ctr;
fs.writeFile('boards.txt' , JSON.stringify(boards), function (err) {
if(err) throw err;
console.log("saved board after upvote in file");
res.send('a'+y);
});
});
});
app.get('/notification' , function (req,res) {
current_user = req.query.user;
var query = 'INSERT INTO notifications(fromuser,touser,content,link) VALUES ("' +current_user + '","' + req.query.from + '","has claimed points for his edit.","' + req.query.link + '")';
sql.notification(query,function (data) {
res.send(data);
});
});
app.get('/shownotifications', function (req,res) {
var query = 'SELECT * FROM notifications';
sql.notification(query,function (data) {
res.send(data);
});
});
app.get('/sendrating' , function (req,res) {
var query = 'SELECT * FROM notifications WHERE id='+req.query.id;
sql.notification(query,function (data) {
var switchto = data[0].fromuser;
var switchfrom = data[0].touser;
var link = data[0].link;
var content = "gave you "+ req.query.rating + " points for edit.";
var myquery = 'INSERT INTO notifications(fromuser,touser,content,link) VALUES("' + switchfrom + '","' + switchto +'","' + content + '","'+link+'")';
sql.notification(myquery,function (data) {
console.log("hello");
});
myquery = 'UPDATE users SET points = points + (10*'+req.query.rating+'), thisweek = thisweek + (10*'+req.query.rating+')WHERE username = "'+switchto + '"';
sql.notification(myquery,function (data) {
res.send('');
});
});
});
app.get('/getpoints' , function (req,res) {
var query = 'SELECT * FROM users where username="'+req.query.name+'"';
sql.notification(query,function (data) {
res.send(data);
});
});
app.get('/getleaderboard' , function (req,res) {
var query = 'SELECT * FROM users';
sql.notification(query, function (data) {
var mydata = data;
mydata.sort(function (a, b) {
return parseInt(b.points) - parseInt(a.points);
});
res.send(mydata);
});
});
app.get('/weekly' , function (req,res) {
var query = 'UPDATE users SET thisweek = 0';
sql.notification(query,function () {
console.log("weekly chal gaya");
res.send('');
});
});
app.get('/passwordcheck',function (req,res) {
var query = 'SELECT * FROM users where username="'+req.query.name+'"';
sql.notification(query,function (data) {
if(data.length == 0)
{
console.log("yeh naya banda hai");
res.send("naya banda");
}
else {
if(data[0].password == req.query.secondtier) {
res.send("sahi user");
}
else {
res.send("galat user");
}
}
})
});
app.listen(4000 , function () {
console.log("Server is running on 4000");
});