Skip to content

fixed weather api #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/javascript/moment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! moment.js

;(function (global, factory) {
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.moment = factory()
Expand Down
161 changes: 110 additions & 51 deletions assets/javascript/weather.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,117 @@
let dayArray = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
//weatherForcast()
function weatherForcast(searchCity) {
var APIKey = '87231f56cfbb4322a1a44bc975de93ac';
//api call for 5 day forecast
var queryURL = `https://api.openweathermap.org/data/2.5/forecast?q=${searchCity}&units=imperial&appid=${APIKey}`;
//API call for current temp
let currentTemp = `https://api.openweathermap.org/data/2.5/weather?q=${searchCity}&units=imperial&appid=${APIKey}`;

var APIKey = "87231f56cfbb4322a1a44bc975de93ac";
//api call for 5 day forecast
var queryURL = `https://api.openweathermap.org/data/2.5/forecast?q=${searchCity}&units=imperial&appid=` + APIKey;
//API call for current temp
let currentTemp = `https://api.openweathermap.org/data/2.5/weather?q=${searchCity}&units=imperial&appid=` + APIKey;
//${searchCity}
//for current temp
$.ajax({
url: currentTemp,
method: "GET"
})
.then(function (searchResponse) {
$('#current-temp').html(searchResponse.name + "'s" + ' Current Temp: ' + searchResponse.main.temp + " " + "<img src=http://openweathermap.org/img/w/" + searchResponse.weather[0].icon + ".png>")
console.log(searchResponse)
})
//for 5 day forecast
$.ajax({
url: queryURL,
method: "GET"
})
.then(function (weatherForecast) {
let dailyForecast = weatherForecast.list;
let temperatureArray = []
//for current temp
$.ajax({
url: currentTemp,
method: 'GET',
}).then(function(searchResponse) {
curr = searchResponse.main.temp;
currIcon = searchResponse.weather[0].icon;
$('#current-temp').html(
`${searchResponse.name}'s Current Temp: ${
searchResponse.main.temp
} <img src=http://openweathermap.org/img/w/${
searchResponse.weather[0].icon
}.png>`
);
});
//for 5 day forecast
$.ajax({
url: queryURL,
method: 'GET',
}).then(function(forecast) {
let dailyForecast = forecast.list;
let days = [
{
data: [],
time: moment()
.add(1, 'days')
.format('YYYY-MM-DD'),
},
{
data: [],
time: moment()
.add(2, 'days')
.format('YYYY-MM-DD'),
},
{
data: [],
time: moment()
.add(3, 'days')
.format('YYYY-MM-DD'),
},
{
data: [],
time: moment()
.add(4, 'days')
.format('YYYY-MM-DD'),
},
{
data: [],
time: moment()
.add(5, 'days')
.format('YYYY-MM-DD'),
},
];

for (i = 0; i < dailyForecast.length; i+=8) {
let day = moment(dailyForecast[i].dt_txt).format('ddd')
let hour = moment(dailyForecast[i].dt_txt).format('HH')
temperatureArray.push({
days: day,
tempMax: dailyForecast[i].main.temp_max,
tempMin: dailyForecast[i].main.temp_min,
icon: '<img src=http://openweathermap.org/img/w/' + dailyForecast[i].weather[0].icon + '.png>'
})

}
$(".day1").html(temperatureArray[0].days + " " + temperatureArray[0].icon + "<br>" + "Low: " + temperatureArray[0].tempMin + " " + "High: " + temperatureArray[0].tempMax);
$(".day2").html(temperatureArray[1].days + " " + temperatureArray[1].icon + "<br>" + "Low: " + temperatureArray[1].tempMin + " " + "High: " + temperatureArray[1].tempMax);
$(".day3").html(temperatureArray[2].days + " " + temperatureArray[2].icon + "<br>" + "Low: " + temperatureArray[2].tempMin + " " + "High: " + temperatureArray[2].tempMax);
$(".day4").html(temperatureArray[3].days + " " + temperatureArray[3].icon + "<br>" + "Low: " + temperatureArray[3].tempMin + " " + "High: " + temperatureArray[3].tempMax);
$(".day5").html(temperatureArray[4].days + " " + temperatureArray[4].icon + "<br>" + "Low: " + temperatureArray[4].tempMin + " " + "High: " + temperatureArray[4].tempMax);
console.log(temperatureArray)
//temperatureArray.filter(dayFilter(i))
//console.log('Temperature Array', temperatureArray.filter(dayFilter(i)))
// Break days up into 24hrs
dailyForecast.map(x => {
let curDate = x.dt_txt;
let matchDate = curDate.substr(0, curDate.indexOf(' '));
days.map(y => {
if (matchDate === y.time) {
y.data.push(x);
}
});
});

//Math.max(temperatureArray.filter(tempMax))
//Math.min(temperatureArray.filter(tempMin))

})
function getHighLow(temp) {
let tempArr = {
temp_low: 1000,
temp_high: 0,
high_icon: '',
low_icon: '',
};
temp.data.map(x => {
if (x.main.temp_max > tempArr.temp_high) {
tempArr.temp_high = x.main.temp_max;
tempArr.high_icon = `<img src=http://openweathermap.org/img/w/${
x.weather[0].icon
}.png>`;
}
if (x.main.temp_min < tempArr.temp_low) {
tempArr.temp_low = x.main.temp_min;
tempArr.low_icon = `<img src=http://openweathermap.org/img/w/${
x.weather[0].icon
}.png>`;
}
});
return tempArr;
}

function display(data) {
$('.temp').empty();
data.map(newDiv => {
$('.temp').append(
`<div class="card-body days col">
<div class='day1 arvo'>
${moment(newDiv.time).format('ddd')} ${
getHighLow(newDiv).high_icon
} Low: ${getHighLow(newDiv).temp_low} High: ${
getHighLow(newDiv).temp_high
}
</div>
</div>`
);
});
}




}
display(days);
});
}
Loading