Toggle navigation
Toggle navigation
This project
Loading...
Sign in
신원형
/
study-or-enjoy
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
1
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
신원형
2022-05-27 03:29:54 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
2f56da049bddfda67775a5e3e14a2e1c5ac4142f
2f56da04
1 parent
547a0afa
implemented weather forecast (2)
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
115 additions
and
0 deletions
.eslintrc.json
test/weather_test.js
weather.js
.eslintrc.json
0 → 100644
View file @
2f56da0
{
"env"
:
{
"browser"
:
true
,
"es2021"
:
true
},
"extends"
:
"eslint:recommended"
,
"parserOptions"
:
{
"ecmaVersion"
:
"latest"
,
"sourceType"
:
"module"
},
"rules"
:
{
}
}
test/weather_test.js
0 → 100644
View file @
2f56da0
import
*
as
mocha
from
'mocha'
import
*
as
weater
from
'../weather.js'
mocha
.
describe
(
'weather'
,
()
=>
{
mocha
.
it
(
'get tomorrow weather'
,
async
()
=>
{
weater
.
get_weather_forecast
(
new
Date
())
.
then
(
it
=>
console
.
log
(
it
))
.
catch
(
it
=>
console
.
log
(
it
))
})
mocha
.
it
(
'get 7 days later weather'
,
async
()
=>
{
const
date
=
new
Date
()
date
.
setDate
(
new
Date
().
getDate
()
+
7
)
weater
.
get_weather_forecast
(
date
)
.
then
(
it
=>
console
.
log
(
it
))
.
catch
(
it
=>
console
.
log
(
it
))
})
});
\ No newline at end of file
weather.js
0 → 100644
View file @
2f56da0
//@ts-check
/* eslint-disable no-unused-vars */
import
*
as
axios
from
'axios'
;
// 최대 7 일간 예보를 반환합니다. 경희대 국제캠퍼스 정문 앞 삼거리 기준으로 호출됩니다.
// 5/27일에 호출했을 경우 6/3일까지의 정오 (한국표준시 기준) 결과를 반환할 수 있습니다.
// 정오 기준, 가장 가까운 날짜의 예보를 반환합니다.
// 예) 호출한 날짜로부터 한 달 뒤 호출 => 호출한 날짜로부터 일주일 뒤 날짜 반환 (최대가 일주일이므로)
// 예) 6/2일 오후 4시 호출 => 6/2일 정오 날씨 반환 (정오 기준이므로)
// 온도의 경우 단위는 섭씨입니다.
/*example
"dt": 1653620400,
"sunrise": 1653596132,
"sunset": 1653648142,
"moonrise": 1653589560,
"moonset": 1653637140,
"moon_phase": 0.9,
"temp": {
"day": 21.75,
"min": 15.7,
"max": 22.85,
"night": 16.88,
"eve": 21.29,
"morn": 15.7
},
"feels_like": {
"day": 21.13,
"night": 16.35,
"eve": 20.57,
"morn": 15.41
},
"pressure": 1001,
"humidity": 44,
"dew_point": 8.88,
"wind_speed": 5.88,
"wind_deg": 273,
"wind_gust": 12.32,
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"clouds": 9,
"pop": 0.2, //Probability of precipitation. The values of the parameter vary between 0 and 1, where 0 is equal to 0%, 1 is equal to 100%
"rain": 0.13, //Precipitation volume, mm
"uvi": 7.71 //The maximum value of UV index for the day
*/
export
async
function
get_weather_forecast
(
date
)
{
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_from
(
date
,
it
.
data
)
})
}
function
extract_from
(
date
,
json_response
)
{
const
time_zone
=
date
.
getTimezonOffset
()
*
60
*
1000
const
target_timestamp
=
Math
.
floor
(
date
.
getTime
()
/
1000
)
+
time_zone
const
target_index
=
find_min_index
(
json_response
.
daily
.
map
(
it
=>
Math
.
abs
(
it
.
dt
-
target_timestamp
)))
return
json_response
.
daily
[
target_index
]
}
function
find_min_index
(
array
)
{
let
lowest_index
=
0
for
(
var
i
=
0
;
i
<
array
.
length
;
i
++
)
{
if
(
array
[
lowest_index
]
>
array
[
i
])
{
lowest_index
=
i
}
}
return
lowest_index
}
\ No newline at end of file
Please
register
or
login
to post a comment