Last active
August 29, 2015 14:22
-
-
Save dan-c-underwood/814336f119c258fee961 to your computer and use it in GitHub Desktop.
Forecast.io based script designed for Today-Scripts. Powered by Forecast.
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
require "json" | |
require "net/http" | |
require "yaml" | |
require "colorize" | |
require "forecast_io" | |
def format_temp(temp) | |
temp_i = temp.to_i | |
temp = temp + "Β°C" | |
case | |
when temp_i <= 0 | |
temp.colorize(:blue) | |
when temp_i < 5 | |
temp.colorize(:light_blue) | |
when temp_i < 10 | |
temp.colorize(:cyan) | |
when temp_i < 15 | |
temp.colorize(:light_cyan) | |
when temp_i < 20 | |
temp.colorize(:yellow) | |
when temp_i < 25 | |
temp.colorize(:light_yellow) | |
when temp_i < 30 | |
temp.colorize(:light_red) | |
when temp_i >= 30 | |
temp.colorize(:red) | |
else | |
temp | |
end | |
end | |
def to_celsius(temp) | |
((temp - 32) * 5.0 / 9.0).round(1) | |
end | |
config = YAML.load_file(ARGV[0]) | |
ForecastIO.configure do |configuration| | |
configuration.api_key = config["api_key"] | |
end | |
lat = ARGV[1] | |
lon = ARGV[2] | |
forecast = ForecastIO.forecast(lat, lon, params: { exclude: "minutely,hourly,daily,alerts,flags"}) | |
weatherDesc = forecast.currently.summary | |
weatherIcon = case forecast.currently.icon | |
when "clear-day" then "βοΈ" | |
when "clear-night" then "π" | |
when "rain" then "π§" | |
when "snow", "sleet" then "βοΈ" | |
when "wind" then "π¨" | |
when "fog" then "π" | |
when "cloudy" then "βοΈ" | |
when "partly-cloudy-day", "partly-cloudy-night" then "β οΈ" | |
when "thunderstorm" then "β‘οΈ" | |
else "π" | |
end | |
precip = (forecast.currently.precipProbability * 100).round(1).to_s + "%" | |
temp = format_temp(to_celsius(forecast.currently.temperature).to_s) | |
tempFeel = format_temp(to_celsius(forecast.currently.apparentTemperature).to_s) | |
puts "Weather".bold.colorize(:magenta) + ": #{weatherDesc} #{weatherIcon}" | |
puts "Precipitation".bold.colorize(:cyan) + ": #{precip}" | |
puts "" | |
puts "Temperature".bold.colorize(:red) + ": #{temp}, " + | |
"Feels".bold.colorize(:yellow) + ": #{tempFeel}" | |
puts "Powered by Forecast".bold.colorize(:blue) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment