-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (115 loc) · 3.63 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
import express from 'express';
import axios from 'axios';
import cors from 'cors';
import logger from './logger.js';
import environment from './environment.js';
import he from 'he';
const app = express();
const { log, error } = logger();
const { APP_ORIGIN,
WEATHER_API_KEY,
WEATHER_API_BASE,
INSULT_API,
COMPLIMENT_API,
PORT } = environment;
app.use(cors({
origin: APP_ORIGIN
}));
app.get('/', (req, res) => {
res.send(`<h1>Moody Weather API</h1>
<p>Get what you deserve plus the weather: <a href="/moody-weather?q=10001">Click here</a>`);
});
app.get('/insult', async (req, res) => {
try {
// Generate insult
const insultResponse = await axios.get(`${INSULT_API}`, {
params: {
type: 'json'
}
});
const insult = he.decode(insultResponse.data.insult);
log('Retrieved insult...');
res.send({
isInsult: true,
text: insult
});
} catch (e) {
error(e.message);
res.sendStatus(500);
}
});
app.get('/compliment', async (req, res) => {
try {
// Generate compliment
const complimentResponse = await axios.get(`${COMPLIMENT_API}`);
let compliment = complimentResponse.data.compliment;
compliment = `${compliment[0].toUpperCase()}${compliment.substring(1)}.`;
log('Retrieved compliment...');
res.send({
isInsult: false,
text: compliment
});
} catch (e) {
error(e.message);
res.sendStatus(500);
}
});
app.get('/moody-weather', async (req, res) => {
try {
let { q, degrading } = req.query;
if (!q) {
error('Query not provided. Try again with \'q\' as a query parameter.');
return;
}
const weatherResponse = await axios.get(`${WEATHER_API_BASE}/current.json`, {
params: {
key: WEATHER_API_KEY,
q
}
});
// Get weather data
const weather = weatherResponse.data;
const weatherCode = weather.current.condition.code;
log('Retrieved weather...');
if (checkBadMood(weatherCode) || degrading == 'true') {
// Generate insult
const insultResponse = await axios.get(`${INSULT_API}`, {
params: {
type: 'json'
}
});
const insult = he.decode(insultResponse.data.insult);
log('Retrieved insult...');
res.send({weather, dialogue: {
text: insult,
isInsult: true
}});
} else {
// Generate compliment
const complimentResponse = await axios.get(`${COMPLIMENT_API}`);
let compliment = complimentResponse.data.compliment;
compliment = `${compliment[0].toUpperCase()}${compliment.substring(1)}.`;
log('Retrieved compliment...');
res.send({weather, dialogue: {
text: compliment,
isInsult: false
}});
}
} catch (e) {
error(e.message, 'Error');
res.sendStatus(500);
}
});
function checkBadMood(code) {
return (code !== 1000 && code !== 1003); // Not sunny or partly cloudy
}
app.listen(PORT, () => {
console.clear();
console.log(
`-------------------------------
\x1b[32mMoody Weather API Server\x1b[0m
-------------------------------`
);
log(`Listening to port \x1b[34m${PORT}\x1b[0m...`, 'Moody Weather API');
log(`Allowing origin: \x1b[34m\x1b[4m${APP_ORIGIN}\x1b[0m`, 'Moody Weather API');
});