[ create a new paste ] login | about

Link: http://codepad.org/HF3DOG9L    [ raw code | fork ]

NeoCat - Ruby, pasted on May 24:
#!/usr/local/bin/ruby
#-*- coding: utf-8 -*-
require 'socket'
require 'open-uri'

class Hue
  def Hue::to_rgb(h)
  h = h % (2 * Math::PI)
  c = (h / (Math::PI / 3)).to_i
  p = ((h % (Math::PI / 3)) / (Math::PI / 3) * 255).to_i
  case c
  when 0
    [255, p, 0]
  when 1
    [255 - p, 255, 0]
  when 2
    [0, 255, p]
  when 3
    [0, 255 - p, 255]
  when 4
    [p, 0, 255]
  when 5
    [255, 0, 255 - p]
  end 
  end
end

def get_rainfall
  rainfall = {}
  
  open("http://weather.yahoo.co.jp/weather/jp/14/4610.html") do |f|
    data = f.readlines
    data.map! {|l| l.encode("UTF-8") }
    mode = :None
    cnt = 0
    r = []
    date = []
    data.each do |l|
      # puts l
      if mode == :None
        mode = :Date if l =~ /---Today---/ || l =~ /---Tomorrow---/
      elsif mode == :Date
        if l =~ /<b>(\d+)月(\d+)日/
          date = $1.to_i * 100 + $2.to_i
          mode = :Rainfall
        end
      elsif mode == :Rainfall
        mode = :Capture if l =~ /降水確率/
      elsif mode == :Capture
        l =~ />([-\d]+)</
        r.push $1.to_i
        cnt += 1
        if cnt == 4
          rainfall[date] = r
          cnt = 0
          r = []
          mode = :None
        end
      end
    end
  end
  rainfall
end

def set_color(s, rainfall, id)
  last = "000000"
  4.times do |i|
    t = Time.now + (i+1)*6*60*60
    x = t.month * 100 + t.day
    h = t.hour / 6
    
    rgb = Hue::to_rgb(rainfall[x][h] * Math::PI / 100 * 4/3).map {|x| sprintf("%02x", x) }.join
    puts "#{id} - #{rainfall[x][h]} - #{rgb}"
    s.puts id + rgb + last
    last = rgb
    sleep 0.4
    s.puts id + rgb + rgb
    sleep 1
  end
end

s = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
sockaddr = Socket.sockaddr_in(23, "192.168.0.201")
s.connect(sockaddr)
while l = s.gets
  if l =~ /(boxel\d\d\d) start/
    id = $1
    puts $&
    rainfall = get_rainfall
    set_color(s, rainfall, id)
  end
end


Create a new paste based on this one


Comments: