Class: KindleManager::HighlightsAdapter

Inherits:
BaseAdapter show all
Defined in:
lib/kindle_manager/adapters/highlights_adapter.rb

Constant Summary collapse

KINDLE_HIGHLIGHT_URL =
"https://read.#{AmazonInfo.domain}/kp/notebook"

Instance Attribute Summary collapse

Attributes inherited from BaseAdapter

#options, #session, #store

Instance Method Summary collapse

Methods inherited from BaseAdapter

#initialize, #limit, #max_scroll_attempts

Constructor Details

This class inherits a constructor from KindleManager::BaseAdapter

Instance Attribute Details

#failed_library_idsObject

Returns the value of attribute failed_library_ids.



5
6
7
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 5

def failed_library_ids
  @failed_library_ids
end

#library_idsObject

Returns the value of attribute library_ids.



5
6
7
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 5

def library_ids
  @library_ids
end

#loaded_library_idsObject

Returns the value of attribute loaded_library_ids.



5
6
7
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 5

def loaded_library_ids
  @loaded_library_ids
end

Instance Method Details

#check_library_scrollObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 40

def check_library_scroll
  if session.first('#library .kp-notebook-scroller-addon').present?
    scroll_top = session.evaluate_script("$('#library .kp-notebook-scroller-addon').get(0).scrollTop")
    scroll_height = session.evaluate_script("$('#library .kp-notebook-scroller-addon').get(0).scrollHeight")
    offset_height = session.evaluate_script("$('#library .kp-notebook-scroller-addon').get(0).offsetHeight")
    log "Scroll top:#{scroll_top} height:#{scroll_height} offset_height:#{offset_height}"
    scroll_top
  else
    log "Couldn't find the node '#library .kp-notebook-scroller-addon'"
    0
  end
end

#fetchObject



7
8
9
10
11
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 7

def fetch
  go_to_kindle_highlights_page
  fetch_library_ids
  fetch_kindle_highlights
end

#fetch_book_with_highlights(library_id) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 67

def fetch_book_with_highlights(library_id)
  log "Fetching highlights for the book #{library_id}"
  session.first("##{library_id}").click
  wait_for_selector('#annotations .kp-notebook-annotation-container', wait_time: 10)
  title = doc.css('#annotations .kp-notebook-annotation-container h3.kp-notebook-metadata').try!(:text)
  highlights_count, notes_count = fetch_highlights_and_notes
  snapshot_page("Saving page for [#{title}] (#{library_id}) highlights:#{highlights_count} notes:#{notes_count}")
  if title.present?
    self.loaded_library_ids << library_id
  else
    self.failed_library_ids << library_id
    log "[ERROR] Failed to load #{library_id} or this book doesn't have any highlights and notes"
  end
end

#fetch_highlights_and_notesObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 82

def fetch_highlights_and_notes
  highlights_count = notes_count = nil
  10.times do
    sleep(1)
    highlights_count = doc.css('#annotations .kp-notebook-annotation-container #kp-notebook-highlights-count').try!(:text)
    notes_count = doc.css('#annotations .kp-notebook-annotation-container #kp-notebook-notes-count').try!(:text)
    break if highlights_count != '--' && notes_count != '--'
  end
  [highlights_count, notes_count]
end

#fetch_kindle_highlightsObject



57
58
59
60
61
62
63
64
65
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 57

def fetch_kindle_highlights
  library_ids.each_with_index do |library_id,i|
    break if limit && limit < i+1
    next if loaded_library_ids.include?(library_id)
    fetch_book_with_highlights(library_id)
  end
  report_failed_ids
  snapshot_page
end

#fetch_library_idsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 23

def fetch_library_ids
  last_scroll_top = check_library_scroll
  20.times do
    scroll_library_pane(last_scroll_top + 20000)
    sleep(2)
    new_scroll_top = check_library_scroll
    break if limit && limit < doc.css('#library #kp-notebook-library > .a-row').size
    break if last_scroll_top == new_scroll_top
    last_scroll_top = new_scroll_top
  end
  snapshot_page
  self.library_ids = doc.css('#library #kp-notebook-library > .a-row').map{|e| e['id'] }
  self.loaded_library_ids ||= []
  self.failed_library_ids ||= []
  log "Number of library ids is #{library_ids.size}"
end

#go_to_kindle_highlights_pageObject



13
14
15
16
17
18
19
20
21
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 13

def go_to_kindle_highlights_page
  unless session.current_url == KINDLE_HIGHLIGHT_URL
    log "Visiting kindle highlights page"
    session.visit KINDLE_HIGHLIGHT_URL
  end
  wait_for_selector('#library')
  check_library_scroll
  snapshot_page
end

#loadObject



97
98
99
100
101
102
103
104
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 97

def load
  books = []
  store.list_html_files.each do |file|
    parser = KindleManager::HighlightsParser.new(file)
    books += parser.parse
  end
  books.reject(&:invalid?).sort_by{|b| [-b.last_annotated_on.to_time.to_i, -b.fetched_at.to_i] }.uniq(&:asin)
end

#report_failed_idsObject



93
94
95
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 93

def report_failed_ids
  log("May have failed with #{failed_library_ids.inspect}. Retry with client.adapter.session.first('#B000000000').click") if failed_library_ids.size > 0
end

#scroll_library_pane(target_scroll_top) ⇒ Object



53
54
55
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 53

def scroll_library_pane(target_scroll_top)
  session.evaluate_script("$('#library .kp-notebook-scroller-addon').get(0).scrollTop = #{target_scroll_top}")
end

#snapshot_page(message = nil) ⇒ Object



106
107
108
109
# File 'lib/kindle_manager/adapters/highlights_adapter.rb', line 106

def snapshot_page(message = nil)
  store.record_page
  log(message.presence || "Saving page")
end