-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
163 lines (136 loc) · 4.89 KB
/
script.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
$(function(){
showCalender();
showTodayWeather();
showTimelineWeather();
setInterval(showCalender, 1000);
setInterval(showTodayWeather, 1000 * 60 * 60);
setInterval(showTimelineWeather, 1000 * 60 * 60);
/* for debug */
$('#btnReload').click(function(){
location.reload();
})
});
// GET YOUR API KEY & CITY ID
// https://openweathermap.org/
const APIkey = 'YOUR API KEY';
const city = 'CITY ID';
const week = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT']
function showupdateTime() {
let updateDate = new Date().toLocaleString();
$('#btnReload').text(updateDate);
}
function showCalender(){
let nowDate = new Date();
$('#calMon').text(nowDate.getMonth() + 1);
$('#calDate').text(nowDate.getDate());
$('#calDay').text(week[nowDate.getDay()]);
let HH = nowDate.getHours();
if(HH < 10) {
HH = '0' + HH;
}
$('#clockHH').text(HH);
let MM = nowDate.getMinutes();
if(MM < 10) {
MM = '0' + MM;
}
$('#clockMM').text(MM);
}
function showTodayWeather(){
const url = 'http://api.openweathermap.org/data/2.5/weather?id=' + city + '&units=metric&APPID=' + APIkey;
$.ajax({
url: url,
dataType: "json",
type: 'GET'
})
.done(function(res){
/* icon */
let iconClassName = 'wi-owm-' + res.weather[0].id;
$('#todayWeatherIcon').removeClass().addClass('wi ' + iconClassName);
/* weather label */
let weatherName = res.weather[0].main;
$('#todayWeatherText').text(weatherName);
/* meta */
$('#todayWeatherTemp').text(Math.round(res.main.temp));
$('#todayWeatherClouds').text(res.clouds.all);
showupdateTime();
})
.fail(function(res){
$('#todayWeatherIcon').removeClass().addClass('wi wi-na');
$('#todayWeatherText').text('Error:' + res.responseJSON.cod);
})
}
function showTimelineWeather(){
const url = 'http://api.openweathermap.org/data/2.5/forecast?id=' + city + '&units=metric&APPID=' + APIkey;
$.ajax({
url: url,
dataType: "json",
type: 'GET'
})
.done(function(res){
const data_list = res.list;
let timeline_list = []; // array for timeline display data
const today = new Date();
let timeline_6 = {};
let timeline_6_dateObj = new Date();
timeline_6_dateObj.setHours(6, 0, 0);
timeline_6_dateObj = Math.floor(timeline_6_dateObj.getTime() / 1000);
timeline_6.time = timeline_6_dateObj;
timeline_list.timeline_6 = timeline_6;
let timeline_12 = {};
let timeline_12_dateObj = new Date();
timeline_12_dateObj.setHours(12, 0, 0);
timeline_12_dateObj = Math.floor(timeline_12_dateObj.getTime() / 1000);
timeline_12.time = timeline_12_dateObj;
timeline_list.timeline_12 = timeline_12;
let timeline_18 = {};
let timeline_18_dateObj = new Date();
timeline_18_dateObj.setHours(18, 0, 0);
timeline_18_dateObj = Math.floor(timeline_18_dateObj.getTime() / 1000);
timeline_18.time = timeline_18_dateObj;
timeline_list.timeline_18 = timeline_18;
let timeline_24 = {};
let timeline_24_dateObj = new Date();
timeline_24_dateObj.setDate(today.getDate() + 1);
timeline_24_dateObj.setHours(0, 0, 0);
timeline_24_dateObj = Math.floor(timeline_24_dateObj.getTime() / 1000);
timeline_24.time = timeline_24_dateObj;
timeline_list.timeline_24 = timeline_24;
let timeline_tomorrow = {};
let timeline_tomorrow_dateObj = new Date();
timeline_tomorrow_dateObj.setDate(today.getDate() + 1);
timeline_tomorrow_dateObj.setHours(6, 0, 0);
timeline_tomorrow_dateObj = Math.floor(timeline_tomorrow_dateObj.getTime() / 1000);
timeline_tomorrow.time = timeline_tomorrow_dateObj;
timeline_list.timeline_tomorrow = timeline_tomorrow;
for (line in timeline_list) {
var timeline_item = timeline_list[line];
for (data in data_list) {
if(data_list[data].dt === timeline_item.time) {
timeline_item.clouds = data_list[data].clouds.all;
timeline_item.main = data_list[data].main;
timeline_item.weather = data_list[data].weather[0];
localStorage.setItem(line, JSON.stringify(timeline_item));
}
}
}
for (item in timeline_list) {
let iconID = item + '_icon';
let cloudID = item + `_cloud`;
if('clouds' in timeline_list[item]) {
$('#' + iconID).removeClass().addClass('wi wi-owm-' + timeline_list[item].weather.id);
$('#' + cloudID).text(timeline_list[item].clouds);
} else if(localStorage.getItem(item)) {
var localData = JSON.parse(localStorage.getItem(item));
$('#' + iconID).removeClass().addClass('wi wi-owm-' + localData.weather.id);
$('#' + cloudID).text(localData.clouds);
} else {
$('#' + iconID).removeClass().addClass('wi wi-na');
$('#' + cloudID).text('--');
}
}
})
.fail(function(res){
$('.timeline__icon i').removeClass().addClass('wi wi-na');
$('.timeline__cloudy span').text('--');
})
}