This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforecast.js
70 lines (66 loc) · 2.12 KB
/
forecast.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
---
# this ensures Jekyll reads the file and can uses liquid
---
var apiKey = "{{ site.module.weather.key }}";
var lat = "{{ site.module.weather.latitude }}";
var long = "{{ site.module.weather.longitude }}"
var units = "uk"
var darksky = "https://api.darksky.net/forecast/" + apiKey + "/" + lat + "," + long + "?units=" + units + "&extend=minutely,hourly";
function getWeather() {
$.ajax({
url: darksky,
// dataType: 'application/json',
dataType: "jsonp",
type: "GET",
error: function(data) {
// TODO error
console.log(data);
},
success: function(data) {
var currently = data.currently;
var week = data.daily;
var todayDate = new Date(currently.time * 1000);
$('#weather .current .icon').each(function() {$(this).hide();});
$('#weather .current .icon.' + getCssIconName(currently.icon)).show();
$('#weather .current .temp').html(Math.round(currently.temperature) + "°C");
// $('#weather .week .summary').html(week.summary);
$('#weather .week .look').html('');
$.each(week.data, function(index, day) {
var date = new Date(day.time * 1000);
if (date.getDate() == todayDate.getDate()) return;
var weekNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var dayWeek = weekNames[date.getDay()];
$('#weather .week .look').append('<div class="day ' + dayWeek + '"></div>');
var current = $('#weather .week .look .day.' + dayWeek);
var icon = $('#weather .current .icon.' + getCssIconName(day.icon)).clone();
icon.show();
current.append('<span class="name">' + dayWeek + '</span>')
current.append(icon);
current.append('<span class="temp max">' + Math.round(day.temperatureMax) + '°C</span>');
current.append('<span class="temp min">' + Math.round(day.temperatureMin) + '°C</span>');
});
}
});
}
function getCssIconName(icon) {
var cssName;
switch (icon) {
case 'rain': {
cssName = 'rainy';
break;
}
case 'snow': {
cssName = 'snowy';
break;
}
case 'fog': {
cssName = 'foggy';
break;
}
default: {
cssName = icon;
break;
}
}
return cssName;
}