-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
91 lines (76 loc) · 2.92 KB
/
app.js
File metadata and controls
91 lines (76 loc) · 2.92 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
const express = require("express");
const parser = require("body-parser");
const axios = require("axios");
const ejs = require("ejs");
require("dotenv").config();
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
organization: "org-1y8mjnjVnwP86A6Yw2L9mqnc",
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const weatherAPIKey = process.env.WEATHER_API_KEY;
const app = express();
app.use(express.static("public"));
app.use(parser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.get("/", (req, res) => {
res.render("index", {
infoLine:
"Welcome to our travel planning page! Please enter the name of the city you want to explore, and we will create a customized travel plan for you. Please note that it may take a few moments for us to generate your personalized plan. Get ready to embark on a journey of discovery and adventure!",
parArray: [],
});
});
app.post("/", async (req, res) => {
try {
const cityName = req.body.cityName.toString();
const weatherURL =
"https://api.openweathermap.org/data/2.5/weather?q=" +
cityName +
"&appid=" +
weatherAPIKey +
"&units=metric";
const weatherData = await axios.get(weatherURL);
const temperature = weatherData.data.main.temp;
const weatherDescription = weatherData.data.weather[0].description;
const icon = weatherData.data.weather[0].icon;
const timezone = weatherData.data.timezone;
const now = new Date(Date.now() + timezone * 1000);
now.setUTCMinutes(now.getUTCMinutes() + 30); // add 30 minutes (1800 seconds)
now.setUTCMinutes(0, 0, 0); // round down to the nearest hour
const utcTime =
Number(now.toISOString().substr(11, 2)) > 8
? now.toISOString().substr(11, 2)
: "8";
const prompt =
"Plan my unique holiday in " +
cityName +
" with detail spots by time. Starting time is " +
utcTime +
+":00. Today's weather is " +
temperature +
" Celsius, and weather description is " +
weatherDescription +
". Also keep in mind the weather while planning. Example: 8:00 - Visit the zoo. It is a wonderful place. Full of shades from trees. 19:00 - Enjoy a boat ride on the Yamuna River";
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }],
temperature: 0.6,
});
const plan = completion.data.choices[0].message.content;
const myArray = plan.split("\n\n");
res.render("index", {
infoLine:
"'The world is a book, and those who do not travel read only a page.' - Saint Augustine",
parArray: myArray,
});
} catch (err) {
res.render("index", {
infoLine: err.response.data.message,
parArray: [],
});
}
});
app.listen(process.env.PORT || 3000, () => {
console.log("Server started on port 3000");
});