-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
154 lines (131 loc) · 4.59 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
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
const exp = require('constants');
const express = require('express')
const path = require('path')
const passport = require('passport')
const session = require('express-session');
const cron = require('node-cron')
const { default: mongoose } = require('mongoose');
const { spawn } = require('child_process');
const csv = require('csv-parser')
const fs = require('fs')
const date = new Date();
const app = express()
const port = 3000;
const db = require('./config/key').MongoURI
const { CompanyStore, CompanyDetails } = require('./models/Users');
mongoose.connect(db, { useNewUrlParser: true })
.then(() => {
console.log("Mongodb connected")
})
.catch(err => console.log(err))
app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: false,
// cookie: { secure: process.env.ENV === 'PRODUCTION' } production mai jab daalenge tab comment out kar denge
}));
app.use(passport.initialize());
app.use(passport.session());
//template engine
app.set('view engine', 'hbs');
app.use("/public", express.static(path.join(__dirname, 'public')));
app.set("/views", (path.join(__dirname, 'views')));
app.use(express.json());
// middleware
app.use(express.urlencoded({ extended: false }))
app.use('/', require('./route/index'))
app.use('/user', require('./route/user'))
//reading the all_ticker.csv using read file module------------------------------
fs.readFile('./public/textfile/all_ticker.txt', "utf8", async (err, data) => {
if (err) {
console.log(err);
}
try {
const lines = data.split("\n").map(line => ({ company: line.trim() }));
CompanyStore.insertMany(lines)
.then(() => {
console.log("data is sucessfully inserted")
})
.catch(err => {
console.log(err);
})
} catch (error) {
console.log(error);
}
})
//storing the data in mongodb data of all_stock_data.csv------------------
const results = [];
fs.createReadStream('all_stock_data.csv')
.pipe(csv())
.on('data', (data) => {
results.push({
Ticker: data.Ticker,
Price: data.Price,
Change: data.Change,
ChangePercentage: data['Change %'],
Volume: data.Volume,
MarketCap: data["Market Cap"],
YearChange: data["52W Change"],
beta: data.Beta
})
})
.on("end", async () => {
console.log("csv parsing is complete and is ready to store to your database")
await alldatastoring();
})
async function alldatastoring() {
try {
await CompanyDetails.insertMany(results)
.then(() => {
console.log("data is sucessfully pushed")
})
.catch(err => {
console.log(err);
})
} catch (error) {
console.log(error);
}
}
//-----------------------------------------------------------------------------------------------
//calling python to make all_stock_data.csv and sorting_data.json
let data1 = '';
function python_script(args) {
const pyone = spawn('python', [args]);
pyone.stdout.on('data', function (data) {
data1 += data.toString();
console.log(data1)
});
pyone.on('close', (code) => {
console.log(`child process close all stdio with code ${code}`);
if (code === 0) {
console.log("csv file of all the data is ready for the day;")
task.stop();
}
});
}
var task;
// function callingfunc() {
// task = cron.schedule('* * * * *', () => { //calling python file using child-process
// console.log("ready to launch the rocket at" + new Date());
// const args = ["./python_files/try.py"]
// python_script(args);
// })
// }
// function sortingdata() {
// task = cron.schedule('* * * * *', () => { //callling python files using child-process
// const args = ["./python_files/sortingdata.py"]
// python_script(args);
// })
// }
// cron.schedule('30 10 * * * ', () => { //calling callingfunc function for calling python files
// console.log('hola amigo')
// callingfunc();
// })
// cron.schedule('0 11 * * *', () => { //calling sortingdata function for calling python files
// console.log("sortingdata is running")
// sortingdata();
// })
//-------------------------------------------------------------------------------------------------
app.listen(port, () => {
console.log("app is listening to the port 3000")
})