Google自訂搜尋

目前分類:Ruby console (16)

瀏覽方式: 標題列表 簡短摘要
今天剛看到生活+釋出API(其實早就釋出了,API網址:http://tw.developer.yahoo.com/lifestyle_api.html)後,就開始把NetBeans打開來寫程式了XD
現在RubyForge的專案還沒開,倒是GoogleCode的已經開了(網址:http://code.google.com/p/yahoo-lifetype-api/)
程式是BSD授權,忘記怎樣包裝Gem檔,等到哪天想起來再包XD
程式使用範例(列出生活+的分類):
#!/usr/bin/env ruby
APPID = "NhYX9XjV34FPxdq7zD8T7wwc4QGI5VWu_48NHh03zbPYUfPpcWrpZzhcVDKFQsH9dQ--"
require 'lifetype'
require 'rexml/document'
include REXML

puts "獲取生活+類別中... 請稍後"
doc = Document.new(LifeType::Class.new(APPID).listClasses)
puts "獲取類別結束"
puts "類別總數: " + doc.get_elements("//rsp/ClassList")[0].attribute("count").to_s
puts "列出類別中... 請稍後"
doc.elements.each("//rsp/ClassList/Class") do |ele|
puts "ID: #{ele.attributes["id"]} -- #{ele.get_elements("Title")[0].text}"
end
puts "列出類別結束"
很簡單的就可以使用了,還有doc喔!
有Bug可以丟到GoogleCode的Issues或者丟到我信箱內
謝謝^^
Update: 2008/04/25 09:15
  現在可以從我的ShareMe抓0.3版了,請按我

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(292)

我把原始碼放在妹尬阿撲落得上面了
UTF-8ANSI碼兩種編碼版本
參考這篇文章撰寫的:[台灣論壇]  測試你的手機號碼是吉是凶
晚點再放上執行檔下載點...

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(350)

有鑒於某台主機的DNS常常掛掉.. 所以就寫了這個小程式..

while true
  `ps aux | grep named`.split("\n").each{|line|
    user, pid, cpu, mem, vsz, rss, tty, stat, start, time, *command = line.split("\s")
    flag = true if command[0] == "/usr/sbin/named"
    `/etc/init.d/named start` unless flag
  }
  sleep 300
end

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(318)

這個Library搭配http://www.twsms.com才可以使用喔!
有任何問題可以直接回這篇文章,或者寫信問我,我信箱是Gmail的,帳號跟我的這個部落格帳號一樣

我先說用法好了:
require 'twsms'
sms = TWSMS.new(username, password) # 帳號密碼
sms.sendSMS(mobile, message) # mobile: 目標手機號碼  message: 要傳的訊息
原始碼:
=begin
  == Information ==
  === Copyright: Apache 2.0
  === Author: CFC < zusocfc@gmail.com >
  === Prog. Name: TWSMS lib
  === Version: 0.1
  == Introduction ==
    TWSMS(Taiwan SMS)
    TWSMS is a SMS sender, it must use with http://www.twsms.com.
    There has no any library for the SMS system in Taiwan. So, I just coded this and release this version.
    This version just support for sending SMS.
  == Featured ==
   
  == Using TWSMS ==
    It just support for standalone class now.
    require it before you use.
  === Using TWSMS by standalone class
    require 'twsms'
    sms = TWSMS.new('username', 'password')
    sms.sendSMS('09xxxxxxxx', 'Hi, there! TWSMS library is so easy to use!')
    sms.sendSMS('09xxxxxxxx', 'Send SMS with options',
        :popup => 1,
        :type => "now",
        :mo => "Y")
=end

%w|uri cgi net/http|.each{|r| require r}

class TWSMS
  def initialize(username, password)
    @uname, @upwd = username, password
    @options = {
      :type => "now", # Sending type: now, vld
      :popup => "",
      :mo => "Y".upcase,
      :vldtime => "86400",
      :modate => "",
      :dlvtime => "",
      :wapurl => "",
      :encoding => "big5"
    }
   
    @errors = {
      -1.to_s.to_sym => "Send failed",
      -2.to_s.to_sym => "Username or password is invalid",
      -3.to_s.to_sym => "Popup tag error",
      -4.to_s.to_sym => "Mo tag error",
      -5.to_s.to_sym => "Encoding tag error",
      -6.to_s.to_sym => "Mobile tag error",
      -7.to_s.to_sym => "Message tag error",
      -8.to_s.to_sym => "vldtime tag error",
      -9.to_s.to_sym => "dlvtime tag error",
      -10.to_s.to_sym => "You have no point",
      -11.to_s.to_sym => "Your account has been blocked",
      -12.to_s.to_sym => "Type tag error",
      -13.to_s.to_sym => "You can't send SMS message by dlvtime tag if you use wap push",
      -14.to_s.to_sym => "Source IP has no permission",
      -99.to_s.to_sym => "System error!! Please contact the administrator, thanks!!"
    }
    @args = []
    @url ||= "http://api.twsms.com/send_sms.php?"
    @url += "username=" + @uname
    @url += "&password=" + @upwd
  end
 
  def sendSMS(mobile, message, opt={})
    @options[:mobile], @options[:message] = mobile, message
    @options.merge!(opt).each{|k, v| @args << k.to_s + "=" + CGI::escape(v.to_s)}
    @url += "&" + @args.join("&")
    self.chk_val
    chk_errors(Net::HTTP.get(URI.parse(@url)))
  end
 
  def chk_val
    @options[:dlvtime] = "" unless @options[:type] == "dlv"
    @options[:wapurl] = "" if @options[:type] != ("push" && "upush")
  end
 
  def chk_errors(resp)
    resp = resp.split("=")[1]
    if @errors.has_key?(resp.to_s.to_sym)
      puts "==========", "Error!! Message: ", @errors[resp.to_s.to_sym]
    else
      puts "==========", "Message has been send! Your message id is: " + resp.to_s
    end
  end
 
  protected :chk_val
end
晚點丟到Google Code Hosting上去...

Updated:
TWSMS on Google Code Hosting: http://code.google.com/p/twsms/
SMSender on RubyForge: http://rubyforge.org/projects/smsender/

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(591)

class Array
  def which_long?
    # Version 1.0
    # Coded by CFC < zusocfc  @ gmail . com >
    # PLEASE DO NOT REMOVE THE COMMENT OF THIS FUNCTION, THANKS A LOT.
    # Usage:
    #   ['a', 'ab', 'abc' 1234].which_long?
    #   => 1234
    self.size.times{|i| self[i]=self[i].to_s}
    max, long = 0, String.new
    self.each{|item| item.size > max ? (max = item.size; long = item) : next}
    long
  end
end

以上是原始碼,使用方式如下:

puts ['a', 'ab', 'abc', 1234].which_long?
=> 1234

授權還沒定,不過大家還是可以拿去使用:P
請不要拿掉註解.. 謝謝

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(378)

版本1可以不用寫群組名稱,但是程式碼好醜ˊˋ
版本2必須要有群組名稱,適用於學校(?)

版本1下載
版本2下載

版本1:

#!/usr/bin/env ruby
File.open(ARGV[0]) do |file|
  while a = file.gets
    a = a.chomp.split(/ /)
    print "username => #{a[0]} ", "password => #{a[1]} ", "group => #{a[2]}", "\n"
    a[2].nil? ? `useradd -m #{a[0]}` : `useradd -m -G #{a[2]} #{a[0]}`
    `echo #{a[0]}:#{a[1]} | chpasswd`
  end
end
exec "pwconv"

使用者清單寫法:

  帳號 密碼 群組

版本2:

#!/usr/bin/env ruby
require 'yaml'
YAML.load_file(ARGV[0]).each{ |grp|
  grp.each{ |usr|
    usr.each{ |i|
      info = i.chomp.split(/ /)
      `useradd -m -G #{grp[0]} #{info[0]}`
      `echo #{info[0]}:#{info[1]} | chpasswd`
    }
  }
}
`pwconv`

使用者清單寫法:
grp1:
  - usr1 pwd1
  - usr2 pwd2
grp2:
  - usr3 pwd3
  - usr4 pwd4
grp3:
  - usr5 pwd5
  - usr6 pwd6
使用方式都是:
./account list

程式授權.. 隨便啦

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(388)

1 2 3