Skip to content

Instantly share code, notes, and snippets.

@nefo-mi
Created August 31, 2012 12:51
Show Gist options
  • Save nefo-mi/3552317 to your computer and use it in GitHub Desktop.
Save nefo-mi/3552317 to your computer and use it in GitHub Desktop.
天気予報を取得するスクリプト
#!/usr/bin/env/ruby
# encoding : utf-8
require 'open-uri'
require 'json'
CACHE = "/tmp/weather.rb.cache"
if File.exist?(CACHE) && (Time.now - File::Stat.new(CACHE).mtime) <= 1800
print File.read(CACHE)
exit
end
begin
uri = URI.parse('http://api.wunderground.com/api/Your_Key/conditions/lang:JP/q/Japan/Okinawa.json')
json_string = uri.read('r:binary').encode("UTF-8", invalid: :replace, undef: :replace)
parsed_json = JSON.parse(json_string)
conditions = parsed_json['current_observation']
rescue Exception => ex
error_no = ex.message.split("\s").first
open(CACHE, 'w'){|io| io.print error_no}
print error_no
exit
end
weather = conditions['weather']
temp_c = conditions['temp_c']
wind_dir = conditions['wind_dir']
wind_kph = conditions['wind_kph']
message = [weather, temp_c, 'C', wind_dir, wind_kph , 'km/h'].join(' ')
if message.nil?
File.delete CACHE
print 'err'
else
open(CACHE, 'w'){|io| io.print message}
print message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment