-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
134 lines (109 loc) · 3.55 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
const express = require("express");
const bodyParser = require("body-parser");
const { spawn } = require("child_process");
const fetch = require("node-fetch");
const cheerio = require("cheerio");
const cors = require("cors");
const mongoose = require("mongoose");
const app = express();
app.use(cors());
const port = 3002;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
mongoose.connect("mongodb://localhost:27017/mydatabase", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
db.once("open", () => {
console.log("Connected to MongoDB database");
});
const DataSchema = new mongoose.Schema({
title: String,
target_industry: [String],
problem_statement: [String],
output: [String],
});
const DataModel = mongoose.model("DataModel", DataSchema);
app.post("/post", async (req, res) => {
try {
const { title, target_industry, problem_statement } = req.body;
const inputData = new DataModel({
title,
target_industry: target_industry.split(",").map((term) => term.trim()),
problem_statement: problem_statement
.split(",")
.map((term) => term.trim()),
});
await inputData.save();
const targetIndustriesArray = target_industry
.split(",")
.flatMap((term) => term.split(/\s*,\s*/))
.map((term) => term.trim());
const array1 = problem_statement.split(",").map((term) => term.trim());
const industryData = await fetchIndustryData(targetIndustriesArray);
const pythonProcess = spawn("python", [
"script3.py",
title,
target_industry,
problem_statement,
]);
let responseData = [];
pythonProcess.stdout.on("data", async (data) => {
const response = data.toString().trim();
responseData.push(response);
const formattedData = responseData[0]
.split("\n")
.map((item) => item.trim());
const percentage = formattedData[0];
res.send(`Percentage: ${percentage}%`);
console.log("Data from Python script:", formattedData);
await DataModel.findOneAndUpdate(
{ title },
{ output: formattedData },
{ new: true }
);
});
} catch (error) {
console.error("Error processing request:", error);
res.status(500).json({ error: "Internal server error" });
}
});
async function fetchIndustryData(industries) {
try {
const industryData = [];
for (const industry of industries) {
let searchCount = await fetchSearchCount(industry);
while (searchCount === null || isNaN(searchCount)) {
searchCount = await fetchSearchCount(industry);
}
industryData.push(Number(searchCount));
}
return industryData;
} catch (error) {
console.error("Error fetching industry data:", error);
return [];
}
}
async function fetchSearchCount(industry) {
try {
const bingSearchUrl = `https://www.bing.com/search?q=${encodeURIComponent(
industry
)}`;
const response = await fetch(bingSearchUrl);
const body = await response.text();
const $ = cheerio.load(body);
const searchCountElement = $(".sb_count").first();
const searchCountText = searchCountElement.text().trim();
const trimmedString = searchCountText.replace(/,/g, "").replace(/\D/g, "");
return trimmedString;
} catch (error) {
console.error("Error fetching search count:", error);
return "Error";
}
}
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});