Skip to content

Instantly share code, notes, and snippets.

@cosmo0920
Created July 2, 2012 15:21
Show Gist options
  • Save cosmo0920/3033759 to your computer and use it in GitHub Desktop.
Save cosmo0920/3033759 to your computer and use it in GitHub Desktop.
上手いパース方法ないかなぁ
#!/usr/bin/env ruby -Ku
require "open-uri"
require "rubygems"
require "json"
require "time"
src = ''
group = "1-A"
begin
open("http://kteiden.chibiegg.net/api/kteiden.json?&group=#{group}"){|file|
src = file.read.gsub(/^\[/,"").gsub(/\]$/,"") #前後についている[]を取り払う
}
#jsonパーサーを使う
json = JSON.parser.new(src)
#突っ込むためのハッシュ作成
h = Hash.new
h = json.parse()
source = h["source"].to_f
if source == 1 then
info = "月間予定"
elsif source == 2 then
info = "翌日予定"
elsif source == 3 then
info = "当日予定"
else
info = "情報なし"
end
t = Time.parse(h["date"])
print "日付 : ", t.year,"/", t.month, "/", t.day, "\n"
printf("グループ : %s\n", h["group"])
print "計画停電対象時間 : ", h["start"], "から", h["end"], "まで\n"
print "計画停電の有無 : ", h["is_implemented"]? "実施有り" : "実施無し", "\n"
print "グループ内停電順位 : ", h["order"], "\n"
print "情報源 : ", info , "\n"
rescue JSON::ParserError
print "json parse error."
rescue OpenURI::HTTPError
print "Http bad request."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment