권주희

implement get current weather api

...@@ -4,6 +4,7 @@ var axios = require("axios"); ...@@ -4,6 +4,7 @@ var axios = require("axios");
4 4
5 const openAPIKey = require("./secrets.json").openAPIKey; 5 const openAPIKey = require("./secrets.json").openAPIKey;
6 const googleMapKey = require("./secrets.json").googleAPIKey; 6 const googleMapKey = require("./secrets.json").googleAPIKey;
7 +const weatherAPIKey = require("./secrets.json").weatherAPIKey;
7 8
8 axios.create({ 9 axios.create({
9 // TODO : 웹을 AWS에 올릴때, 해당 baseURL이 달라져야할 수 있음 10 // TODO : 웹을 AWS에 올릴때, 해당 baseURL이 달라져야할 수 있음
...@@ -25,6 +26,40 @@ router.get("/", async function (req, res, next) { ...@@ -25,6 +26,40 @@ router.get("/", async function (req, res, next) {
25 res.send(airCondition); 26 res.send(airCondition);
26 }); 27 });
27 28
29 +router.get("/weather", async function (req, res, next) {
30 + console.log("경도:", req.query.latitude);
31 + console.log("경도:", req.query.longitude);
32 +
33 + let airCondition = "";
34 + let response = await getEnglishPosition(
35 + req.query.latitude,
36 + req.query.longitude
37 + )
38 + .then((encodedStation) => getWeather(encodedStation))
39 + .then((result) => {
40 + airCondition = result;
41 + });
42 +
43 + res.send(airCondition);
44 +});
45 +
46 +const getWeather = (encodedStation) => {
47 + return axios
48 + .get(
49 + "https://api.openweathermap.org/data/2.5/weather?q=" +
50 + encodedStation +
51 + "&appid=" +
52 + weatherAPIKey
53 + )
54 + .then(function (response) {
55 + // console.log("RES :: ", response["data"]["weather"]);
56 + return response["data"]["weather"][0];
57 + })
58 + .catch(function (error) {
59 + console.log(error.response);
60 + });
61 +};
62 +
28 const getPosition = (lat, lon) => { 63 const getPosition = (lat, lon) => {
29 return axios 64 return axios
30 .get( 65 .get(
...@@ -58,6 +93,29 @@ const getPosition = (lat, lon) => { ...@@ -58,6 +93,29 @@ const getPosition = (lat, lon) => {
58 console.log(error.response); 93 console.log(error.response);
59 }); 94 });
60 }; 95 };
96 +
97 +const getEnglishPosition = (lat, lon) => {
98 + return axios
99 + .get(
100 + "https://maps.googleapis.com/maps/api/geocode/json?latlng=" +
101 + lat +
102 + "," +
103 + lon +
104 + "&location_type=ROOFTOP&result_type=street_address&key=" +
105 + googleMapKey +
106 + "&language=en"
107 + )
108 + .then(function (response) {
109 + let stationName =
110 + response["data"].results[0]["address_components"][3]["long_name"];
111 + console.log("STATION : ", stationName);
112 + return (encodedStation = encodeURI(stationName));
113 + })
114 + .catch(function (error) {
115 + console.log(error.response);
116 + });
117 +};
118 +
61 /* GET route airCondition listing. */ 119 /* GET route airCondition listing. */
62 router.get("/route", async function (req, res, next) { 120 router.get("/route", async function (req, res, next) {
63 console.log("출발지:", req.query.departure); 121 console.log("출발지:", req.query.departure);
......