신원형

add weather current function

......@@ -10,7 +10,7 @@ const axios = require('axios')
// 예) 6/2일 오후 4시 호출 => 6/2일 정오 날씨 반환 (정오 기준이므로)
// 온도의 경우 단위는 섭씨입니다.
/*example
/*example - forecast
"dt": 1653620400,
"sunrise": 1653596132,
......@@ -52,6 +52,34 @@ const axios = require('axios')
"uvi": 7.71 //The maximum value of UV index for the day
*/
/*example - current
{
"dt": 1653989440,
"sunrise": 1653941622,
"sunset": 1653993914,
"temp": 23.74,
"feels_like": 22.82,
"pressure": 1008,
"humidity": 25,
"dew_point": 2.56,
"uvi": 0.17,
"clouds": 20,
"visibility": 10000,
"wind_speed": 5.66,
"wind_deg": 300,
"weather": [
{
"id": 801,
"main": "Clouds",
"description": "few clouds",
"icon": "02d"
}
]
}
*/
async function get_weather_forecast(date) {
const lat = 37.24764302276268 //위도
const lon = 127.0783992268606 //경도
......@@ -62,6 +90,16 @@ async function get_weather_forecast(date) {
return await axios.default.get(target).then(it => { return extract_from(date, it.data) })
}
async function get_weather_current() {
const lat = 37.24764302276268 //위도
const lon = 127.0783992268606 //경도
const api_key = "336ddd01d3d6f78782eed90d3921bc7e"
const target = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=minutely,hourly,alerts&appid=${api_key}&units=metric`
return await axios.default.get(target).then(it => { return extract_current(it.data) })
}
function extract_from(date, json_response) {
const target_timestamp = Math.floor(date.getTime() / 1000)
......@@ -70,6 +108,10 @@ function extract_from(date, json_response) {
return json_response.daily[target_index]
}
function extract_current(json_response) {
return json_response.current
}
function find_min_index(array) {
let lowest_index = 0
for (var i = 0; i < array.length; i++) {
......@@ -82,3 +124,4 @@ function find_min_index(array) {
}
module.exports.get_weather_forecast = get_weather_forecast
module.exports.get_weather_current = get_weather_current
\ No newline at end of file
......