Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created September 24, 2012 00:09

Revisions

  1. ttscoff created this gist Sep 24, 2012.
    68 changes: 68 additions & 0 deletions allpinboard.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #!/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
    }