-
-
Save andyfoster/14f42377f73ab9aa8d8d to your computer and use it in GitHub Desktop.
Alternate version (with Emoji) of the Today-Scripts weather script.
This file contains 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 "colorize" | |
def formatTemp(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 | |
url = "http://api.openweathermap.org/data/2.5/weather?q=" + ARGV[0] + "," + ARGV[1] + "&units=metric" | |
resp = Net::HTTP.get_response(URI.parse(url)) | |
data = resp.body | |
map = JSON.parse(data) | |
weather = map['weather'][0]['main'].downcase | |
weatherDesc = map['weather'][0]['description'].capitalize | |
weatherId = map['weather'][0]['id'].to_i | |
weatherIcon = case weather | |
when "rain", "drizzle" then case weatherId | |
when 300..500 then "π¦" | |
when 501..511 then "π§" | |
when 520..531 then "βοΈ" | |
else weather.capitalize | |
end | |
when "thunderstorm" then "β‘οΈ" | |
when "clouds", "atmosphere", "extreme" then case weatherId | |
when 701, 711, 721, 741 then "π" | |
when 731, 751 then "β³" | |
when 762 then "π" | |
when 771, 781, 900, 902 then "π" | |
when 800, 904 then "βοΈ" | |
when 801..802 then "β οΈ" | |
when 803..804 then "βοΈ" | |
when 901 then "β‘οΈ" | |
when 903, 906 then "βοΈ" | |
when 904 then "βοΈ" | |
when 905 then "π¨" | |
else weather.capitalize | |
end | |
when "snow" then weatherIcon = "βοΈ" | |
when "additional" then weatherIcon = "π¨" | |
else weather.capitalize | |
end | |
puts "Weather".bold.colorize(:magenta) + ": #{weatherIcon} #{weatherDesc}" | |
puts "" | |
temp = formatTemp(map['main']['temp'].round.to_s) | |
low = formatTemp(map['main']['temp_min'].round.to_s) | |
high = formatTemp(map['main']['temp_max'].round.to_s) | |
puts "Temperature".bold.colorize(:yellow) + ": #{temp}, " + | |
"Low".bold.colorize(:blue) + ": #{low}, " + "High".bold.colorize(:red) + | |
": #{high}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment