Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

del.icio.us backup (See related posts)

This ruby script dumps all your del.icio.us bookmarks into a sqlite database. Requires sqlite3-ruby installed via rubygems. Change the lines
require 'rubygems'
require_gem 'sqlite3-ruby'
if you have a different setup.
Don't forget to change the user and pass.
#!/usr/bin/env ruby
require 'rexml/document'
require 'net/http'
require 'rubygems'
require_gem 'sqlite3-ruby'

# change credentials!
user = 'del.icio.us username'
pass = 'del.icio.us password'

# User-Agent: required for del.icio.us api
agent = 'delicious-backup.rb v0.1'
schema = <<EOF
create table bookmarks (
    hash char(32) primary key,
    url varchar(1024),
    title varchar(1024),
    note varchar(2048),
    time timestamp
);
create table tags (hash char(32), tag varchar(1024));
create index ix_tags_hash on tags (hash);
create index ix_tags_tag on tags (tag);
EOF
insert_url = 'insert into bookmarks (hash, url, title, note, time) ' +
             'values (?, ?, ?, ?, ?);'
insert_tag = 'insert into tags (hash, tag) values (?, ?);'

xml = Net::HTTP.start('del.icio.us') { |http|
    req = Net::HTTP::Get.new('/api/posts/all', {'User-Agent' => agent})
    req.basic_auth(user, pass)
    http.request(req).body
}

db_name = ARGV[0] || Time.now.strftime("%Y-%m-%d.db")
SQLite3::Database.open(db_name).transaction { |db|
    db.execute_batch(schema)
    db.prepare(insert_url) { |url_stmt|
        db.prepare(insert_tag) { |tag_stmt|
            REXML::Document.new(xml).elements.each('posts/post') { |el|
                url_stmt.execute(el.attributes['hash'],
                    el.attributes['href'], el.attributes['description'],
                    el.attributes['extended'], el.attributes['time'])
                el.attributes['tag'].split(' ').each { |tag|
                    tag_stmt.execute(el.attributes['hash'], tag)
                }
            }
        }
    }
}

You need to create an account or log in to post comments to this site.


Click here to browse all 4829 code snippets

Related Posts