è±ç²ã®é£æ£éãåå¾ãã Ruby ã¹ã¯ãªãã
åæ©
ã¡ãã£ã¨åã«è¦ããã¦ããè¨äºã§ãããèªåãè±ç²çã«è¦ããã§ããã¨ãããã¨ããããNadoka さんã® IRC Bot ã«ä»è¾¼ããã¨æã£ã¦ Ruby ã§æ¸ãã¦ã¿ã¾ããã :-p
ç°å¢çãéç¨ããè±ç²è¦³æ¸¬ã·ã¹ãã (ã¯ãªããã)ã§ã¯ï¼æéæ¯ã®è±ç²é£æ£ãã¼ã¿ãå ¬éããã¦ãã¾ãã 1æéæ¯ã«è±ç²é£æ£ãã¼ã¿ãæ´æ°ãããããå¤åºã®ã¿ã¤ãã³ã°ã決å®ããã®ã«å½¹ç«ã¡ã¾ãã ããã°ã©ãã§ããã°ããã®ãã¼ã¿ã cron ã§å®æçã«ç£è¦ãã¦è¦åã¡ã¼ã«ãéã£ãããShell ã®ããã³ãããemacs ã®ãããããã¡ã«è¡¨ç¤ºãããããã¨æãã¯ãã§ãã
ããã§ããã®è±ç²è¦³æ¸¬ã·ã¹ãã ã¯ãªãããããè±ç²é£æ£ãã¼ã¿ãåå¾ãã perl module ãä½æãã¾ããã
è±ç²æ
å ±ã«ããèå³ããªãã£ãã®ã§ã風åã風éæ
å ±ã¯ã¨ã£ã¦ãã¾ãããã»ãã¨ã¯ scraping ããã»ããããã£ãã®ããããã¾ãããããããæãæãã¡ããã¾ããã
èªåã¨ãã¦ã¯ãNet::HTTP ã§ã® Cookie ã®ä½¿ãæ¹ã¨ãRuby 1.9 ã§ã® Array#to_s ã®åããåå¼·ã«ãªã£ãããªãã¨ã
# Nadoka ããã Ruby 1.9 ã§åããã¦ããããããªããã§ããããã®ã¾ã¾ãã¼ãã£ã³ã°ããã®ãè¸ããªãã£ãã®ã§ããªãã¨ãªã 1.9 対å¿...
åä½ç¢ºèª
æ±äº¬ã®è¦³æ¸¬æã¯ä»¥ä¸ã¨ãªãã¾ããä»ã®å£ç¯ã¯ã¾ã ãé½å ã¯ãã»ã©ã§ã¯ãªãã§ãããå¤æ©æ£®æç§å¦åã®è±ç²éã¯ãã³ããªãã§ãã
ã¨ãªã¢ã³ã¼ã | 観測æã³ã¼ã | 観測æ |
---|---|---|
3 | 51310200 | æ±äº¬é½å¤æ©å°å¹³ä¿å¥æ |
3 | 51320100 | ç¬ç«è¡æ¿æ³äººæ£®æç·ç å¤æ©æ£®æç§å¦å |
3 | 51300100 | æ¥æ¬å»ç§å¤§å¦ä»å±å¤æ©æ°¸å±±ç é¢ |
3 | 51300200 | æ¥æ¬å»ç§å¤§å¦ä»å±ç é¢ |
12ææç¹ã§åããã¦ã¿ããã以ä¸ã®çµæã§ãããé£æ£éã 1000 ãè¶ããã¨ç¸å½å±éºãããã§ãã
# èªå㯠100åæªæºã®é½å¸é¨ã§ããããªããããããã§ãã...ã
% ruby191 hanako.rb 51310200 3 ææ°ã®è±ç²é£æ£éã¯ã32 [å/m3] 9 æã®è±ç²é£æ£éã¯ã24 [å/m3] % ruby191 hanako.rb 51320100 3 ææ°ã®è±ç²é£æ£éã¯ã520 [å/m3] 9 æã®è±ç²é£æ£éã¯ã61 [å/m3] % ruby191 hanako.rb 51300100 3 ææ°ã®è±ç²é£æ£éã¯ã86 [å/m3] 9 æã®è±ç²é£æ£éã¯ã20 [å/m3] % ruby191 hanako.rb 51300200 3 ææ°ã®è±ç²é£æ£éã¯ã69 [å/m3] 9 æã®è±ç²é£æ£éã¯ã8 [å/m3]
Array#to_s ã®æå
Ruby 1.8 ã§ã¯ Array#joinãRuby 1.9 ã§ã¯ Array#inspect ã¨åãåãã«ãªãã¾ãã
% irb186 >> %w(a b).to_s => "ab" >> %w(a b).inspect => "[\"a\", \"b\"]" >> %w(a b).join => "ab" >> exit % irb191 >> %w(a b).to_s => "[\"a\", \"b\"]" >> %w(a b).inspect => "[\"a\", \"b\"]" >> %w(a b).join => "ab" >> exit
ã¹ã¯ãªãã
Ruby 1.9 ãããªãã¨åãã¾ããã
ãããã·ã®è¨å®ãã§ããããã«ãã¦ã¾ãããçµç±ã®å¿
è¦ããªããã°é©å®æ¸ãããã¦ãã ããã
# coding: utf-8 require 'net/http' class Hanako Net::HTTP.version_1_2 PROXY_HOST = "xxx" PROXY_PORT = yyy HANAKO_BASE_HOST = "kafun.taiki.go.jp" attr_reader :kafun, :mst_code, :area_code def initialize(mst_code = "51300200", area_code = "3") @kafun = { } @mst_code = mst_code @area_code = area_code request_and_parse(mst_code, area_code) end def now now = @kafun.keys.sort.last @kafun[now] end private def request_and_parse(mst_code, area_code) cookie = nil Net::HTTP::Proxy(PROXY_HOST, PROXY_PORT).start(HANAKO_BASE_HOST) do |http| response, = http.get("/Hyou0.aspx?MstCode=#{mst_code}&AreaCode=#{area_code.rjust(2,"0")}") response.error! unless response.is_a? Net::HTTPOK cookie = response.get_fields("Set-Cookie").join.split(";").first end Net::HTTP::Proxy(PROXY_HOST, PROXY_PORT).start(HANAKO_BASE_HOST) do |http| response, = http.get("/Hyou2.aspx", "Cookie" => cookie) response.error! unless response.is_a? Net::HTTPOK parse(response) end end def parse(response) response.body.each_line do |line| if /<td><font size=\"2\">([0-9]*)æ<\/font><\/td><td align=\"Right\"><font size=\"2\">([0-9]*)<\/font><\/td>/ =~ line.force_encoding("UTF-8") @kafun[$1.to_i] = $2.to_i end end end end # == how to use == hanako = Hanako.new ARGV[0], ARGV[1] puts "ææ°ã®è±ç²é£æ£éã¯ã#{hanako.now} [å/m3]" puts "9 æã®è±ç²é£æ£éã¯ã#{hanako.kafun[9]} [å/m3]"