Created
February 5, 2015 20:24
-
-
Save ciryon/ade5d00316826341deb1 to your computer and use it in GitHub Desktop.
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
# Prep: npm install whatismyip geoip-lite openweathermap | |
# Usage: coffee weather.coffee hostname | |
# Provide hostname as argument and weather is looked up for that hostname. | |
# Skip argument and YOUR location is used (if found) | |
# Patched together one cold evening by Christian Hedin, github.com/ciryon | |
# | |
dns = require 'dns' | |
ip = require 'whatismyip' | |
geoip = require 'geoip-lite' | |
weather = require 'openweathermap' | |
weather.defaults {units:'metric'} | |
options = { | |
url: 'http://checkip.dyndns.org/', | |
truncate: '', | |
timeout: 3000, | |
matchIndex: 0 | |
} | |
lookup_ip = (callback) -> | |
console.log "Looking up your IP..." | |
ip.whatismyip options, (err, data) -> | |
console.log "Found IP: #{data.ip}" | |
callback data.ip | |
lookup_geo_for_ip = (ip) -> | |
console.log "Locating..." | |
geo = geoip.lookup(ip) | |
console.log "Found location #{geo.city}, #{geo.country}" if geo? | |
return geo | |
lookup_geo_and_weather = (ip) -> | |
geo = lookup_geo_for_ip ip | |
if geo | |
console.log "Looking up weather for: #{geo.ll[0]},#{geo.ll[1]}" | |
weather.find { lon: geo.ll[1], lat: geo.ll[0]}, (resp) -> | |
#console.log resp | |
temp = resp.list[0].main.temp | |
weather = (resp.list[0].weather[0]) | |
console.log "" | |
console.log "We got #{temp}ºC and #{weather.description}" | |
console.log "It's COLD!" if temp < 3.0 | |
else | |
console.log "geo lookup failed" | |
# MAIN | |
if process.argv[2] | |
console.log "Looking up #{process.argv[2]}" | |
dns.lookup process.argv[2], (err, addresses, family) -> | |
ip = addresses | |
lookup_geo_and_weather ip | |
else | |
lookup_ip lookup_geo_and_weather | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment