close
這次的這個版本,則是將 "最新書籤(整個網站)", "使用者最新書籤", "群組最新書籤" 3個合併囉
也就是說,使用者只要下參數,就可以抓到資訊了!
參數:

$ ruby ./recent.rb [要抓的資訊] [要顯示的書籤量] [名稱]

* 整個程式省略掉參數的話,就是整個網站的最新書籤

要抓的資訊:
  0: 整個網站的最新書籤
  1: 使用者最新書籤
  2: 群組最新書籤

這個版本還沒有Method overload... 因此沒有辦法省略掉其中的幾個參數..
未來版本會修正這個問題
程式碼如下:

#!/usr/bin/env ruby
# Code by CFC Zuso Security
# # Taiwan Ruby Users Group: http://www.ruby.oss.tw/
# # Rails Taiwan: http://www.rubyonrails.org.tw/
#
# Hemidemi bookmarks loader: Ver. 0.0.0.2 Beta
#
#==========
#
# 1. Recent: http://www.hemidemi.com/rss/bookmark/recent.xml
# 2. User's recent: http://www.hemidemi.com/rss/user/#{@usr_name}/bookmark/recent.xml
# 3. Group's recent: http://www.hemidemi.com/rss/group/#{@grp_name}/bookmark/recent.xml
#
#==========
#

require 'rexml/document'
require 'open-uri'
include REXML

class Recent
  def initialize(opt, count, name)
    case opt.to_i
      when 0
        @url = "http://www.hemidemi.com/rss/bookmark/recent.xml"
      when 1
        @url = "http://www.hemidemi.com/rss/user/#{name}/bookmark/recent.xml"
      when 2
        @url = "http://www.hemidemi.com/rss/group/#{name}/bookmark/recent.xml"
      else
        @url = "http://www.hemidemi.com/rss/bookmark/recent.xml"
    end
    @channel = @channel || {}
    @items = []
    if count == nil
      @count = 6
    else
      @count = count.to_i
    end
  end

  def run
    load_in
    show
  end

  def load_in
    open(@url) do |f|
      xml = Document.new(f.read)

      xml.elements.each("*/channel") do |chnl|
        @channel["name"] = chnl.elements["description"].text
        @channel["link"] = chnl.elements["link"].text
      end

      xml.elements.each("*/channel/item") do |item|
        itm = {}
        itm["title"] = item.elements["title"].text
        itm["description"] = item.elements["description"].text
        itm["link"] = item.elements["link"].text
        itm["creator"] = item.elements["dc:creator"].text
        @items << itm
      end
    end
  end

  def show
    line = "=" * 100
    puts @url
    puts line
    puts "#{@channel["name"]} [ #{@channel["link"]} ]"
    puts "共有#{@items.size}個書籤"
    puts line
    @count = @items.size if @items.size < @count
    @count.times { |cnt|
      puts "張貼者:#{@items[cnt]["creator"]} [ http://www.hemidemi.com/user/#{@items[cnt]["creator"]}/home ]"
      puts "標題:#{@items[cnt]["title"]}"
      puts "網址:#{@items[cnt]["link"]}"
      puts "敘述:#{@items[cnt]["description"]}"
      puts "-"*10
    }
    puts line
  end
  public :run
  private :load_in, :show
end

rss = Recent.new(ARGV[0], ARGV[1], ARGV[2])
rss.run

下載點:
http://stmail.tajen.edu.tw/~593092514/recent.rb
arrow
arrow
    全站熱搜

    hechian 發表在 痞客邦 留言(0) 人氣()