Created
January 30, 2011 21:40
-
-
Save rbxbx/803278 to your computer and use it in GitHub Desktop.
Import and mirror your http://pinboard.in bookmarks because you're SCARED.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'sinatra' | |
| require 'nokogiri' | |
| require 'open-uri' | |
| require 'haml' | |
| require 'sequel' | |
| require 'sqlite3' | |
| get '/' do | |
| redirect '/update' unless bookmarks_table_exists? | |
| haml :index, :locals => { bookmarks: bookmarks_table } | |
| end | |
| get '/update' do | |
| create_bookmarks_table unless bookmarks_table_exists? | |
| bookmarks_table.delete and persist(bookmarks, bookmarks_table) | |
| redirect '/' | |
| end | |
| def export | |
| @export ||= Nokogiri::HTML(open('https://pinboard.in/export/', http_basic_authentication: ['username', 'password'])) | |
| end | |
| def bookmarks | |
| @bookmarks ||= Bookmark.extract_from export.xpath('//a') | |
| end | |
| def persist(entries, table) | |
| entries.each do |bm| | |
| if bookmark = bm.to_hash | |
| table.insert bookmark | |
| end | |
| end | |
| end | |
| def create_bookmarks_table | |
| database.create_table :bookmarks do | |
| primary_key :id | |
| String :title | |
| String :tags | |
| String :href | |
| end | |
| end | |
| def bookmarks_table | |
| database[:bookmarks] | |
| end | |
| def bookmarks_table_exists? | |
| database.tables.include? :bookmarks | |
| end | |
| def database | |
| @database ||= (Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://pinboarder.db')) | |
| end | |
| template(:index) do | |
| <<-haml | |
| !!! | |
| %title Pinboarder - hoard those pinboard.in bookmarx! | |
| %body | |
| %div.content | |
| %h1 Pinboarder! | |
| %ul | |
| - bookmarks.each do |bm| | |
| %li | |
| %a{:href => bm[:href]}= bm[:title] | |
| tags: | |
| = bm[:tags] | |
| haml | |
| end | |
| class Bookmark | |
| def initialize(noko_node) | |
| @noko_node = noko_node | |
| end | |
| def self.extract_from(export) | |
| export.map { |node| new(node) } | |
| end | |
| def title | |
| @noko_node.children.map(&:inner_text).join(', ') | |
| end | |
| def to_hash | |
| { title: title, href: href, tags: tags } | |
| end | |
| def method_missing(meth, *args) | |
| @noko_node.attr(meth.to_sym) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment