-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
384 lines (282 loc) · 9.05 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
const express = require('express')
const mongoose = require('mongoose')
const cors = require("cors");
const cheerio = require("cheerio")
const axios = require("axios")
const passport = require("passport");
const path = require("path")
const dbModel = require("./models/db")
const API = require('./routes/apikey');
const app = express();
app.use(express.json());
app.use(cors());
app.use(passport.initialize());
app.use(passport.session());
require("./routes/passportConfig")(passport);
const uri = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@wavedb.uej6u.mongodb.net/${process.env.MONGO_DB}?retryWrites=true&w=majority`;
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
//----------------------------------------- END OF MIDDLEWARE---------------------------------------------------
// Routes
app.post("/login", (req, res, next) => {
passport.authenticate("local", (err, user, info) => {
if (err) throw err;
if (!user) res.send("No User Exists");
else {
req.logIn(user, (err) => {
if (err) throw err;
res.send("Successfully Authenticated");
});
}
})(req, res, next);
});
//----------------------------------------- END OF ROUTES---------------------------------------------------
app.get('/api', (req,res) => {
res.json({
// Health Check Route
message: 'Welcome to JetZ privet API'
})
})
app.get('/api/bmprice',API.validateKey, async (req,res) => {
// Fetch DZD Black Market Prices From MongoDB
dbModel.find({}, (err, result) => {
if(err){
res.send(err)
}
res.send(result);
})
})
app.put('/api/bmupdate',API.validateKey, async (req,res) => {
// Update DZD Black Market Prices
const newBuyPrice = req.body.newBuyPrice;
const newSellPrice = req.body.newSellPrice;
const id = req.body._id;
try {
await dbModel.findById(id, (err, updateSelected) => {
updateSelected.buyPrice = newBuyPrice;
updateSelected.sellPrice = newSellPrice;
updateSelected.save();
console.log('value updated')
})
}catch(err){
console.error(err)
}
})
async function getPrice() {
// Fetch Crypto Market Prices with Cheerio and handle data
try {
const furl = 'https://coinmarketcap.com/'
const { data } = await axios({
method : "GET",
url : furl,
})
const $ = cheerio.load(data)
const elemSelector ='.h7vnx2-2 > tbody:nth-child(3) > tr'
const keys = [
'rank',
'name',
'price',
'day',
'week',
'marketCap',
'volume',
'circulatingSupply'
]
const imgs = [
'src'
]
const coinArr = []
const srcArr = []
$(elemSelector).each((parentIdx,parentElm) =>{
let keyIdx = 0
let srcIdx = 0
const coinObj = {}
const srcObj = {}
if (parentIdx <10) {
$(parentElm).children().each((childIdx, childElm) => {
let tdValue = $(childElm).text()
let tsValue = $(childElm).attr('src')
if (keyIdx ===1 || keyIdx ===6) {
tdValue = $('p:first-child', $(childElm).html()).text()
tsValue = $('img', $(childElm).html()).attr('src')
if(tsValue) {
srcObj[imgs[srcIdx]] = tsValue
srcIdx++
}
}
if (keyIdx ===5) {
tdValue = $('span:last-child', $(childElm).html()).text()
}
if(tdValue) {
coinObj[keys[keyIdx]] = tdValue
keyIdx++
}
})
coinArr.push(coinObj)
srcArr.push(srcObj)
}
})
return {coinArr,srcArr}
}catch(err){
console.error(err)
}
}
app.get('/api/crypto',API.validateKey, async (req, res) => {
// Fetch Crypto Market Prices
try {
const crypto = await getPrice()
return res.status(200).json({
result: crypto,
})
}catch(err){
return res.status(500).json({
err : err.toString(),
})
}
})
async function getDirection() {
// Fetch Crypto Market % ups and downs with Cheerio and handle data
try {
const furl = 'https://coinmarketcap.com/'
const { data } = await axios({
method : "GET",
url : furl,
})
const $ = cheerio.load(data)
selemSelector = '.sc-15yy2pl-0'
const sClassArr = []
$(selemSelector).each((parentIdx,parentElm) =>{
let keyIdx = 0
const sClassObj = {}
if (parentIdx <20) {
$(parentElm).children().each((childIdx, childElm) => {
let tsValue = $(childElm).attr('class')
if(tsValue) {
sClassObj['class'] = tsValue
}
})
sClassArr.push(sClassObj)
}
})
return sClassArr
}catch(err){
console.error(err)
}
}
app.get('/api/cryptoups',API.validateKey, async (req, res) => {
// Fetch Crypto Market % ups and downs
try {
const direc = await getDirection()
return res.status(200).json({
data: direc,
})
}catch(err){
return res.status(500).json({
err : err.toString(),
})
}
})
async function getRate() {
// Fetch USD Market Prices with cheerio
try {
const xurl = 'https://www.x-rates.com/table/?from=USD&amount=1'
const { data } = await axios({
method : "GET",
url : xurl,
})
const $ = cheerio.load(data)
const elemSelector ='table.ratesTable:nth-child(4) > tbody:nth-child(2) > tr'
const keys = [
'name',
'sell',
'buy'
]
const priceArr = []
$(elemSelector).each((parentIdx,parentElm) =>{
let keyIdx = 0
const priceObj = {}
$(parentElm).children().each((childIdx, childElm) => {
let tdValue = $(childElm).text()
if(tdValue) {
priceObj[keys[keyIdx]] = tdValue
keyIdx++
}
})
priceArr.push(priceObj)
})
return priceArr
}catch(err){
console.error(err)
}
}
app.get('/api/rates',API.validateKey, async (req, res) => {
// Fetch USD Market Prices
try {
const rates = await getRate()
return res.status(200).json({
result: rates,
})
}catch(err){
return res.status(500).json({
err : err.toString(),
})
}
})
async function getsRate() {
// Fetch DZD Market Prices with cheerio
try {
const xurl = 'https://fx-rate.net/DZD/'
const { data } = await axios({
method : "GET",
url : xurl,
})
const $ = cheerio.load(data)
const selemSelector = 'table.currencyratetable2:nth-child(1) > tbody:nth-child(2) > tr'
const keys = [
'name',
'sell',
'buy'
]
const spriceArr = []
$(selemSelector).each((parentIdx,parentElm) =>{
let keyIdx = 0
const spriceObj = {}
$(parentElm).children().each((childIdx, childElm) => {
let tsValue = $(childElm).text()
if(tsValue) {
spriceObj[keys[keyIdx]] = tsValue
keyIdx++
}
})
spriceArr.push(spriceObj)
})
return spriceArr
}catch(err){
console.error(err)
}
}
app.get('/api/srates',API.validateKey, async (req, res) => {
// Fetch DZD Market Prices
try {
const rates = await getsRate()
return res.status(200).json({
result: rates,
})
}catch(err){
return res.status(500).json({
err : err.toString(),
})
}
})
if(process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client','build','index.html'));
})
}
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Starting server at ${port}`);
});