-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (69 loc) · 3.16 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
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const https = require("https");
const path = require("path");
const dotenv = require("dotenv");
const axios = require('axios');
dotenv.config();
const port = 8000;
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, './public')));
app.get('/', (req, res) => {
res.render("home");
})
app.get('/main', (req, res) => {
res.render("home");
})
app.post('/main', (req, res) => {
const cityName = req.body.location;
const apiKey = process.env.WEATHER_API_KEY;
const unit = "metric";
const url = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${apiKey}&units=${unit}`;
https.get(url, (response) => {
response.on("data", async (data) => {
const weatherData = JSON.parse(data);
if (weatherData.cod === "404") {
res.render("error");
}
else {
const city = weatherData.name;
const lon = weatherData.coord.lon;
const lat = weatherData.coord.lat;
const description = weatherData.weather[0].main;
const icon = weatherData.weather[0].icon;
const temp = weatherData.main.temp;
const feelsLike = weatherData.main.feels_like;
const windSpeed = weatherData.wind.speed;
const humidity = weatherData.main.humidity;
const imgURL = `http://openweathermap.org/img/wn/${icon}@2x.png`;
//! for chatGPT
const options = {
method: 'GET',
url: 'https://chatgpt-api9.p.rapidapi.com/ask',
params: {
question: `Forecast the weather of the location: ${city} where Latitude: ${lat} & Longitude: ${lon}, and write the response WEATHER FORECAST MANNER.`
},
headers: {
'X-RapidAPI-Key': process.env.CHAT_GPT_API_KEY,
'X-RapidAPI-Host': 'chatgpt-api9.p.rapidapi.com'
}
};
try {
const response = await axios.request(options);
const forecast = response.data.answer;
res.render("main", { cityName: city, lat: lat, lon: lon, description: description, temp: temp, humidity: humidity, feelsLike: feelsLike, windSpeed: windSpeed, img: imgURL, forecast: forecast });
} catch (error) {
const forecast = `Apologies, Due to some internal error we are unable to forecast the weather.`;
res.render("main", { cityName: city, lat: lat, lon: lon, description: description, temp: temp, humidity: humidity, feelsLike: feelsLike, windSpeed: windSpeed, img: imgURL, forecast: forecast });
console.error(error);
}
}
})
})
});
const server = app.listen(process.env.PORT || 4000);
const portNumber = server.address().port;
console.log(`port is open on ${portNumber}`);