Skip to content

Instantly share code, notes, and snippets.

@swdyh
Created April 24, 2013 20:25
Show Gist options
  • Save swdyh/5455259 to your computer and use it in GitHub Desktop.
Save swdyh/5455259 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'cgi'
require 'json'
require 'open-uri'
require 'pathname'
def get url
p ['get', url]
r = OpenURI.open_uri(url).read
sleep 1
r
end
def export user
base = 'https://avosapi.delicious.com/api/v1/posts'
a = nil
lim = 1000
r = []
count = 0
lim.times do
url = a ? "#{base}/public/#{user}/time?limit=20&anchor=#{a}&index=0&inclpriv=1&bundle_name=null" : "#{base}/public/#{user}/time"
res = get url
v = JSON.parse res
if v['error']
p ['error', v['error']]
exit 1
end
if !v['pkg']
break
end
count += v['pkg'].size
v['pkg'].each { |i| r << i }
a = v['pkg'].last['time_created'] - 1
p ['count', count, a]
end
out = "delicious_export.html"
open(out, 'w') { |f| f.write bookmarks_html(r) }
p ['ok', out]
end
def bookmarks_html bs
list = bs.map do |i|
tags = i['tags'] ? i['tags'].join(',') : ''
d = [i['url'], i['time_created'].to_s, tags, i['title']].map { |j| CGI::escapeHTML j }
s = %(<DT><A HREF="%s" ADD_DATE="%s" PRIVATE="0" TAGS="%s">%s</A>\n) % d
if i['note']
s << (('<DD>%s' % CGI::escapeHTML(i['note'])) + "\n")
end
s
end.join
t = <<-EOS
<!DOCTYPE NETSCAPE-Bookmark-file-1>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<!-- This is an automatically generated file.
It will be read and overwritten.
Do Not Edit! -->
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
#{list}
</DL><p>
EOS
end
if ARGV.first
export ARGV.first
else
puts 'Usage: ruby delicious_export.rb username'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment