siuying-kindle-highlights 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/kindle-highlights.rb +75 -0
  2. metadata +70 -0
@@ -0,0 +1,75 @@
1
+ require 'rubygems'
2
+ require 'mechanize'
3
+ require 'asin'
4
+
5
+ class KindleHighlight
6
+ attr_accessor :highlights
7
+ attr_accessor :next_url, :fetched
8
+
9
+ def initialize(email_address, password)
10
+ @highlights = Array.new
11
+ @agent = Mechanize.new
12
+ @fetched = false
13
+
14
+ page = @agent.get("https://www.amazon.com/ap/signin?openid.return_to=https%3A%2F%2Fkindle.amazon.com%3A443%2Fauthenticate%2Flogin_callback%3Fwctx%3D%252F&pageId=amzn_kindle&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.pape.max_auth_age=0&openid.assoc_handle=amzn_kindle&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select")
15
+ @amazon_form = page.form('signIn')
16
+ @amazon_form.email = email_address
17
+ @amazon_form.password = password
18
+ end
19
+
20
+ def scrape_highlights
21
+ if self.next_url.nil?
22
+ signin_submission = @agent.submit(@amazon_form)
23
+ highlights_page = @agent.click(signin_submission.link_with(:text => /Your Highlights/))
24
+ self.fetched = true
25
+ else
26
+ highlights_page = @agent.get(self.next_url)
27
+ end
28
+
29
+ new_highlights = Array.new
30
+ highlights_page.search(".//div[@class='highlightRow yourHighlight']").each do |h|
31
+ new_highlights << Highlight.new(h)
32
+ end
33
+ self.highlights = self.highlights + new_highlights
34
+ self.next_url = highlights_page.search("a#nextBookLink").attribute('href') rescue nil
35
+ new_highlights
36
+ end
37
+
38
+ def has_more?
39
+ !self.fetched || (self.fetched && !self.next_url.nil?)
40
+ end
41
+ end
42
+
43
+ class KindleHighlight::Highlight
44
+ include ASIN::Client
45
+
46
+ attr_accessor :annotation_id, :asin, :end_location, :note
47
+ attr_accessor :details_url, :image_url, :title, :author, :content
48
+
49
+ @@amazon_items = Hash.new
50
+
51
+ def initialize(highlight)
52
+ self.annotation_id = highlight.xpath("form/input[@id='annotation_id']").attribute("value").value
53
+ self.asin = highlight.xpath("p/span[@class='hidden asin']").text
54
+ self.content = highlight.xpath("span[@class='highlight']").text rescue ""
55
+ self.note = highlight.xpath("span[@class='noteContent']").text rescue ""
56
+ self.end_location = highlight.xpath("span[@class='end_location']").text
57
+
58
+ amazon_item = lookup_or_get_from_cache(self.asin)
59
+ self.title = amazon_item.title
60
+ self.author = amazon_item.raw.ItemAttributes.Author rescue nil
61
+ self.details_url = amazon_item.details_url
62
+ self.image_url = amazon_item.image_url
63
+ end
64
+
65
+ def lookup_or_get_from_cache(asin)
66
+ unless @@amazon_items.has_key?(asin)
67
+ @@amazon_items[asin] = lookup(asin).first
68
+ end
69
+ @@amazon_items[asin]
70
+ end
71
+
72
+ def to_s
73
+ "<Highlight##{annotation_id}>"
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: siuying-kindle-highlights
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eric Farkas
9
+ - Francis Chong
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-10-15 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mechanize
17
+ requirement: &70358233487240 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70358233487240
26
+ - !ruby/object:Gem::Dependency
27
+ name: asin
28
+ requirement: &70358233437920 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *70358233437920
37
+ description: Until there is a Kindle API, this will suffice.
38
+ email:
39
+ - eric@prudentiadigital.com
40
+ - francis@ignition.hk
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - lib/kindle-highlights.rb
46
+ homepage: https://github.com/siuying/kindle-highlights
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.15
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Kindle highlights
70
+ test_files: []