#!/usr/bin/ruby require 'cgi' require 'md5' require 'net/http' require 'net/https' require 'rexml/document' require 'date' include REXML class SingleLink def initialize(href,description,extended,hash,others,tags,time) @href=href @description=description @extended=extended @hash=hash @others=others @tags=tags @time=Time.parse(time) end attr_reader :username, :href, :description, :extended, :hash, :others, :tags, :time end class LinkList include Enumerable def initialize @links=Array.new end def add_link(link) @links.push(link) self #chain several calls together .add_link().add_link()... end def[](index) @links[index] end def length @links.length end def each @links.each { |x| yield x } end end class HttpCom def initialize(username,password) @username=username @password=password @doc_body=nil end def getHttpDoc(params) sleep(1.1) http = Net::HTTP.new('api.del.icio.us',443) http.use_ssl = true http.start { |http| req = Net::HTTP::Get.new("/v1/#{params}") req.basic_auth(@username, @password) response = http.request(req) @doc_body = response.body #print @doc_body,"\n" } end def parseLinks(links) doc = Document.new(@doc_body.to_s) doc.elements.each('posts/post') { |element| link=SingleLink.new(element.attributes['href'],element.attributes['description'],element.attributes['extended'],element.attributes['hash'],element.attributes['others'],element.attributes['tag'].split(' '),element.attributes['time']) links.add_link(link) } return links end def getUpdate getHttpDoc('posts/update') doc = Document.new(@doc_body.to_s) update_time = nil doc.elements.each('update') { |element| update_time = Time.parse(element.attributes['time']) } return update_time end def getValidDates valid_dates=Array.new getHttpDoc("posts/dates") doc = Document.new(@doc_body.to_s) doc.elements.each('dates/date') { |element| valid_dates.push(Time.parse(element.attributes['date'])) } return valid_dates end def getByDate(*date) valid_dates=getValidDates if date[0] year=date[0] else year='[0-9][0-9][0-9][0-9]' end if date[1] month=sprintf("%0.2d",date[1]) else month='[0-9][0-9]' end if date[2] day=sprintf("%0.2d",date[2]) else day='[0-9][0-9]' end request_date = Time.parse("#{year}-#{month}-#{day}") links=LinkList.new valid_dates.each { |x| if request_date.to_i == x.to_i getHttpDoc("posts/get?dt=#{x.year}-#{x.mon}-#{x.day}") links=parseLinks(links) end } return links end def getAllXml getHttpDoc('posts/all') return @doc_body end def getAll getHttpDoc('posts/all') links=LinkList.new links=parseLinks(links) return links end def getLastN(n) getHttpDoc("posts/recent?count=#{n}") links=LinkList.new links=parseLinks(links) return links end end