Skip to content

Instantly share code, notes, and snippets.

@dlo
Forked from ttscoff/allpinboard.rb
Last active September 5, 2019 15:26
Show Gist options
  • Save dlo/4629073 to your computer and use it in GitHub Desktop.
Save dlo/4629073 to your computer and use it in GitHub Desktop.
Python version of https://gist.github.com/3773519 that pulls all bookmarks on the first sync, and does incremental updates afterwards. Also uses the Mac OS X keychain to retrieve your password so it doesn't need to live in a file on your computer in plain text.
#!/usr/bin/ruby
=begin
This script is designed to generate a simple html file with _all_ of your Pinboard.in bookmarks
The HTML file can be added to Launchbar's index as a custom bookmark file and you can search
your entire Pinboard.in collection instantly from Launchbar (by title only). It includes
any applied tags as part of the title to aid in searching.
This does no checking for deltas, it just grabs the whole bunch and overwrites the html file
every time. Don't run it too frequently.
Set your username, password and the path/filename to write to below.
=end
### User configuration
PB_USERNAME = 'username'
PB_PASSWORD = 'password'
BOOKMARK_FILE = 'path/to/store/PinboardBookmarks.html'
### END config
require 'cgi'
require 'fileutils'
require 'net/https'
require 'rexml/document'
class Net::HTTP
alias_method :old_initialize, :initialize
def initialize(*args)
old_initialize(*args)
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
end
def get_bookmarks
xml = ''
http = Net::HTTP.new('api.pinboard.in', 443)
http.use_ssl = true
http.start do |http|
request = Net::HTTP::Get.new('/v1/posts/all')
request.basic_auth PB_USERNAME,PB_PASSWORD
response = http.request(request)
response.value
xml = response.body
end
return REXML::Document.new(xml)
end
def bookmarks_to_array(doc)
bookmarks = []
doc.elements.each('posts/post') do |ele|
post = {}
ele.attributes.each {|key,val|
post[key] = val;
}
bookmarks.push(post)
end
return bookmarks
end
bookmarks = bookmarks_to_array(get_bookmarks)
output = ""
bookmarks.each do |b|
output += %Q{<a href="#{b['href']}" title="#{CGI.escapeHTML(b['extended']).gsub(/\n/,' ')}">#{b['description'].gsub(/\n+/,"\n")} @#{b['tag'].split(' ').join(' @')}</a>\n}
end
# Append to a file:
open(File.expand_path(BOOKMARK_FILE), 'w') { |f|
f.puts output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment