Skip to content

Instantly share code, notes, and snippets.

@nefo-mi
Created August 15, 2012 22:59
Show Gist options
  • Save nefo-mi/3364462 to your computer and use it in GitHub Desktop.
Save nefo-mi/3364462 to your computer and use it in GitHub Desktop.
天気予報を取得するスクリプト ref: http://qiita.com/items/04d2c773d4ee429cc295
#!/usr/bin/env ruby
# encoding : utf-8
require 'open-uri'
require 'rexml/document'
CACHE = "/tmp/ustatus.rb.cache"
if File.exist?(CACHE) && (Time.now - File::Stat.new(CACHE).mtime) <= 2000
print File.read(CACHE)
exit
end
url = URI.parse('http://www.google.com/ig/api?weather=naha,okinawa&hl=ja')
res = url.read('r:binary').encode("UTF-8", invalid: :replace, undef: :replace)
rss = REXML::Document.new(res)
condition = rss.elements['//weather/current_conditions/condition/@data'].value.strip
wind_condition = rss.elements['//weather/current_conditions/wind_condition/@data'].value.strip
wind, windy, tani = *wind_condition.split(':').last.split(' ')
temp_c = rss.elements['//weather/current_conditions/temp_c/@data'].value.strip + 'C'
msg = [condition, temp_c, wind, windy + tani].join(' ')
if msg.nil?
File.delete CACHE
else
open(CACHE, 'w'){|io| io.print msg}
print msg
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment