Skip to content

Instantly share code, notes, and snippets.

@lavilet
Created May 11, 2014 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lavilet/38a3845a4fd75d3758ca to your computer and use it in GitHub Desktop.
Save lavilet/38a3845a4fd75d3758ca to your computer and use it in GitHub Desktop.
Import your chrome bookmarks to a database via ActiveRecord. It tags each bookmark automatically by scraping keywords from a webpage. Need to add threads.
require 'rubygems'
require 'active_record'
require 'markio'
require 'open-uri'
require 'nokogiri'
require 'socket'
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "bookmarks_imported.db"
)
ActiveRecord::Schema.define do
create_table :bookmarks do |table|
table.column :title, :string
table.column :link, :string
end
create_table :tags do |table|
table.column :tag, :string
end
create_table :bookmarks_tags do |table|
table.integer :bookmark_id
table.integer :tag_id
end
end
class Bookmark < ActiveRecord::Base
has_and_belongs_to_many :tags
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :bookmarks
end
#
@bookmarks_array = []
bookmarks = Markio::parse(File.open('bookmarks.html'))
bookmarks.each do |bookmark|
@bookmarks_array << bookmark
end
@bookmarks_array.each do |bookmark|
begin
doc = Nokogiri::HTML(open(bookmark.href))
keywords = ["keywords", "KEYWORDS", "Keywords"]
keywords.each do |keyword|
if doc.xpath("//meta[@name=#{keyword}]/@content").nil?
next
else
doc.xpath("//meta[@name='#{keyword}']/@content").each do |attr|
bookmark_ar = Bookmark.create(:title => bookmark.title,
:link => bookmark.href)
tags = attr.value.split(',')
tags.each do |t|
bookmark_ar.tags = Tag.where(:tag => t.strip).first_or_create
end
end
end
end
rescue
puts "Error while processing link."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment