Created
April 21, 2013 20:07
-
-
Save mckeed/5430893 to your computer and use it in GitHub Desktop.
My script to use the DarkSky API to tell me when it's going to start raining in the next hour. When I bike to work, I run this on a launchd job every 10 minutes from 4:00-6:00.
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
#! /usr/bin/env ruby | |
# encoding: UTF-8 | |
require 'rubygems' | |
require 'active_support/core_ext' | |
require 'rest_client' | |
require 'json' unless defined?(JSON) | |
require 'growl' | |
API_KEY = ENV["DARKSKY_API_KEY"] | |
LAT_LON = "44.982953,-93.246889" | |
URL = "https://api.darkskyapp.com/v1/forecast/#{API_KEY}/#{LAT_LON}" | |
def spark_char_for_ratio(x) | |
@spark_chars ||= %w(▁ ▂ ▃ ▄ ▅ ▆ ▇ █) | |
@spark_chars[(x * (@spark_chars.count - 1)).floor] | |
end | |
def growl_summary_and_hour_forecast(forecast) | |
message = "" | |
forecast["hourPrecipitation"].each_slice(5) do |mins| | |
ratio = mins.sum {|m| m["probability"].to_f } / mins.count | |
message << spark_char_for_ratio(ratio) | |
end | |
title = forecast["currentSummary"] | |
if forecast["hourSummary"] != forecast["currentSummary"] | |
title << ", " << forecast["hourSummary"] | |
end | |
Growl.notify message, :title => title, | |
:icon => "~/Pictures/Icons/Dark Sky/darksky_light.png" | |
end | |
forecast = JSON.parse(RestClient.get(URL)) | |
if forecast["isPrecipitating"] || forecast["minutesUntilChange"] > 0 | |
growl_summary_and_hour_forecast(forecast) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment