-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
143 lines (133 loc) · 3.38 KB
/
bot.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
//Requiring Modules
var fs = require('fs')
const fetch = require('node-fetch')
const express = require('express')
const app = express()
const getIt = require("./mainFunct")
const ForinstaGram = require("./forInstagram")
//Port
const port = process.env.PORT || 8000;
app.use(function(req,res,next) {
// res.set('Cache-control', `no-store, no-cache, max-age=0`)
res.setHeader("Cache-Control", `public, max-age=3600`);
next()
})
app.use("/ig",ForinstaGram)
//Getting Complete Data WorldWide
app.get("/", (req, res) => {
//Fetch Api
fetch('https://disease.sh/v2/all')
.then(res => res.json())
.catch(err => console.log(err))
.then(json => {
res.setHeader("Content-Type", "image/png");
getIt(json, "false", "")
.catch(err => {
res.sendStatus(404);
res.send(err);
console.log(err)
})
.then(data => {
res.send(data);
console.log("Done")
})
})
})
//Getting Latest Data Worldwide
app.get("/latest", (req, res) => {
fetch('https://disease.sh/v2/all')
.catch(err =>{
res.send("Error Occured")
console.log(err)
})
.then(response =>{
if (response.ok) {
return response.json()
} else {
return res.json("error: Something is wrong.")
}
})
.then(data => {
res.setHeader("Content-Type", "image/png");
getIt(data, "true", "")
.catch(err =>{
res.sendStatus(404);
res.send(err);
console.log(err)
})
.then(data => {
res.send(data);
console.log("Done")
})
})
})
//Getting complete data for a specific country
app.get("/country/:id", (req, res) => {
var country = req.params.id;
fetch(`https://disease.sh/v2/countries/${country}`)
.catch(err => {
res.send("Error Occured")
console.log(err)
})
.then(response => {
if(!response.ok){
res.sendStatus(404)
res.json({
error: "Couldn't find Country"
})
return;
}
return response.json()
})
.then(data => {
res.setHeader("Content-Type", "image/png");
getIt(data, "false", data.country)
.catch(err => {
res.sendStatus(404);
res.send(err);
console.log(err)
})
.then(data => {
res.send(data);
console.log("Done")
})
})
})
//Getting Latest Stats for a specific country
app.get("/country/:id/latest",(req,res)=> getCountryLatest(req,res))
//Specified 2 routes for my personal use :)
app.get("/latest/:id", (req,res)=> getCountryLatest(req,res))
//Function for above routes
var getCountryLatest = (req, res) => {
var country = req.params.id;
fetch(`https://disease.sh/v2/countries/${country}`)
.catch(err => {
res.send("Error Occured")
console.log(err)
})
.then(response => {
if(!response.ok){
res.sendStatus(404)
res.json({
error: "Couldn't find Country"
})
return;
}
return response.json()
})
.then(data => {
res.setHeader("Content-Type", "image/png");
getIt(data, "true", data.country)
.catch(err => {
res.sendStatus(404);
res.send(err);
console.log(err)
})
.then(data => {
res.send(data);
console.log("Done")
})
})
}
//Listening for request...;)
app.listen(port)