#!/usr/bin/env ruby # # Created by Demitrious S. Kelly on 2007-02-07. # # License: GPL Version 2, see # http://www.gnu.org/licenses/gpl.txt # (and pretend it's attached or included # in some way in/to this file) # # Release Version 0.001 # # Warranty: None, at all, whatsoever, use at your own # risk, may burn down your house and knock over your # garbage cans and return the car with the gas level # on "E" and refuse to return your lawnmower even though # it's now 7 months later and you're growing a small # rainforest out back... might fall asleep while # watching your children, forget to pay the electric # bill, and run up massive credit card debt. In other # words. You're on your own. Dont come crying to me! # # FAQ: # # Q: so whats with these dumb data functions? Aren't # they just modified urls? I could do that if I wanted # too. # # A: I've included those functions in this library be- # cause I intend this library to be included in any # application with as little work as possible. This # API does not assume that you want to store just URLS # (and to share them) it assumes you want to use the # del.icio.us service to att tagging to YOUR application # tagging to whatever data you have laying around. Also # these data:,/ urls are private by default (I'm not # even sure if you CAN share them.) Short answer: Just # because... # # Using this API Interface # # # Initialize... # @del = Delicious.new('testuser', 'testpass', {:useragent => "ruby-Delicious-client-testrun"}) # # URLS # @del.tag_url("http://www.foo.com/", "foo dot com", "foo bar baz") # @del.delete_url_tags("http://www.foo.com/", "foo bar") # puts @del.fetch_url_tags("http://www.foo.com/").inspect # @del.delete_url("http://www.foo.com/") # # DATA # @del.tag_data("baz", "boo foo bar ick uck gross test") # @del.delete_data_tags("baz", "foo bar") # puts @del.fetch_data_tags("baz").inspect # @del.delete_data("baz") # # Implimented Functions: # # Building Block Functions ==================================== # fetch_detail # base_request_bool # tag # fetch_description # fetch_tags # Detail Oriented Functions =================================== # Data Specific Functions =================================== # fetch_data_tags (data) # tag_data (data, tags) # delete_data (data) # delete_data_tags (data, tags) # URL Specific Functions ==================================== # fetch_url_tags (url) # tag_url (url, description, tags) # delete_url (url) # delete_url_tags (url, tags) require "rubygems" require 'net/https' require "cgi" require "hpricot" class Delicious def initialize(username, password, options = {} ) @username = username @password = password @apihost = "api.del.icio.us" @apiport = 443 @apissl = true if options.has_key?(:apihost) @aapihost = options[:apihost] else @apiuseragent = "api.del.icio.us" end if options.has_key?(:apiport) @apiport = options[:apiport] else @apiport = 443 end if options.has_key?(:apissl) @apissl = options[:apissl] else @apissl = true end if options.has_key?(:apiuseragent) @apiuseragent = options[:apiuseragent] else @apiuseragent = "ruby-Delicious-client" end end def fetch_data_tags(data) return fetch_tags("data:/,#{data}") end def fetch_url_tags(url) return fetch_tags(url) end def fetch_description(url) description=fetch_detail(url, "description") if description == false or description == "" return url else return description end end def fetch_tags(url) tags=fetch_detail(url, "tag") if tags == false return [] else return tags.split end end def fetch_detail(url, detail) @url="/v1/posts/get?url="+CGI::escape(url) resp = href = ""; begin http = Net::HTTP.new(@apihost, @apiport) http.use_ssl = @apissl http.start do |http| req = Net::HTTP::Get.new(@url, {"User-Agent" => @apiuseragent}) req.basic_auth(@username, @password) response = http.request(req) resp = Hpricot(response.body) end return resp.at('post')[detail] rescue return false end end def tag_data(data, tags) return tag(CGI::escape("data:/,#{data}"), CGI::escape("#{data}"), CGI::escape(tags)) end def tag_url(url, description, tags) return tag(CGI::escape(url), CGI::escape(description), CGI::escape(tags)) end def tag(url, description, tags) @url = "/v1/posts/add?url=#{url}&description=#{description}&tags=#{tags}" return base_request_bool(@url) end def delete_data(data) return delete_url("data:/,#{data}") end def delete_url(url) @url = "/v1/posts/delete?url="+CGI::escape("#{url}") return base_request_bool(@url) end def delete_data_tags(data, tags) return delete_url_tags("data:/,#{data}", tags) end def delete_url_tags(url, tags) ## this is an ugly hack... ## there should be a better way to do this!!! @something_removed=false @oldtags=fetch_url_tags(url) for i in tags.split if @oldtags.member?(i) @something_removed=true @oldtags.delete(i) end end if @something_removed == true description = fetch_description(url) tag_url(url, description, @oldtags.join(" ")) end end def base_request_bool(url) if @username == nil or @password == nil return false end resp = href = ""; begin http = Net::HTTP.new(@apihost, @apiport) http.use_ssl = @apissl http.start do |http| req = Net::HTTP::Get.new(url, {"User-Agent" => @apiuseragent}) req.basic_auth(@username, @password) response = http.request(req) resp = Hpricot(response.body) end if resp.at("result")['code'] == "done" return true else return false end rescue return false end return true end end