-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
176 lines (163 loc) · 4.61 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
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
require("dotenv").config();
const express = require("express");
const app = express();
module.exports = app;
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const async = require("async");
const port = process.env.PORT || 3002;
const results = [];
const csv = require("csv-parser");
const fs = require("fs");
const Multer = require("multer");
const uploads = Multer({ dest: "uploads/" });
const NewsModel = require("./model/News").newsModel;
const passport = require("passport");
app.use(passport.initialize());
app.use(passport.session());
require("./config/passport")(passport);
mongoose.connect(process.env.db_url, { useNewUrlParser: true }, function(err) {
if (err) console.log("Error While connecting to DB:", err);
else console.log("DB Connected Successfully");
});
app.use(cors());
global.mongoose = mongoose;
global.async = async;
app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.send("NodeJS");
});
const user = require("./routes/user");
app.use("/user", user);
const news = require("./routes/news");
app.use("/news", news);
const report = require("./routes/report");
app.use("/report", report);
const rating = require("./routes/ratings");
app.use("/rating", rating);
const type = uploads.single("image");
app.post("/image", type, (req, res) => {
var tmp_path = req.file.path;
var target_path = "uploads/" + req.file.originalname;
NewsModel.findOneAndUpdate(
{ id: "11" },
{ image: target_path },
(err, result) => {
if (err) {
return res.send(err);
}
res.send("Success");
}
);
});
app.get("/postnews", async (req, res, next) => {
try {
fs.createReadStream("./config/satire_politics_v1.csv")
.pipe(csv())
.on("data", data => results.push(data))
.on("end", () => {
for (i = 0; i < results.length; i++) {
{
NewsModel.insertMany(results[i], { ordered: false }, function(
error,
docs
) {});
}
}
});
} catch (err) {
next(err);
}
});
app.get("/delete", (req, res) => {
let pattern = /fake-[a-z]/;
NewsModel.deleteMany({ group: pattern }, (err, result) => {
if (err) {
res.send(err);
}
res.send("success");
});
});
app.get("/datajson", (req, res) => {
let pattern = /fake-[a-z]/;
async.waterfall(
[
function(waterfallCb1) {
NewsModel.find({ group: "satire" }).exec((err, result) => {
if (err) {
return waterfallCb1(err);
}
waterfallCb1(null, result);
});
},
function(Ids, waterfallCb) {
async.eachLimit(
Ids,
100,
function(singleSource, eachCallback) {
async.waterfall(
[
function(innerWaterfallCb) {
NewsModel.find(
{
$or: [{ source: singleSource }, { group: "satire" }]
},
(err, results) => {
if (err) {
return innerWaterfallCb("Error in saving to the DB");
}
innerWaterfallCb(null, results);
}
).select("-_id");
}
],
function(err, resultsid) {
if (err) {
return eachCallback(err);
}
eachCallback(resultsid, null);
}
);
},
function(resultsid, err) {
if (err) {
return waterfallCb(err);
}
waterfallCb(null, resultsid);
}
);
}
],
function(err, Ids) {
if (err) {
return res.json({ message: err });
}
const resultArrayfinal = [];
const map = new Map();
for (const item of Ids) {
if (!map.has(item.source)) {
map.set(item.source, true); // set any value to Map
resultArrayfinal.push({
lang: item.lang,
title: item.title,
publishedDate: item.publishedDate,
url: item.url,
source: item.source,
category: item.category,
content: item.content,
collection: item.group
});
}
}
let xyz = resultArrayfinal.slice(0, 25);
let data = JSON.stringify(xyz);
fs.writeFileSync("satire_politics", data);
res.json({ message: "success" });
}
);
});
app.listen(port, () => {
console.log("server running on port number 3002");
});