Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding files

  • Loading branch information...
commit 737da02727c2f9421e573c54ecc93681c09994fe 1 parent 9b86d31
@lodestone authored
View
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
View
1  .rvmrc
@@ -0,0 +1 @@
+rvm gemset use kindle
View
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in kindle.gemspec
+gemspec
View
2  Rakefile
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
View
22 bin/kindle
@@ -0,0 +1,22 @@
+#!/usr/bin/env ruby
+
+lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
+$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
+
+require 'kindle'
+
+unless login = ARGV[0]
+ print "Enter your Amazon login:"
+ login = gets
+end
+print "Enter your password for Amazon (We don't store this)"
+passwd = gets
+
+begin
+ k = Kindle::Kindle.new(:login => login, :password => passwd)
+ puts "Getting your kindle highlights..."
+ highlights = k.get_kindle_highlights
+ puts highlights
+rescue => ex
+ puts "Crud, something went wrong..."
+end
View
23 kindle.gemspec
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "kindle/version"
+
+Gem::Specification.new do |s|
+ s.name = "kindle"
+ s.version = Kindle::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Matt Petty"]
+ s.email = ["matt@kizmeta.com"]
+ s.homepage = ""
+ s.summary = %q{Manage your kindle highlights with ruby}
+ s.description = %q{Manage your kindle highlights with ruby}
+
+ s.rubyforge_project = "kindle"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+ s.add_dependency "nokogiri"
+ s.add_dependency "mechanize"
+end
View
95 lib/kindle.rb
@@ -0,0 +1,95 @@
+require 'nokogiri'
+require 'mechanize'
+
+module Kindle
+
+ class Highlight
+ attr_reader :highlight, :asin
+ def initialize(highlight, asin)
+ @highlight, @asin = highlight, asin
+ end
+ end
+
+ class Kindle
+ include Nokogiri
+
+ KINDLE_URL = 'http://kindle.amazon.com'
+
+ attr_reader :agent, :current_page, :asins, :highlights
+
+ def initialize(options = {:login => nil, :password => nil})
+ @highlights = []
+ @current_offset = 25
+ @current_highlights = 1
+ @current_upcoming = []
+ options.each_pair do |k,v|
+ instance_variable_set("@#{k}", v)
+ end
+ @agent = Mechanize.new
+ @agent.redirect_ok = true
+ @agent.user_agent_alias = 'Windows IE 7'
+ end
+
+ def first_page
+ @agent.get(KINDLE_URL)
+ end
+
+ def login(page=first_page)
+ lp = page.link_with(:text => "Sign in").click
+ lp.forms.first.email = @login
+ lp.forms.first.password = @password
+ @current_page = lp.forms.first.submit
+ @current_page
+ end
+
+ def fetch_highlights
+ @asins = []
+ @current_page = @current_page.link_with(:text => 'Your Highlights').click
+ extract_highlights(@current_page)
+ until @current_highlights.length == 0 do
+ next_highlights
+ end
+ end
+
+ def extract_highlights(page)
+ @current_highlights = hls = (page/".yourHighlight")
+ asins = (page/".asin").collect{|asin| asin.text}
+ if hls.length > 0
+ @current_upcoming = (page/".upcoming").first.text.split(',') rescue []
+ @current_offset = ((current_page/".yourHighlightsHeader").collect{|h| h.attributes['id'].value }).first.split('_').last
+ (page/".yourHighlight").each do |hl|
+ highlight = parse_highlight(hl)
+ @highlights << highlight
+ if !@asins.include?(highlight.asin)
+ @asins << highlight.asin unless @asins.include?(highlight.asin)
+ end
+ end
+ end
+ end
+
+ def next_highlights
+ asins_string = @asins.map{|l| "used_asins[]=#{l}" } * '&'
+ upcoming_string = @current_upcoming.map{|l| "upcoming_asins[]=#{l}" } * '&'
+ current_offset = @current_offset
+ url = "https://kindle.amazon.com/your_highlights/next_book?#{asins_string}&current_offset=#{@current_offset}&#{upcoming_string}"
+ @current_page = @agent.get(url)
+ extract_highlights(@current_page)
+ @current_page
+ end
+
+ def parse_highlight(hl)
+ highlight = (hl/".highlight").text
+ asin = (hl/".asin").text
+ Highlight.new(highlight, asin)
+ end
+
+ def get_kindle_highlights
+ login
+ fetch_highlights
+ return highlights.map(&:highlight)
+ end
+
+ end
+
+
+end
View
3  lib/kindle/version.rb
@@ -0,0 +1,3 @@
+module Kindle
+ VERSION = "0.0.1"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.