Uproar.rb: Using Ruby and YAML to keep bookmarks
I use del.icio.us, but I don’t like the idea that I don’t have control of the server, so if something happens to my bookmarks, I’m screwed. I also want more universal integration of my bookmarks with all the browsers I used - from Lynx to Opera. So I wrote some Ruby.
delicious-to-yaml.rb
require 'rubygems' require 'hpricot' doc = open("links.html") { |f| Hpricot(f) } (doc/"a").each do |link| puts "---" puts "title: '#{link.inner_html.gsub(/'/, '\'\'')}'" puts "link: #{link.attributes['href']}" tags = link.attributes['tags'] ? link.attributes['tags'].gsub(/,/, ', ') : '' puts "tags: [#{tags}]" end
Go to https://secure.del.icio.us/settings/<your delicious username>/bookmarks/export - uncheck ‘include my notes’, but keep ‘include my tags’ checked. Save the result to links.html, then run the code above in the same directory as links.html. Pipe the output to another file, like Links.yaml. This is your new bookmark repository. Each link is a separate document, like the following:
--- title: 'I speak dog » Using GnuPG encyption with Mac OS X Mail' link: http://www.wasuvi.com/?page_id=2368 tags: [apple, howto, osx, gpg, security, email, toread] --- title: 'All recipes – complete resource for recipes and cooking tips' link: http://allrecipes.com/ tags: [recipes, howto, health, toread]
So on, so forth. This is obviously very human readable - you can simply add new links to the file by typing them in yourself. The next step is a file which asks for a tag and gives you the links that are tagged with it:
uproar.rb
require 'yaml' def set_option hash, name, default i = ARGV.index(name) raise "no argument specified" unless i hash[name] = default and return unless i raise "no argument specified: #{name}" if(ARGV.length <= i) hash[name] = ARGV[i + 1] end o = {} set_option o, '-t', nil raise "please specify a tag." if o['-t'] == nil links = File.open( 'Links.yaml' ) db = [] YAML::load_documents( links ) { |doc| db << doc } matches = [] db.each { |l| matches << l if l['tags'].include? o['-t'] } matches.each { |l| printf "%30s... - %s\n", l['title'][0..30], l['link'] }
It’s a little ugly right now, but it does what it’s supposed to do. That is Uproar.
Possible Improvements
- Open random link containing tag in web browser
- Add links to yaml file by command line
- Use the del.icio.us API to get links
- Use the del.icio.us API to sync links
- Use the Yaml API instead of just formatting strings