Skip to content

Instantly share code, notes, and snippets.

@dramalho
Created July 6, 2012 09:30
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 dramalho/3059207 to your computer and use it in GitHub Desktop.
Save dramalho/3059207 to your computer and use it in GitHub Desktop.
Kindle Clippings to MongoDB
require 'rubygems'
require 'kindleclippings'
require 'mongo'
require 'digest/md5'
parser = KindleClippings::Parser.new
clippings = parser.parse_file('./My Clippings.txt')
connection = Mongo::Connection.new("localhost")
db = connection.db("kindle")
coll = db.collection("clippings")
clippings.each do |clipping|
hash = {}
clipping.instance_variables.each do |var|
hash[var.to_s.delete("@")] = clipping.instance_variable_get(var).to_s
hash['checksum'] = Digest::MD5.hexdigest( clipping.book_title.to_s + clipping.content.to_s )
end
coll.insert( hash) unless coll.find_one({ :checksum => hash['checksum'] } )
end
@dramalho
Copy link
Author

Now with collision detection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment