kindle-highlights 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,10 @@
1
- require 'rubygems'
2
- require 'mechanize'
3
- require 'amazon/aws'
4
- require 'amazon/aws/search'
5
- require 'kindle_highlights/kindle_highlight'
6
- require 'kindle_highlights/highlight'
7
-
8
- include Amazon::AWS
9
- include Amazon::AWS::Search
1
+ require 'rubygems'
2
+ require 'mechanize'
3
+ require 'json'
4
+ require 'kindle_highlights/client'
5
+
6
+ module KindleHighlights
7
+
8
+ KINDLE_LOGIN_PAGE = "http://kindle.amazon.com/login"
9
+ SIGNIN_FORM_IDENTIFIER = "signIn"
10
+ end
@@ -0,0 +1,50 @@
1
+ module KindleHighlights
2
+ class Client
3
+ attr_reader :books
4
+
5
+ def initialize(email_address, password)
6
+ @email_address = email_address
7
+ @password = password
8
+
9
+ setup_mechanize_agent
10
+ load_books_from_kindle_account
11
+ end
12
+
13
+ def highlights_for(asin)
14
+ highlights = @mechanize_agent.get("https://kindle.amazon.com/kcw/highlights?asin=#{asin}&cursor=0&count=1000")
15
+ json = JSON.parse(highlights.body)
16
+ json["items"]
17
+ end
18
+
19
+ private
20
+
21
+ def load_books_from_kindle_account
22
+ @books = Hash.new
23
+ signin_page = @mechanize_agent.get(KINDLE_LOGIN_PAGE)
24
+
25
+ signin_form = signin_page.form(SIGNIN_FORM_IDENTIFIER)
26
+ signin_form.email = @email_address
27
+ signin_form.password = @password
28
+
29
+ kindle_logged_in_page = @mechanize_agent.submit(signin_form)
30
+ highlights_page = @mechanize_agent.click(kindle_logged_in_page.link_with(:text => /Your Books/))
31
+
32
+ loop do
33
+ books = highlights_page.search(".//td[@class='titleAndAuthor']")
34
+ books.each do |book|
35
+ asin = book.search("a").first.attributes["href"].value.split("/").last
36
+ title = book.search("a").first.inner_html
37
+ @books[asin] = title
38
+ end
39
+ break if highlights_page.link_with(text: /Next/).nil?
40
+ highlights_page = @mechanize_agent.click(highlights_page.link_with(text: /Next/))
41
+ end
42
+ end
43
+
44
+ def setup_mechanize_agent
45
+ @mechanize_agent = Mechanize.new
46
+ @mechanize_agent.user_agent_alias = 'Windows Mozilla'
47
+ @mechanize_agent.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
48
+ end
49
+ end
50
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kindle-highlights
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-19 00:00:00.000000000 Z
12
+ date: 2013-08-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mechanize
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 2.0.1
21
+ version: 2.7.2
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,23 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 2.0.1
30
- - !ruby/object:Gem::Dependency
31
- name: ruby-aaws
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: 0.7.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.7.0
29
+ version: 2.7.2
46
30
  description: Until there is a Kindle API, this will suffice.
47
31
  email: eric@prudentiadigital.com
48
32
  executables: []
@@ -50,8 +34,7 @@ extensions: []
50
34
  extra_rdoc_files: []
51
35
  files:
52
36
  - lib/kindle_highlights.rb
53
- - lib/kindle_highlights/kindle_highlight.rb
54
- - lib/kindle_highlights/highlight.rb
37
+ - lib/kindle_highlights/client.rb
55
38
  homepage: https://github.com/speric/kindle-highlights
56
39
  licenses: []
57
40
  post_install_message:
@@ -77,3 +60,4 @@ signing_key:
77
60
  specification_version: 3
78
61
  summary: Kindle highlights
79
62
  test_files: []
63
+ has_rdoc:
@@ -1,27 +0,0 @@
1
- class KindleHighlight::Highlight
2
-
3
- attr_accessor :annotation_id, :asin, :author, :title, :content
4
-
5
- @@amazon_items = Hash.new
6
-
7
- def initialize(highlight)
8
- self.annotation_id = highlight.xpath("form/input[@id='annotation_id']").attribute("value").value
9
- self.asin = highlight.xpath("p/span[@class='hidden asin']").text
10
- self.content = highlight.xpath("span[@class='highlight']").text
11
- amazon_item = lookup_or_get_from_cache(self.asin)
12
- self.author = amazon_item.item_attributes.author.to_s
13
- self.title = amazon_item.item_attributes.title.to_s
14
- end
15
-
16
- def lookup_or_get_from_cache(asin)
17
- unless @@amazon_items.has_key?(asin)
18
- request = Request.new
19
- request.locale = 'us'
20
- response = ResponseGroup.new('Small')
21
- lookup = Amazon::AWS::ItemLookup.new('ASIN', {'ItemId' => asin, 'MerchantId' => 'Amazon'})
22
- amazon_item = request.search(lookup, response).item_lookup_response[0].items.item.first
23
- @@amazon_items[asin] = amazon_item
24
- end
25
- @@amazon_items[asin]
26
- end
27
- end
@@ -1,25 +0,0 @@
1
- class KindleHighlight
2
-
3
- attr_accessor :highlights
4
-
5
- def initialize(email_address, password)
6
- @agent = Mechanize.new
7
- @agent.user_agent_alias = 'Windows Mozilla'
8
- @agent.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
9
- page = @agent.get("https://www.amazon.com/ap/signin?openid.assoc_handle=amzn_kindle&openid.mode=checkid_setup&pageId=amzn_kindle&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Fkindle.amazon.com%3A443%2Fauthenticate%2Flogin_callback%3Fwctx%3D%252F&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.pape.max_auth_age=0&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0")
10
- @amazon_form = page.form('signIn')
11
- @amazon_form.email = email_address
12
- @amazon_form.password = password
13
- scrape_highlights
14
- end
15
-
16
- def scrape_highlights
17
- signin_submission = @agent.submit(@amazon_form)
18
- highlights_page = @agent.click(signin_submission.link_with(:text => /Your Highlights/))
19
- collected_highlights = Array.new
20
- highlights_page.search(".//div[@class='highlightRow yourHighlight']").each do |h|
21
- collected_highlights << Highlight.new(h)
22
- end
23
- self.highlights = collected_highlights
24
- end
25
- end