kindle 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm gemset use kindle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in kindle.gemspec
4
+ gemspec
data/README ADDED
File without changes
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
+
6
+ require 'kindle'
7
+ require 'highline/import'
8
+
9
+
10
+ unless login = ARGV[0]
11
+ login = ask("Enter your username: ") { |q| q.echo = true }
12
+ end
13
+ passwd = ask("Enter your password (This is not stored): ") { |q| q.echo = "*" }
14
+
15
+ begin
16
+ k = Kindle::Kindle.new(:login => login, :password => passwd)
17
+ puts "Getting your kindle highlights..."
18
+ highlights = k.get_kindle_highlights
19
+ puts highlights
20
+ rescue => ex
21
+ puts "Crud, something went wrong..."
22
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "kindle/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "kindle"
7
+ s.version = Kindle::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Matt Petty"]
10
+ s.email = ["matt@kizmeta.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Manage your kindle highlights with ruby}
13
+ s.description = %q{Manage your kindle highlights with ruby}
14
+
15
+ s.rubyforge_project = "kindle"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ s.add_dependency "nokogiri"
22
+ s.add_dependency "highline"
23
+ s.add_dependency "mechanize"
24
+ end
@@ -0,0 +1,95 @@
1
+ require 'nokogiri'
2
+ require 'mechanize'
3
+
4
+ module Kindle
5
+
6
+ class Highlight
7
+ attr_reader :highlight, :asin
8
+ def initialize(highlight, asin)
9
+ @highlight, @asin = highlight, asin
10
+ end
11
+ end
12
+
13
+ class Kindle
14
+ include Nokogiri
15
+
16
+ KINDLE_URL = 'http://kindle.amazon.com'
17
+
18
+ attr_reader :agent, :current_page, :asins, :highlights
19
+
20
+ def initialize(options = {:login => nil, :password => nil})
21
+ @highlights = []
22
+ @current_offset = 25
23
+ @current_highlights = 1
24
+ @current_upcoming = []
25
+ options.each_pair do |k,v|
26
+ instance_variable_set("@#{k}", v)
27
+ end
28
+ @agent = Mechanize.new
29
+ @agent.redirect_ok = true
30
+ @agent.user_agent_alias = 'Windows IE 7'
31
+ end
32
+
33
+ def first_page
34
+ @current_page = @agent.get(KINDLE_URL)
35
+ end
36
+
37
+ def login(page=first_page)
38
+ lp = page.link_with(:text => "Sign in").click
39
+ lp.forms.first.email = @login
40
+ lp.forms.first.password = @password
41
+ @current_page = lp.forms.first.submit
42
+ @current_page.forms.first.submit
43
+ end
44
+
45
+ def fetch_highlights
46
+ @asins = []
47
+ @current_page = @current_page.link_with(:text => 'Your Highlights').click
48
+ extract_highlights(@current_page)
49
+ until @current_highlights.length == 0 do
50
+ next_highlights
51
+ end
52
+ end
53
+
54
+ def extract_highlights(page)
55
+ @current_highlights = hls = (page/".yourHighlight")
56
+ asins = (page/".asin").collect{|asin| asin.text}
57
+ if hls.length > 0
58
+ @current_upcoming = (page/".upcoming").first.text.split(',') rescue []
59
+ @current_offset = ((current_page/".yourHighlightsHeader").collect{|h| h.attributes['id'].value }).first.split('_').last
60
+ (page/".yourHighlight").each do |hl|
61
+ highlight = parse_highlight(hl)
62
+ @highlights << highlight
63
+ if !@asins.include?(highlight.asin)
64
+ @asins << highlight.asin unless @asins.include?(highlight.asin)
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ def next_highlights
71
+ asins_string = @asins.map{|l| "used_asins[]=#{l}" } * '&'
72
+ upcoming_string = @current_upcoming.map{|l| "upcoming_asins[]=#{l}" } * '&'
73
+ current_offset = @current_offset
74
+ url = "https://kindle.amazon.com/your_highlights/next_book?#{asins_string}&current_offset=#{@current_offset}&#{upcoming_string}"
75
+ @current_page = @agent.get(url)
76
+ extract_highlights(@current_page)
77
+ @current_page
78
+ end
79
+
80
+ def parse_highlight(hl)
81
+ highlight = (hl/".highlight").text
82
+ asin = (hl/".asin").text
83
+ Highlight.new(highlight, asin)
84
+ end
85
+
86
+ def get_kindle_highlights
87
+ login
88
+ fetch_highlights
89
+ return highlights.map(&:highlight)
90
+ end
91
+
92
+ end
93
+
94
+
95
+ end
@@ -0,0 +1,3 @@
1
+ module Kindle
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kindle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Petty
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: highline
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mechanize
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Manage your kindle highlights with ruby
63
+ email:
64
+ - matt@kizmeta.com
65
+ executables:
66
+ - kindle
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - .rvmrc
72
+ - Gemfile
73
+ - README
74
+ - Rakefile
75
+ - bin/kindle
76
+ - kindle.gemspec
77
+ - lib/kindle.rb
78
+ - lib/kindle/version.rb
79
+ homepage: ''
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project: kindle
99
+ rubygems_version: 1.8.23
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Manage your kindle highlights with ruby
103
+ test_files: []