Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
189 lines (143 loc) · 5.5 KB
/
Copy pathapp.js
File metadata and controls
189 lines (143 loc) · 5.5 KB
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
//Défi - Soixante Circuits - Corentin Burguiere le 16 février 2022
require('dotenv').config();
const express = require('express');
const app = express();
const http = require("http");
const https = require("https");
const { Server } = require("socket.io");
const port = process.env.PORT;
const server = http.createServer(app);
const io = new Server(server);
//Waiting time before change city (in milliseconds)
var waitTime = process.env.WAIT_TIME;
var seconds = waitTime / 1000;
app.use('/', express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
server.listen(port, () => {
console.log(`Listening at http://localhost:${port}`)
})
//API's options
const options = {
"method": "GET",
"hostname": "community-open-weather-map.p.rapidapi.com",
"port": null,
"path": "/weather?",
"headers": {
"x-rapidapi-host": "community-open-weather-map.p.rapidapi.com",
"x-rapidapi-key": process.env.API_KEY,
"useQueryString": true
}
};
//Choose a random city of the list
function villeAleatoire() {
//Init the link, then choose the city randomly
options.path = "/weather?";
var cityList = ["Paris", "London", "Tokyo", "New+York+City", "Moscow", "Nantes", "Florence", "Belgrade", "Madrid", "Berlin", "Cologne", "Amsterdam", "Los+Angeles", "Inuvik", "Barrow", "Mould+Bay", "Anadyr", "Seoul", "Sendai", "Singapore"];
var size = cityList.length - 1;
var random = randomIntFromInterval(0, size);
//Create URL with the city
var URLcity = cityList[random];
options.path = options.path + "q=" + URLcity + "&lang=fr";
console.log(options.path);
}
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
//Do once to not get a empty page at the start
villeAleatoire();
io.on('connection', (socket) => {
//Send the seconds to the client
seconds = waitTime / 1000;
socket.emit('chrono', seconds);
var req = https.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
var parsedData = JSON.parse(body);
//Uncommented to see all data on the console
console.log(parsedData);
//Collect data on the parsedData
city = parsedData.name;
meteo = parsedData.weather[0].main;
desc = parsedData.weather[0].description;
temp = parsedData.main.temp;
//Converter on C°
let cel = temp - 273.15;
cel=(Math.round(cel * 100) / 100).toFixed(2);
console.log("Ville : " + city);
console.log("Météo actuelle : " + meteo);
console.log("Description : " + desc);
console.log("Température : " + cel);
console.log("-----------------------------");
//Send information to client
data = [city, meteo, desc, cel];
socket.emit('afficherMeteo', data);
});
});
req.end();
//Search a new city every 30 secondes
var interval = setInterval(function () {
villeAleatoire();
//Get information from the API
var req = https.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
var parsedData = JSON.parse(body);
//Uncommented to see all data on the console
console.log(parsedData);
//Collect data on the parsedData
city = parsedData.name;
meteo = parsedData.weather[0].main;
desc = parsedData.weather[0].description;
temp = parsedData.main.temp;
//Converter on C°
let cel = temp - 273.15;
cel = (Math.round(cel * 100) / 100).toFixed(2);
console.log("Ville : " + city);
console.log("Météo actuelle : " + meteo);
console.log("Description : " + desc);
console.log("Température : " + cel);
console.log("-----------------------------");
//Send information to client
data = [city, meteo, desc, cel];
seconds = waitTime / 1000;
socket.emit('afficherMeteo', data);
});
});
io.on('close', function close() {
clearInterval(interval);
});
req.end();
}, waitTime);
//Chronometre
var interval = setInterval(function () {
//Decrement time
seconds--;
if (seconds < 0) {
seconds = waitTime / 1000;
//Send the seconds to the client
socket.emit('afficherMeteo', data);
}
else {
console.log("Une seconde est passée..." + seconds);
//Send the seconds to the client
socket.emit('chrono', seconds);
}
io.on('close', function close() {
clearInterval(interval);
});
socket.on('disconnect', () => {
console.log('user disconnected');
});
}, 1000);
});
module.exports = app;