Showing
5 changed files
with
111 additions
and
0 deletions
This diff is collapsed. Click to expand it.
... | @@ -13,12 +13,19 @@ | ... | @@ -13,12 +13,19 @@ |
13 | "author": "", | 13 | "author": "", |
14 | "license": "", | 14 | "license": "", |
15 | "dependencies": { | 15 | "dependencies": { |
16 | + "@types/axios": "^0.14.0", | ||
16 | "@types/selenium-webdriver": "^4.1.0", | 17 | "@types/selenium-webdriver": "^4.1.0", |
17 | <<<<<<< HEAD | 18 | <<<<<<< HEAD |
19 | +<<<<<<< HEAD | ||
18 | "csv-parser": "^3.0.0", | 20 | "csv-parser": "^3.0.0", |
19 | "eslint": "^8.15.0", | 21 | "eslint": "^8.15.0", |
20 | ======= | 22 | ======= |
21 | >>>>>>> 6c6e356fa8269fbbdee92f54a6f2f16ffe2ba729 | 23 | >>>>>>> 6c6e356fa8269fbbdee92f54a6f2f16ffe2ba729 |
24 | +======= | ||
25 | +======= | ||
26 | + "axios": "^0.27.2", | ||
27 | +>>>>>>> weather | ||
28 | +>>>>>>> 039e1a43c37157ea46e8f87599a93f75536a140e | ||
22 | "mocha": "^10.0.0", | 29 | "mocha": "^10.0.0", |
23 | "selenium-webdriver": "^4.1.2" | 30 | "selenium-webdriver": "^4.1.2" |
24 | }, | 31 | }, | ... | ... |
test/weather_test.js
0 → 100644
1 | +import * as mocha from 'mocha' | ||
2 | +import * as weater from '../weather.js' | ||
3 | + | ||
4 | +mocha.describe('weather', () => { | ||
5 | + mocha.it('get tomorrow weather', async () => { | ||
6 | + weater.get_weather_forecast(new Date()) | ||
7 | + .then(it => console.log(it)) | ||
8 | + .catch(it => console.log(it)) | ||
9 | + }) | ||
10 | + | ||
11 | + mocha.it('get 7 days later weather', async () => { | ||
12 | + const date = new Date() | ||
13 | + date.setDate(new Date().getDate() + 7) | ||
14 | + | ||
15 | + weater.get_weather_forecast(date) | ||
16 | + .then(it => console.log(it)) | ||
17 | + .catch(it => console.log(it)) | ||
18 | + }) | ||
19 | +}); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
weather.js
0 → 100644
1 | +//@ts-check | ||
2 | +/* eslint-disable no-unused-vars */ | ||
3 | +import * as axios from 'axios'; | ||
4 | + | ||
5 | + | ||
6 | +// 최대 7 일간 예보를 반환합니다. 경희대 국제캠퍼스 정문 앞 삼거리 기준으로 호출됩니다. | ||
7 | +// 5/27일에 호출했을 경우 6/3일까지의 정오 (한국표준시 기준) 결과를 반환할 수 있습니다. | ||
8 | +// 정오 기준, 가장 가까운 날짜의 예보를 반환합니다. | ||
9 | +// 예) 호출한 날짜로부터 한 달 뒤 호출 => 호출한 날짜로부터 일주일 뒤 날짜 반환 (최대가 일주일이므로) | ||
10 | +// 예) 6/2일 오후 4시 호출 => 6/2일 정오 날씨 반환 (정오 기준이므로) | ||
11 | +// 온도의 경우 단위는 섭씨입니다. | ||
12 | + | ||
13 | +/*example | ||
14 | + | ||
15 | + "dt": 1653620400, | ||
16 | + "sunrise": 1653596132, | ||
17 | + "sunset": 1653648142, | ||
18 | + "moonrise": 1653589560, | ||
19 | + "moonset": 1653637140, | ||
20 | + "moon_phase": 0.9, | ||
21 | + "temp": { | ||
22 | + "day": 21.75, | ||
23 | + "min": 15.7, | ||
24 | + "max": 22.85, | ||
25 | + "night": 16.88, | ||
26 | + "eve": 21.29, | ||
27 | + "morn": 15.7 | ||
28 | + }, | ||
29 | + "feels_like": { | ||
30 | + "day": 21.13, | ||
31 | + "night": 16.35, | ||
32 | + "eve": 20.57, | ||
33 | + "morn": 15.41 | ||
34 | + }, | ||
35 | + "pressure": 1001, | ||
36 | + "humidity": 44, | ||
37 | + "dew_point": 8.88, | ||
38 | + "wind_speed": 5.88, | ||
39 | + "wind_deg": 273, | ||
40 | + "wind_gust": 12.32, | ||
41 | + "weather": [ | ||
42 | + { | ||
43 | + "id": 500, | ||
44 | + "main": "Rain", | ||
45 | + "description": "light rain", | ||
46 | + "icon": "10d" | ||
47 | + } | ||
48 | + ], | ||
49 | + "clouds": 9, | ||
50 | + "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% | ||
51 | + "rain": 0.13, //Precipitation volume, mm | ||
52 | + "uvi": 7.71 //The maximum value of UV index for the day | ||
53 | + | ||
54 | +*/ | ||
55 | +export async function get_weather_forecast(date) { | ||
56 | + const lat = 37.24764302276268 //위도 | ||
57 | + const lon = 127.0783992268606 //경도 | ||
58 | + const api_key = "336ddd01d3d6f78782eed90d3921bc7e" | ||
59 | + | ||
60 | + const target = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=minutely,hourly,alerts&appid=${api_key}&units=metric` | ||
61 | + | ||
62 | + return await axios.default.get(target).then(it => { return extract_from(date, it.data) }) | ||
63 | +} | ||
64 | + | ||
65 | +function extract_from(date, json_response) { | ||
66 | + const target_timestamp = Math.floor(date.getTime() / 1000) | ||
67 | + | ||
68 | + const target_index = find_min_index(json_response.daily.map(it => Math.abs(it.dt - target_timestamp))) | ||
69 | + | ||
70 | + return json_response.daily[target_index] | ||
71 | +} | ||
72 | + | ||
73 | +function find_min_index(array) { | ||
74 | + let lowest_index = 0 | ||
75 | + for (var i = 0; i < array.length; i++) { | ||
76 | + if (array[lowest_index] > array[i]) { | ||
77 | + lowest_index = i | ||
78 | + } | ||
79 | + } | ||
80 | + | ||
81 | + return lowest_index | ||
82 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment