kindle 0.0.3 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1aab8778c3a0be8f8dcdc26f08a8a38b65bfb7e0
4
- data.tar.gz: ebc53336f7b9b95e94868fe43b0a097664450dd4
3
+ metadata.gz: a15b6f219ad45232674d51107a9b19c6f7012e67
4
+ data.tar.gz: ec18a3312046a01027bf3b2dc57a595d7638b613
5
5
  SHA512:
6
- metadata.gz: d2836557deb6e61cebac2ebcaa06cab72d5c9544627b57c5db9b1015aac428c2c88c0a754ead347f7005635f713c7cc7d9d21c209eae8e3a93ca2c95d98b921e
7
- data.tar.gz: 41f54e665092e900d815e7fde9a78d6e917ba50359b86fcc45da9a06af4dc66e35c3287fb91f74db1eb3dca74219be4a3bb7cfc46bbf8292f67ec3671c970bae
6
+ metadata.gz: 7f4471c2183ccb818046f1fc84dbee45676c256f214a4d2a6b46a295cefbb56f9bb743e34b6f7a3cc1c99dc55111fe45933e75f3eb58fb94edaf5f68e52e9705
7
+ data.tar.gz: d6316c16e858c0b60677d8cda16a969e600ff220a84b949a99f7a2d08efbc18dc35c09ee73ab4ed45d70d673d9be305beb351622d43ab4e9b8e97ea600ceb533
data/Changelog CHANGED
@@ -1,3 +1,8 @@
1
+ # Version 0.1.0
2
+
3
+ * Confirm the script still works as of 2014-04-10
4
+ * Make the default command line output semi-colon delimited
5
+
1
6
  # Version 0.0.3
2
7
 
3
8
  * Update headers in requests to get pages of highlights
data/bin/kindle CHANGED
@@ -1,22 +1,21 @@
1
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
2
 
6
- require 'kindle'
3
+ require_relative "../lib/kindle"
7
4
  require 'highline/import'
8
5
 
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 = "*" }
6
+ login = ask("Enter your Amazon.com username: ") { |q| q.echo = true } unless login = ARGV[0]
7
+ passwd = ask("Enter your Amazon.com password (This is not stored): ") { |q| q.echo = "*" }
14
8
 
15
9
  begin
16
- k = Kindle::Kindle.new(:login => login, :password => passwd)
10
+ k = Kindle::Highlights.new(:login => login, :password => passwd)
17
11
  puts "Getting your kindle highlights..."
18
- highlights = k.get_kindle_highlights
19
- puts highlights
12
+ highlights = k.get_kindle_highlights # TODO: Pass in something to bide our time
13
+ # TODO: Multiple output formats. CSV, JSON, Pretty, HTML?
14
+ highlights.each do |highlight|
15
+ puts "#{highlight.asin};#{highlight.title};#{highlight.author};#{highlight.highlight}"
16
+ end
20
17
  rescue => ex
18
+ # TODO Actually handle this!
19
+ puts ex
21
20
  puts "Crud, something went wrong..."
22
21
  end
data/lib/kindle.rb CHANGED
@@ -1,96 +1,21 @@
1
1
  require 'nokogiri'
2
2
  require 'mechanize'
3
+ require_relative 'kindle/highlight'
4
+ require_relative 'kindle/reader'
3
5
 
4
6
  module Kindle
5
7
 
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'
8
+ class Highlights
17
9
 
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
10
+ def initialize(options = {})
11
+ options.each { |k,v| instance_variable_set("@#{k}", v) }
43
12
  end
44
13
 
45
14
  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
- ajax_headers = { 'X-Requested-With' => 'XMLHttpRequest', 'Host' => 'kindle.amazon.com' }
76
- @current_page = @agent.get(url,[],'https://kindle.amazon.com/your_highlight', ajax_headers)
77
- extract_highlights(@current_page)
78
- @current_page
79
- end
80
-
81
- def parse_highlight(hl)
82
- highlight = (hl/".highlight").text
83
- asin = (hl/".asin").text
84
- Highlight.new(highlight, asin)
85
- end
86
-
87
- def get_kindle_highlights
88
- login
89
- fetch_highlights
90
- return highlights.map(&:highlight)
15
+ parser = HighlightsParser.new(login: @login, password: @password)
16
+ parser.get_highlights
91
17
  end
92
18
 
93
19
  end
94
20
 
95
-
96
21
  end
@@ -0,0 +1,13 @@
1
+ module Kindle
2
+
3
+ class Highlight
4
+
5
+ attr_reader :highlight, :asin, :title, :author
6
+
7
+ def initialize(highlight, asin, title, author)
8
+ @highlight, @asin, @title, @author = highlight, asin, title, author
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,104 @@
1
+ module Kindle
2
+
3
+ class HighlightsParser
4
+
5
+ include Nokogiri
6
+
7
+ KINDLE_URL = 'http://kindle.amazon.com'
8
+
9
+ def initialize(options = {:login => nil, :password => nil})
10
+ options.each_pair { |k,v| instance_variable_set("@#{k}", v) }
11
+ end
12
+
13
+ def get_highlights(args)
14
+ state = {
15
+ current_offset: 25,
16
+ current_upcoming: []
17
+ }
18
+
19
+ page = login
20
+ fetch_highlights(page, state)
21
+ end
22
+
23
+ private
24
+
25
+ def agent
26
+ return @agent if @agent
27
+ @agent = Mechanize.new
28
+ @agent.redirect_ok = true
29
+ @agent.user_agent_alias = 'Windows IE 9'
30
+ @agent
31
+ end
32
+
33
+ def get_login_page
34
+ page = agent.get(KINDLE_URL)
35
+ page.link_with(:text => "Sign in").click
36
+ end
37
+
38
+ def login
39
+ login_form = get_login_page.forms.first
40
+ login_form.email = @login
41
+ login_form.password = @password
42
+
43
+ page = login_form.submit
44
+ page.forms.first.submit
45
+ end
46
+
47
+ def fetch_highlights(page, state)
48
+ page = get_the_first_highlight_page_from(page, state)
49
+
50
+ highlights = []
51
+
52
+ new_highlights = extract_highlights_from(page, state)
53
+
54
+ until new_highlights.length == 0 do
55
+ highlights << new_highlights
56
+ page = get_the_next_page(state, highlights.flatten)
57
+ new_highlights = extract_highlights_from(page, state)
58
+ end
59
+
60
+ highlights.flatten
61
+ end
62
+
63
+ def get_the_first_highlight_page_from(page, state)
64
+ page = page.link_with(:text => 'Your Highlights').click
65
+ initialize_state_with_page state, page
66
+ page
67
+ end
68
+
69
+ def extract_highlights_from(page, state)
70
+ return [] if (page/".yourHighlight").length == 0
71
+ (page/".yourHighlight").map { |hl| parse_highlight(hl, state) }
72
+ end
73
+
74
+ def initialize_state_with_page(state, page)
75
+ return if (page/".yourHighlight").length == 0
76
+ state[:current_upcoming] = (page/".upcoming").first.text.split(',') rescue []
77
+ state[:title] = (page/".yourHighlightsHeader .title").text.to_s.strip
78
+ state[:author] = (page/".yourHighlightsHeader .author").text.to_s.strip
79
+ state[:current_offset] = ((page/".yourHighlightsHeader").collect{|h| h.attributes['id'].value }).first.split('_').last
80
+ end
81
+
82
+ def get_the_next_page(state, previously_extracted_highlights = [])
83
+ asins = previously_extracted_highlights.map(&:asin).uniq
84
+ asins_string = asins.collect { |asin| "used_asins[]=#{asin}" } * '&'
85
+ upcoming_string = state[:current_upcoming].map { |l| "upcoming_asins[]=#{l}" } * '&'
86
+ url = "https://kindle.amazon.com/your_highlights/next_book?#{asins_string}&current_offset=#{state[:current_offset]}&#{upcoming_string}"
87
+ ajax_headers = { 'X-Requested-With' => 'XMLHttpRequest', 'Host' => 'kindle.amazon.com' }
88
+ page = agent.get(url,[],'https://kindle.amazon.com/your_highlight', ajax_headers)
89
+
90
+ initialize_state_with_page state, page
91
+
92
+ page
93
+ end
94
+
95
+ def parse_highlight(hl, state)
96
+ highlight = (hl/".highlight").text
97
+ asin = (hl/".asin").text
98
+ Highlight.new(highlight, asin, state[:title], state[:author])
99
+ end
100
+
101
+ end
102
+
103
+ end
104
+
@@ -1,3 +1,3 @@
1
1
  module Kindle
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kindle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Petty
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-27 00:00:00.000000000 Z
11
+ date: 2014-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: highline
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mechanize
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: Manage your kindle highlights with ruby
@@ -60,8 +60,8 @@ executables:
60
60
  extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
- - .gitignore
64
- - .rvmrc
63
+ - ".gitignore"
64
+ - ".rvmrc"
65
65
  - Changelog
66
66
  - Gemfile
67
67
  - LICENSE
@@ -70,6 +70,8 @@ files:
70
70
  - bin/kindle
71
71
  - kindle.gemspec
72
72
  - lib/kindle.rb
73
+ - lib/kindle/highlight.rb
74
+ - lib/kindle/highlights_parser.rb
73
75
  - lib/kindle/version.rb
74
76
  homepage: ''
75
77
  licenses: []
@@ -80,17 +82,17 @@ require_paths:
80
82
  - lib
81
83
  required_ruby_version: !ruby/object:Gem::Requirement
82
84
  requirements:
83
- - - '>='
85
+ - - ">="
84
86
  - !ruby/object:Gem::Version
85
87
  version: '0'
86
88
  required_rubygems_version: !ruby/object:Gem::Requirement
87
89
  requirements:
88
- - - '>='
90
+ - - ">="
89
91
  - !ruby/object:Gem::Version
90
92
  version: '0'
91
93
  requirements: []
92
94
  rubyforge_project: kindle
93
- rubygems_version: 2.0.2
95
+ rubygems_version: 2.2.1
94
96
  signing_key:
95
97
  specification_version: 4
96
98
  summary: Manage your kindle highlights with ruby