Last active
August 29, 2015 14:11
-
-
Save komiyake/efcf04fcf971f087edcb to your computer and use it in GitHub Desktop.
Weather Hacksから今日と明日の天気を取得する。terminalに表示する用。
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/ruby | |
#encoding:UTF-8 | |
require 'net/http' | |
require 'uri' | |
require 'json' | |
def get_icon(weather) | |
icon = {"晴"=>"☀", "曇"=>"☁", "雨"=>"☂", "雪"=>"☃"} | |
state = {"時々"=>"~", "のち"=>">"} | |
if 2 < weather.size | |
icon[weather[0]] + " " + state[weather[1..2]] + icon[weather[3]] | |
else | |
icon[weather[0]] | |
end | |
end | |
# 地域のidを指定して天気を取得する | |
# http://weather.livedoor.com/weather_hacks/webservice | |
begin | |
uri = URI.parse('http://weather.livedoor.com/forecast/webservice/json/v1?city=250010') | |
json = Net::HTTP.get(uri) | |
data = JSON.load(json) | |
puts get_icon(data['forecasts'][0]['telop']) + " |" + get_icon(data['forecasts'][1]['telop']) | |
rescue | |
puts "✋ " | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment