/usr/lib/ruby/1.8/rubilicious.rb is in librubilicious-ruby1.8 0.1.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 | #######################################################################
# Rubilicious - Delicious (http://del.icio.us/) bindings for Ruby #
# by Paul Duncan <pabs@pablotron.org> #
# #
# #
# For the latest version of this software, Please see the Rubilicious #
# page at http://www.pablotron.org/software/rubilicious/. #
# #
# #
# Copyright (C) 2004 Paul Duncan. #
# #
# Permission is hereby granted, free of charge, to any person #
# obtaining a copy of this software and associated documentation #
# files (the "Software"), to deal in the Software without #
# restriction, including without limitation the rights to use, copy, #
# modify, merge, publish, distribute, sublicense, and/or sell copies #
# of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be #
# included in all copies of the Software, its documentation and #
# marketing & publicity materials, and acknowledgment shall be given #
# in the documentation, materials and software packages that this #
# Software was used. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, #
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF #
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND #
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY #
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF #
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION #
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #
#######################################################################
# load required libraries
require 'cgi'
require 'uri'
require 'time'
require 'net/http'
require 'rexml/document'
class String
#
# Escape XML-special characters in string.
#
def xml_escape
str = gsub(/&/, '&')
str = str.gsub(/</, '<')
str = str.gsub(/>/, '>')
str = str.gsub(/"/, '"')
str
end
#
# XML escape elements, including spaces, ?, and +
#
def uri_escape
CGI::escape(self)
end
end
class Time
#
# Convert from an ISO-8601-format string to a time.
#
# Note: if there are more than one results in the string, this method
# matches the first one.
#
def Time::from_iso8601(str)
str.scan(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/) do
|yr, mo, dy, hr, mn, sc|
return Time::mktime(yr, mo, dy, hr, mn, sc)
end
end
#
# Convert time to an ISO-8601-format string.
#
def to_iso8601
strftime('%Y-%m-%dT%H:%M:%SZ')
end
end
class Array
#
# Convert an array of posts (bookmarks) to an XBEL string.
#
# Note: This method is significantly less taxing on Delicious than
# Rubilicious#to_xbel.
#
# Raises an exception on error.
#
# Example:
# results = r.recent # grab all recent posts
# File::open('output.xbel', 'w') do |file|
# file.puts results.to_xbel # save results to file
# end
#
def to_xbel(tag = nil)
ret = [ "<?xml version='1.0' encoding='utf-8'?>",
"<xbel version='1.0' added='#{Time::now.to_iso8601}'>",
# "<xbel version='1.0'>",
" <title>#{@user}'s del.icio.us bookmarks</title>" ]
# find all bookmarks in list with given tag and sort tag
tags = find_all { |e| !tag || e['tags'].include?(tag) }.inject({}) do |tags, bm|
if bm['tags'] && bm['tags'].size > 0
bm['tags'] = bm['tags'] ? bm['tags'].split(' ').sort : []
# TODO: alias support
bm['tags'].each { |tag| tags[tag] ||= []; tags[tag] << bm }
else
tags['uncategorized'] ||= []
tags['uncategorized'] << bm
end
tags
end
# print the folders out in order
tags.keys.sort.each do |tag|
ary = tags[tag]
ret << [
" <folder id='#{tag}' added='#{Time.now.to_iso8601}'>",
# " <folder id='#{tag}'>",
" <title>#{tag.capitalize}</title>",
ary.sort { |a, b| a['description'] <=> b['description'] }.map do |bm|
href, bm_id = bm['href'].uri_escape, "#{tag}-#{bm['hash']}",
time = bm['time'].to_iso8601
title = bm['description'] ? bm['description'].xml_escape : ''
desc = bm['extended'] ? bm['extended'].xml_escape : ''
[ " <bookmark href='#{href}' id='#{bm_id}' added='#{time}'>",
# [ " <bookmark href='#{href}' id='#{bm_id}'>",
" <title>#{title}</title>",
" <desc>#{desc}</desc>",
" </bookmark>" ,
].join("\n")
end.join("\n"),
' </folder>',
].join("\n")
end
# attach closing tag and return string
ret << '</xbel>'
ret.join("\n")
end
end
#
# Rubilicious - Delicious (http://del.icio.us/) bindings for Ruby.
#
# You'll need to create an account at Delicious (http://del.icio.us/) in
# order to use this API.
#
# Simple Examples:
# # connect to delicious and get a list of your recent posts
# r = Rubilicious.new('user', 'password')
# r.recent.each do |post|
# puts "#{post['desc']}: #{post['href']}"
# end
#
# # add a new link to delicious
# r.add('http://pablotron.org/', 'Pablotron.org')
#
# # save recent funny posts to an XBEL file
# File::open('funny_links.xbel', 'w') do |file|
# file.puts r.recent('funny').to_xbel
# end
#
class Rubilicious
attr_reader :user
attr_accessor :use_proxy, :base_uri
VERSION = '0.1.4'
#
# get the HTTP proxy server and port from the environment
# Returns [nil, nil] if a proxy is not set
#
# This method is private
#
def find_http_proxy
ret = [nil, nil]
# check the platform. If we're running in windows then we need to
# check the registry
if @use_proxy
if RUBY_PLATFORM =~ /win32/i
# Find a proxy in Windows by checking the registry.
# this code shamelessly copied from Raggle :D
require 'win32/registry'
Win32::Registry::open(
Win32::Registry::HKEY_CURRENT_USER,
'Software\Microsoft\Windows\CurrentVersion\Internet Settings'
) do |reg|
# check and see if proxy is enabled
if reg.read('ProxyEnable')[1] != 0
# get server, port, and no_proxy (overrides)
server = reg.read('ProxyServer')[1]
np = reg.read('ProxyOverride')[1]
server =~ /^([^:]+):(.+)$/
ret = [$1, $2]
# don't bother with no_proxy support
# ret['no_proxy'] = np.gsub(/;/, ',') if np && np.length > 0
end
end
ret
else
# handle UNIX systems
ret = ENV['http_proxy'].sub('http://','').split(':') if ENV['http_proxy']
end
end
# return host and port
ret
end
#
# Low-level HTTP GET.
#
# This method is private.
#
def http_get(url)
# get proxy info
proxy_host, proxy_port = find_http_proxy
# get host, port, and base URI for API queries
uri = URI::parse(@base_uri)
base = uri.request_uri
# prepend base to url
url = "#{base}/#{url}"
# connect to delicious
http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port)
http.start
# get URL, check for error
resp = http.get(url, @headers)
raise "HTTP #{resp.code}: #{resp.message}" unless resp.code =~ /2\d{2}/
# close HTTP connection, return response
http.finish
resp.body
end
#
# Get url from del.icio.us, and optionally parse result and return as
# an array of hashes as well.
#
# This method is private.
#
def get(url, elem = nil)
# check last request time, if it was too recent, then wait
sleep 1.0 if @last_request && (Time.now.to_i - @last_request) < 1
@last_request = Time.now.to_i
# get result and parse it
ret = REXML::Document.new(http_get(url))
# if we got something, then parse it
if elem
ary = []
ret.root.elements.each("//#{elem}") do |e|
hash = {}
e.attributes.each { |key, val| hash[key] = val }
ary << hash
end
ret = ary
end
# return result
ret
end
# don't touch these :)
private :get, :http_get, :find_http_proxy
#
# Connect to del.icio.us with username 'user' and password 'pass'.
#
# Note: if the username or password is incorrect, Rubilicious will not
# raise an exception until you make an actual call.
#
# Example:
# r = Rubilicious.new('pabs', 'password')
#
def initialize(user, pass)
@user, @use_proxy = user, true
@base_uri = ENV['RUBILICIOUS_BASE_URI'] || 'http://del.icio.us/api'
@headers = {
'Authorization' => 'Basic ' << ["#{user}:#{pass}"].pack('m').strip,
'User-Agent' => "Rubilicious/#{Rubilicious::VERSION} Ruby/#{RUBY_VERSION}"
}
end
#
# Returns a list of dates with the number of posts at each date. If a
# tag is given, return a list of dates with the number of posts with
# the specified tag at each date.
#
# Raises an exception on error.
#
# Examples:
# dates = r.dates
# puts "date,count"
# dates.keys.sort.each do |date|
# puts "#{date},#{dates[date]}"
# end
#
# # same as above, but only display 'politics' tags
# dates = r.dates('politics')
# puts "date,count",
# dates.map { |args| args.join(',') }.join("\n")
#
def dates(tag = nil)
get('posts/dates?' << (tag ? "tag=#{tag}" : ''), 'date').inject({}) do |ret, e|
ret[e['date']] = e['count'].to_i
ret
end
end
#
# Returns a hash of tags and the number of times they've been used.
#
# Raises an exception on error.
#
# Example:
# tags = r.tags
# puts tags.keys.sort.map { |tag| "#{tag},#{tags[tag]}" }.join("\n")
#
def tags
get('tags/get?', 'tag').inject({}) do |ret, e|
ret[e['tag']] = e['count'].to_i
ret
end
end
#
# Returns an array of posts on a given date, filtered by tag. If no
# date is supplied, most recent date will be used.
#
# Raises an exception on error.
#
# Examples:
# # print out a list of recent links from oldest to newest.
# posts = r.posts
# posts.sort { |a, b| a['time'] <=> b['time'] }.each do |post|
# puts post['href']
# end
#
# # print out a list of link descriptions from the date '2004-09-22'
# posts = r.posts('2004-09-22')
# posts.sort { |a, b| a['description'] <=> b['description'] }
# posts.each { |post| puts post['description'] }
#
def posts(date = nil, tag = nil)
args = [(date ? "dt=#{date}" : nil), (tag ? "tag=#{tag.uri_escape}" : nil)]
get('posts/get?' << args.compact.join('&'), 'post').map do |e|
e['tags'] = e['tag'].split(' ')
e.delete 'tag'
e['time'] = Time::from_iso8601(e['time'])
e
end
end
#
# Returns an array of the most recent posts, optionally filtered by tag.
#
# Raises an exception on error.
#
# Example:
# # get the most recent links
# recent_links = r.recent.map { |post| post['href'] }
#
# # get the 10 most recent 'music' links
# recent_links = r.recent('music', 10).map { |post| post['href'] }
#
def recent(tag = nil, count = nil)
args = [(count ? "count=#{count}" : nil), (tag ? "tag=#{tag.uri_escape}" : nil)]
get('posts/recent?' << args.compact.join('&'), 'post').map do |e|
e['tags'] = e['tag'].split(' ')
e.delete 'tag'
e['time'] = Time::from_iso8601(e['time'])
e
end
end
#
# Post a link to delicious, along with an optional extended
# description, tags (as a space-delimited list), and a timestamp.
#
# Raises an exception on error.
#
# Example:
# # add a link to pablotron to delicious
# r.add('http://pablotron.org/',
# 'Pablotron.org : The most popular site on Internet?')
#
# # add a link to paulduncan.org to delicious with an extended
# # description
# r.add('http://paulduncan.org/', "Paul Duncan", "Damn he's smooth!")
#
# # add a link with an extended description and some tags
# r.add('http://raggle.org/',
# 'Raggle', 'Console RSS Aggregator, written in Ruby.',
# 'rss programming ruby console xml')
#
def add(url, desc, ext = '', tags = '', time = Time.now)
raise "Missing URL" unless url
raise "Missing Description" unless desc
args = [
("url=#{url.uri_escape}"), ("description=#{desc.uri_escape}"),
(ext ? "extended=#{ext.uri_escape}" : nil),
(tags ? "tags=#{tags.uri_escape}" : nil), ("dt=#{time.to_iso8601}")
]
get('posts/add?' << args.compact.join('&'))
nil
end
#
# Delete a link from Delicious.
#
# Raises an exception on error.
#
# Example:
# # delete a link to example.com from delicious
# r.delete('http://example.com/')
#
def delete(url)
raise "Missing URL" unless url
get('posts/delete?' << url.escape_uri)
nil
end
#
# Renames tags across all posts.
#
# Note: Delicious has currently disabled this feature, so it will not
# work until they reenable it.
#
# Raises an exception on error.
#
# Example:
# # rename tag "rss" to "xml"
# r.rename('rss', 'xml')
#
def rename(old, new)
args = ["old=#{old.uri_escape}", "new=#{new.uri_escape}"]
get('tags/rename?' << args.join('&'))
nil
end
#
# Returns a array of inbox entries, optionally filtered by date.
#
# Raises an exception on error.
#
# Example:
# # print a list of posts and who posted them
# r.inbox.each { |post| puts "#{post['user']},#{post['href']}" }
#
def inbox(date = nil)
time_prefix = "#{date || Time.now.strftime('%Y-%m-%d')}T"
ret = get('inbox/get?' << (date ? "dt=#{date}" : ''), 'post').map do |post|
post['time'] = Time::from_iso8601("#{time_prefix}#{post['time']}Z")
post
end
ret
end
#
# Returns a hash of dates containing inbox entries.
#
# Raises an exception on error.
#
# Example:
# # print out a list of the 10 busiest inbox dates
# dates = r.inbox_dates
# puts dates.keys.sort { |a, b| dates[b] <=> dates[a] }.slice(0, 10)
#
def inbox_dates
get('inbox/dates?', 'date').inject({}) do |ret, e|
ret[e['date']] = e['count'].to_i
ret
end
end
#
# Returns a hash of your subscriptions.
#
# Raises an exception on error.
#
# Example:
# # print out a list of subscriptions
# subs = r.subs
# puts "user:tags"
# subs.keys.sort.each do |sub|
# puts "#{sub}:#{subs[sub].join(' ')}"
# end
#
def subs
get('inbox/subs?', 'sub').inject({}) do |ret, e|
ret[e['user']] = [] unless ret[e['user']]
ret[e['user']] += e['tag'].split(' ')
ret
end
end
#
# Add a subscription, optionally to a specific tag.
#
# Raises an exception on error.
#
# Example:
# # subscribe to 'humor' links from solarce
# r.sub('solarce', 'humor')
#
def sub(user, tag = nil)
raise "Missing user" unless user
args = ["user=#{user.uri_escape}", (tag ? "tag=#{tag.uri_escape}" : nil)]
get('inbox/sub?' << args.compact.join('&'), 'post')
nil
end
#
# Removes a subscription, optionally only a specific tag.
#
# Raises an exception on error.
#
# Example:
# # unsubscribe from all links from giblet
# r.unsub('giblet')
#
def unsub(user, tag = nil)
raise "Missing user" unless user
args = ["user=#{user}", (tag ? "tag=#{tag}" : nil)]
get('inbox/unsub?' << args.compact.join('&'))
nil
end
#
# Return the last update time.
#
# Note: this method should be used before calling methods like .posts
# or .all to conserve on bandwidth.
#
# Example:
# t = r.update #=> "Fri Mar 11 02:45:51 EST 2005"
#
def update
Time::xmlschema(get('posts/update', 'update')[0]['time'])
end
#
# Return an array of all your posts ever, optionally filtered by tag.
#
#
# WARNING: This method can generate a large request to del.icio.us,
# and should be used sparingly, and at your own risk.
#
# Raises an exception on error.
#
# Example:
# # save all 'art' posts to file "art_posts.txt"
# art_posts = r.all('art')
# File::open('art_posts.txt', 'w') do |file|
# file.puts art_posts.sort do |a, b|
# a['time'] <=> b['time']
# end.map { |post| post['href'] }
# end
#
def all(tag = nil)
args = [(tag ? "tag=#{tag.uri_escape}" : nil)]
get('posts/all?' << args.compact.join('&'), 'tag')
end
#
# Return an XBEL string of all your posts, optionally filtered by tag.
#
# WARNING: This method can generate a large number of requests to
# del.icio.us, and could be construed as abuse. Use sparingly, and at
# your own risk.
#
# Raises an exception on error.
#
# Example:
# # save all posts ever in XBEL format to file "delicious.xbel"
# File::open('delicious.xbel', 'w') do |file|
# file.puts r.to_xbel
# end
#
def to_xbel(tag = nil)
ret = [ "<?xml version='1.0' encoding='utf-8'?>",
"<xbel version='1.0' added='#{Time::now.to_iso8601}'>",
# "<xbel version='1.0'>",
" <title>#{@user}'s del.icio.us bookmarks</title>" ]
tags = all(tag).inject({}) do |tags, bm|
if bm['tags'] && bm['tags'].size > 0
bm['tags'].sort!
# TODO: alias support
bm['tags'].each { |tag| tags[tag] ||= []; tags[tag] << bm }
else
tags['unsorted'] ||= []
tags['unsorted'] << bm
end
tags
end
tags.keys.sort.each do |tag|
ary = tags[tag]
ret << [
" <folder id='#{tag}' added='#{Time.now.to_iso8601}'>",
# " <folder id='#{tag}'>",
" <title>#{tag.capitalize}</title>",
ary.sort { |a, b| a['description'] <=> b['description'] }.map do |bm|
href, bm_id = bm['href'].uri_escape, "#{tag}-#{bm['hash']}",
time = bm['time'].to_iso8601
title = bm['description'] ? bm['description'].xml_escape : ''
desc = bm['extended'] ? bm['extended'].xml_escape : ''
[ " <bookmark href='#{href}' id='#{bm_id}' added='#{time}'>",
# [ " <bookmark href='#{href}' id='#{bm_id}'>",
" <title>#{title}</title>",
" <desc>#{desc}</desc>",
" </bookmark>" ,
].join("\n")
end.join("\n"),
' </folder>',
].join("\n")
end
ret << '</xbel>'
ret.join("\n")
end
#
# Return all of a user's posts, optionally filtered by tag.
#
# WARNING: This method can generate a large number of requests to
# del.icio.us, and could be construed as abuse. Use sparingly, and at
# your own risk.
#
# Raises an exception on error.
#
# Example:
# # save all posts every by 'delineator' to XBEL format to file
# # "delineator.xbel"
# File::open('delineator.xbel', 'w') do |file|
# file.puts r.user_posts('delineator').to_xbel
# end
#
def user_posts(user, tag = nil)
was_subscribed = true
ret = []
# unless we already subscribed, subscribe to user
unless subs.keys.include?(user)
sub(user)
was_subscribed = false
end
# grab list of user's posts
inbox_dates.keys.each do |date|
ret += inbox(date).find_all do |post|
post['user'] == user && (tag == nil || post['tags'].include?(tag))
end
end
# unsubscribe from user unless we were already subscribed
unsub(user) unless was_subscribed
# return list of user's posts
ret
end
# convenience aliases
alias :rename_tag :rename
alias :subscriptions :subs
alias :subscribe :sub
alias :unsubscribe :unsub
alias :all_posts :all
end
|