Web ad Fortune 無料APIサンプル
概要
年月日を指定すると12星座ごとの占い情報を取得出来ます。ユーザ登録は不要。
取得データフォーマットはJSON(JavaScript Object Notation)形式のため、rubyの標準モジュールでは読み込めません。ですので、WebOS Goodiesさんが公開されているRuby 用 JSON パーサーを使用させていただきました。ありがとうございます。
サンプルページ
http://sakura-computer.co.jp/webservice/webadfortu...
ソースコード
#!/usr/local/bin/ruby -Ku # # $Id$ # Web ad Fortune 無料APIサンプル # 今日の占いを表示します。 # Auther:Yasuo Tabira(ytabira at gmail.com) require 'cgi' require 'erb' require 'open-uri' require 'pp' require 'simple-json' # http://webos-goodies.jp/archives/51071565.html #APIのアドレス #以下のように年月日の文字列を付加して使用する。 #http://api.jugemkey.jp/api/horoscope/free/2007/12/04 REQUEST_URL = 'http://api.jugemkey.jp/api/horoscope/free' begin cgi = CGI.new # とりあえずヘッダは出力しておく。 print cgi.header ####################################### # システム日付を取得してURLを完成させる t = Time.now keystr = t.strftime("%Y/%m/%d") uri = "#{REQUEST_URL}/#{keystr}" puts '<pre>' + uri + '</pre>' if $DEBUG parser = JsonParser.new result = nil open(uri) do |f| obj = parser.parse(f.read) result = obj end ####################################### # 表示するページの作成 script = <<-"EOF" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Web ad Fortune無料APIサンプル</title> </head> <body> <h1><%=keystr%>の運勢</h1> <% result["horoscope"]["#{keystr}"].each { |table| %> <<%=table["sign"]%>の運勢><br/> 金運:<%=table["money"]%><br/> 仕事運:<%=table["job"]%><br/> 恋愛運:<%=table["love"]%><br/> 総合運:<%=table["total"]%><br/> 総評:<%=table["content"]%><br/> ランキング:<%=table["rank"]%><br/> ラッキーアイテム:<%=table["item"]%><br/> <hr/> <% } %> <p>powerd by <a href="http://jugemkey.jp/api/waf/api_free.php">JugemKey</a></p> <p>【PR】<a href="http://www.tarim.co.jp/">原宿占い館 塔里木</a></p> </body> </html> EOF e = ERB.new(script, nil, '-') e.run(binding) # 例外処理 rescue => ex puts '<pre>' + ex + '</pre>' puts '<pre>' pp ex.backtrace puts '</pre>' end
コメント(0) |
2007年12月04日(火) 22:35:36 Modified by ID:6iEpxHmiZg