-
-
Save girliemac/27374e930e6de2012a65 to your computer and use it in GitHub Desktop.
Weather Infographic Gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pub() { | |
console.log("publishing") | |
pubnub.publish({ | |
channel: "wnPutTime", | |
message: { | |
'uuid': uuid | |
}, | |
callback: function(m) { | |
console.log(m) | |
} | |
}) | |
} | |
//PubNub Subscribe registration | |
pubnub.subscribe({ | |
channel: 'wnGet', | |
message: function(message) { | |
places = message | |
}, | |
connect: pub, | |
error: function(err) { | |
console.log(err) | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function refreshView() { | |
Object.keys(places).forEach(function(key){ | |
var name = key.split(' ').join('') | |
var place = places[key] | |
$('.'+name).replaceWith(getPopup(place)) | |
if ($('#'+name).length) { | |
$('#'+name).replaceWith(getMarker(place)) | |
} | |
else { | |
L.marker(place.coordinates, { | |
icon: getIcon(place) | |
}).bindPopup(getPopup(place), popupOptions).addTo(map) | |
} | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getPopup(place) { | |
var template = $('#popup').html() | |
var timeZoneId = place.timeZoneId | |
var info = { | |
place: place.place.split(' ').join(''), | |
sunrise: moment(place.sunrise).tz(timeZoneId).format('hh:mm:ss A'), | |
sunset: moment(place.sunset).tz(timeZoneId).format('hh:mm:ss A'), | |
time: moment().tz(timeZoneId).format('hh:mm:ss A'), | |
temperature: Math.floor(place.temperature - 273.15).toString() + ' \xB0 C', | |
summary: place.description, | |
humidity: place.humidity | |
} | |
return Mustache.render(template, info) | |
} | |
function getMarker(place) { | |
var template = $('#map-marker').html() | |
var weather = { | |
place: place.place, | |
id: place.place.split(' ').join(''), | |
humidity: place.humidity, | |
weather: place.icon, | |
tempColor: getTempColor(place.temperature) | |
} | |
place.temperature - 273.15 < minTemp ? weather.tempSolid = '#'+colorCold : place.temperature -273.15 > maxTemp ? weather.tempSolid = '#'+colorHot : | |
place.wind >= 11 ? weather.wind = 'windHigh' : weather.wind = 'wind' | |
moment(place.sunrise).isBefore(moment()) == moment(place.sunset).isBefore(moment()) ? weather.backgroundColor = 'rgba(0, 0, 0, 0.2)' : weather.backgroundColor = 'rgba(252, 246, 177, 0.6)' | |
return Mustache.render(template, weather) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(err, results) { | |
console.log("Error OR Results", JSON.stringify(err || results)); | |
var data = { | |
'place': place, | |
'coordinates': coordinates.reverse(), | |
'timeZoneId': results[0].timeZoneId, | |
'sunrise': results[1].sunrise, | |
'sunset': results[1].sunset, | |
'temperature': results[2].main.temp, | |
'wind': results[2].wind.speed, | |
'humidity': results[2].main.humidity, | |
'icon': results[2].weather[0].icon, | |
'description': results[2].weather[0].description | |
}; | |
if (i == cities.length -1) { | |
aggregate[place] = data; | |
currentConditions = aggregate | |
pub(currentConditions) | |
} | |
else { | |
++i | |
console.log('fetching city...', i) | |
aggregate[place] = data; | |
fetch(cities[i], cityCoordinates[cities[i]], i, aggregate) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fetch (place, coordinates, i, aggregate) { | |
async.parallel([ | |
function(callback) { | |
request | |
.get('https://maps.googleapis.com/maps/api/timezone/json') | |
.query('location='+coordinates[1]+','+coordinates[0]) | |
.query({timestamp: Math.floor(new Date().getTime()/1000)}) | |
.query({key: gApiToken}) | |
.end(function(err, res){ | |
if(err) { console.log(err); callback(true); return; } | |
obj = res.body; | |
callback(false, obj); | |
}); | |
}, | |
function(callback){ | |
request | |
.get('http://api.sunrise-sunset.org/json') | |
.query({lat:coordinates[1]}) | |
.query({lng: coordinates[0]}) | |
.query({formatted: 0}) | |
.end(function(err, res){ | |
if(err) { console.log(err); callback(true); return; } | |
obj = res.body.results; | |
callback(false, obj); | |
}) | |
}, | |
function(callback){ | |
request | |
.get('http://api.openweathermap.org/data/2.5/weather') | |
.query({lat:coordinates[1]}) | |
.query({lon: coordinates[0]}) | |
.query({APPID: appId}) | |
.end(function(err, res){ | |
if(err) { console.log(err); callback(true); return; } | |
obj = res.body; | |
callback(false, obj); | |
}) | |
} | |
], | |
/* | |
* Collate results | |
*/ | |
function(err, results) { | |
if (err) { | |
console.log('Error in fetching from API') | |
return | |
} else { | |
try { | |
console.log("Results", JSON.stringify(results)); | |
var data = { | |
'place': place, | |
'coordinates': [coordinates[1], coordinates[0]], | |
'timeZoneId': results[0].timeZoneId, | |
'sunrise': results[1].sunrise, | |
'sunset': results[1].sunset, | |
'temperature': results[2].main.temp, | |
'wind': results[2].wind.speed, | |
'humidity': results[2].main.humidity, | |
'icon': results[2].weather[0].icon, | |
'description': results[2].weather[0].description | |
}; | |
if (i == cities.length -1) { | |
aggregate[place] = data; | |
currentConditions = aggregate | |
pub(currentConditions) | |
} | |
else { | |
++i | |
console.log('fetching city...', i) | |
aggregate[place] = data; | |
fetch(cities[i], cityCoordinates[cities[i]], i, aggregate) | |
} | |
} catch (e) { | |
console.log(e) | |
} | |
} | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment