-
Notifications
You must be signed in to change notification settings - Fork 0
/
weathersug.html
105 lines (87 loc) · 3.57 KB
/
weathersug.html
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>레시피 추천</h1>
<div id="result"></div>
<div id="nxny"></div>
<div id="pay"></div>
<button id="weatherButton" onclick="getWeatherInfo()">날씨 정보 가져오기</button>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script>
function getWeatherInfo() {
var weatherButton = document.getElementById("weatherButton");
weatherButton.style.display = "none";
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var nx = parseInt(latitude, 10);
var ny = parseInt(longitude, 10);
const villageWeatherUrl = "http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst?";
const serviceKey = "fKIAUimoXsQG2qWxxgMYaAyeifCXgeEqihWVX2AedUJgd4h%2Bu%2BscaKq3DUSik6vE%2FwbRzFou8nQ4sb7GPX4OBg%3D%3D";
const baseDate = moment().format("YYYYMMDD");
const baseTime = "1700";
const payload = `serviceKey=${serviceKey}&dataType=json&base_date=${baseDate}&base_time=${baseTime}&nx=${nx}&ny=${ny}`;
axios.get(villageWeatherUrl + payload)
.then(response => {
const items = response.data.response.body.items.item;
let weatherData = {};
for (let i = 0; i < items.length; i++) {
let item = items[i];
if (item['category'] === 'TMP') {
let weatherTmp = item['fcstValue'];
let weatherWhat;
if (weatherTmp >= 25) {
weatherWhat = '더운';
} else if (weatherTmp <= 5) {
weatherWhat = '추운';
} else {
weatherWhat = '따듯한';
}
weatherData['What'] = weatherWhat;
}
if (item['category'] === 'PTY') {
let weatherCode = item['fcstValue'];
let weatherState;
switch (weatherCode) {
case '1':
weatherState = '비';
break;
case '2':
weatherState = '비 눈';
break;
case '3':
weatherState = '눈';
break;
case '4':
weatherState = '소나기';
break;
default:
weatherState = '맑음';
break;
}
weatherData['State'] = weatherState;
}
}
var dataset = weatherData['What'] + ' ' + weatherData['State'];
console.log(dataset);
performSearch(dataset);
})
.catch(error => {
console.log("날씨 정보 요청 실패: ", error.response?.data);
});
});
} else {
console.log("Geolocation을 지원하지 않는 브라우저입니다.");
}
}
function performSearch(dataset) {
var searchUrl = "https://www.10000recipe.com/recipe/list.html?q=" + encodeURIComponent(dataset);
window.location.href = searchUrl;
}
</script>
</body>
</html>